improve serialisation of booleans

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@35557 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2012-09-10 10:27:59 +00:00
parent ab65e628d3
commit 2a32245d60
3 changed files with 77 additions and 62 deletions

View file

@ -1,3 +1,9 @@
2012-09-10 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSJSONSerialization.m:
* Source/NSPropertyList.m:
Improve serialisation of booleans.
2012-09-10 Richard Frith-Macdonald <rfm@gnu.org> 2012-09-10 Richard Frith-Macdonald <rfm@gnu.org>
* Headers/Foundation/NSDate.h: Declare new OSX method to add time * Headers/Foundation/NSDate.h: Declare new OSX method to add time

View file

@ -11,6 +11,11 @@
#import <GNUstepBase/NSObject+GNUstepBase.h> #import <GNUstepBase/NSObject+GNUstepBase.h>
#import "GSFastEnumeration.h" #import "GSFastEnumeration.h"
/* Boolean constants.
*/
static id boolN;
static id boolY;
/** /**
* The number of (unicode) characters to fetch from the source at once. * The number of (unicode) characters to fetch from the source at once.
*/ */
@ -614,7 +619,7 @@ parseValue(ParserState *state)
&& (consumeChar(state) == 'e')) && (consumeChar(state) == 'e'))
{ {
consumeChar(state); consumeChar(state);
return [[NSNumber alloc] initWithBool: YES]; return [boolY retain];
} }
break; break;
} }
@ -626,7 +631,7 @@ parseValue(ParserState *state)
&& (consumeChar(state) == 'e')) && (consumeChar(state) == 'e'))
{ {
consumeChar(state); consumeChar(state);
return [[NSNumber alloc] initWithBool: NO]; return [boolN retain];
} }
break; break;
} }
@ -710,8 +715,11 @@ getEncoding(const uint8_t BOM[4], ParserState *state)
/** /**
* Classes that are permitted to be written. * Classes that are permitted to be written.
*/ */
static Class NSNullClass, NSArrayClass, NSStringClass, NSDictionaryClass, static Class NSArrayClass;
NSNumberClass; static Class NSDictionaryClass;
static Class NSNullClass;
static Class NSNumberClass;
static Class NSStringClass;
static NSCharacterSet *escapeSet; static NSCharacterSet *escapeSet;
@ -814,23 +822,17 @@ writeObject(id obj, NSMutableString *output, NSInteger tabs)
[output appendFormat: @"\"%@\"", obj]; [output appendFormat: @"\"%@\"", obj];
} }
} }
else if (obj == boolN)
{
[output appendString: @"false"];
}
else if (obj == boolY)
{
[output appendString: @"true"];
}
else if ([obj isKindOfClass: NSNumberClass]) else if ([obj isKindOfClass: NSNumberClass])
{ {
if ([obj objCType][0] == @encode(BOOL)[0]) [output appendFormat: @"%g", [obj doubleValue]];
{
if ([obj boolValue])
{
[output appendString: @"true"];
}
else
{
[output appendString: @"false"];
}
}
else
{
[output appendFormat: @"%g", [obj doubleValue]];
}
} }
else if ([obj isKindOfClass: NSNullClass]) else if ([obj isKindOfClass: NSNullClass])
{ {
@ -853,7 +855,8 @@ writeObject(id obj, NSMutableString *output, NSInteger tabs)
NSNumberClass = [NSNumber class]; NSNumberClass = [NSNumber class];
escapeSet escapeSet
= [[NSCharacterSet characterSetWithCharactersInString: @"\"\\"] retain]; = [[NSCharacterSet characterSetWithCharactersInString: @"\"\\"] retain];
boolN = [[NSNumber numberWithBool: NO] retain];
boolY = [[NSNumber numberWithBool: YES] retain];
} }
+ (NSData*) dataWithJSONObject: (id)obj + (NSData*) dataWithJSONObject: (id)obj

View file

@ -51,6 +51,16 @@
#import "GSPrivate.h" #import "GSPrivate.h"
static id boolN = nil;
static id boolY = nil;
static void
setupBooleans()
{
if (nil == boolN) boolN = [[NSNumber numberWithBool: NO] retain];
if (nil == boolY) boolY = [[NSNumber numberWithBool: YES] retain];
}
@class GSSloppyXMLParser; @class GSSloppyXMLParser;
#define inrange(ch,min,max) ((ch)>=(min) && (ch)<=(max)) #define inrange(ch,min,max) ((ch)>=(min) && (ch)<=(max))
@ -331,11 +341,11 @@ foundIgnorableWhitespace: (NSString *)string
} }
else if ([elementName isEqualToString: @"true"]) else if ([elementName isEqualToString: @"true"])
{ {
ASSIGN(plist, [NSNumber numberWithBool: YES]); ASSIGN(plist, boolY);
} }
else if ([elementName isEqualToString: @"false"]) else if ([elementName isEqualToString: @"false"])
{ {
ASSIGN(plist, [NSNumber numberWithBool: NO]); ASSIGN(plist, boolN);
} }
else if ([elementName isEqualToString: @"plist"]) else if ([elementName isEqualToString: @"plist"])
{ {
@ -1155,11 +1165,11 @@ static id parsePlItem(pldata* pld)
{ {
if (ptr[0] == 'Y') if (ptr[0] == 'Y')
{ {
result = [[NSNumber alloc] initWithBool: YES]; result = [boolY retain];
} }
else if (ptr[0] == 'N') else if (ptr[0] == 'N')
{ {
result = [[NSNumber alloc] initWithBool: NO]; result = [boolN retain];
} }
else else
{ {
@ -1830,46 +1840,41 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
PString(obj, dest); PString(obj, dest);
} }
} }
else if (obj == boolY)
{
if (x == NSPropertyListXMLFormat_v1_0)
{
[dest appendBytes: "<true/>\n" length: 8];
}
else if (x == NSPropertyListGNUstepFormat)
{
[dest appendBytes: "<*BY>" length: 5];
}
else
{
PString([obj description], dest);
}
}
else if (obj == boolN)
{
if (x == NSPropertyListXMLFormat_v1_0)
{
[dest appendBytes: "<false/>\n" length: 9];
}
else if (x == NSPropertyListGNUstepFormat)
{
[dest appendBytes: "<*BN>" length: 5];
}
else
{
PString([obj description], dest);
}
}
else if ([obj isKindOfClass: NSNumberClass]) else if ([obj isKindOfClass: NSNumberClass])
{ {
const char *t = [obj objCType]; const char *t = [obj objCType];
if (*t == 'c' || *t == 'C') if (strchr("cCsSiIlLqQ", *t) != 0)
{
BOOL val = [obj boolValue];
if (val == YES)
{
if (x == NSPropertyListXMLFormat_v1_0)
{
[dest appendBytes: "<true/>\n" length: 8];
}
else if (x == NSPropertyListGNUstepFormat)
{
[dest appendBytes: "<*BY>" length: 5];
}
else
{
PString([obj description], dest);
}
}
else
{
if (x == NSPropertyListXMLFormat_v1_0)
{
[dest appendBytes: "<false/>\n" length: 9];
}
else if (x == NSPropertyListGNUstepFormat)
{
[dest appendBytes: "<*BN>" length: 5];
}
else
{
PString([obj description], dest);
}
}
}
else if (strchr("sSiIlLqQ", *t) != 0)
{ {
if (x == NSPropertyListXMLFormat_v1_0) if (x == NSPropertyListXMLFormat_v1_0)
{ {
@ -2369,6 +2374,7 @@ static BOOL classInitialized = NO;
[plDictionary instanceMethodForSelector: @selector(setObject:forKey:)]; [plDictionary instanceMethodForSelector: @selector(setObject:forKey:)];
setupQuotables(); setupQuotables();
setupBooleans();
} }
} }
@ -2985,12 +2991,12 @@ NSAssert(pos + count < _length, NSInvalidArgumentException);
if (next == 0x08) if (next == 0x08)
{ {
// NO // NO
result = [NSNumber numberWithBool: NO]; result = boolN;
} }
else if (next == 0x09) else if (next == 0x09)
{ {
// YES // YES
result = [NSNumber numberWithBool: YES]; result = boolY;
} }
else if ((next >= 0x10) && (next < 0x17)) else if ((next >= 0x10) && (next < 0x17))
{ {