mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-14 10:00:55 +00:00
JSON fixups thanks to Lubomir Rintel <lubo.rintel@gooddata.com>
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@34681 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
f2d0bb8ab0
commit
ae9a3339fb
3 changed files with 68 additions and 38 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
2012-01-31 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
|
* Source/NSJSONSerialization.m: bugfix for empty strings.
|
||||||
|
* Tests/base/NSJSONSerialization/json.m: add a few emtpy element
|
||||||
|
tests and fixup to be more readable
|
||||||
|
|
||||||
2012-01-31 Richard Frith-Macdonald <rfm@gnu.org>
|
2012-01-31 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
* Version: revert last change as Fred pointed out I got it wrong and
|
* Version: revert last change as Fred pointed out I got it wrong and
|
||||||
|
|
|
@ -374,6 +374,10 @@ parseString(ParserState *state)
|
||||||
[str release];
|
[str release];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (nil == val)
|
||||||
|
{
|
||||||
|
val = [NSMutableString new];
|
||||||
|
}
|
||||||
if (!state->mutableStrings)
|
if (!state->mutableStrings)
|
||||||
{
|
{
|
||||||
val = [val makeImmutableCopyOnFail: YES];
|
val = [val makeImmutableCopyOnFail: YES];
|
||||||
|
|
|
@ -1,47 +1,67 @@
|
||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
#import "ObjectTesting.h"
|
#import "ObjectTesting.h"
|
||||||
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
[NSAutoreleasePool new];
|
[NSAutoreleasePool new];
|
||||||
// Simple test JSON, used for all of the examples
|
// Simple test JSON, used for all of the examples
|
||||||
NSString *json = @"\
|
|
||||||
{\
|
|
||||||
\"Image\": {\
|
|
||||||
\"Width\": 800,\
|
|
||||||
\"Height\": 600,\
|
|
||||||
\"Title\": \"View from 15th Floor\",\
|
|
||||||
\"Thumbnail\": {\
|
|
||||||
\"Url\": \"http://www.example.com/image/481989943\",\
|
|
||||||
\"Height\": 125,\
|
|
||||||
\"Width\": \"100\"\
|
|
||||||
},\
|
|
||||||
},\
|
|
||||||
\"IDs\": [116, 943, 234, 38793],\
|
|
||||||
\"escapeTest\": \"\\\"\\u0001\"\
|
|
||||||
}";
|
|
||||||
NSStringEncoding encs[] = {NSUTF8StringEncoding, NSUTF16LittleEndianStringEncoding, NSUTF16BigEndianStringEncoding, NSUTF32LittleEndianStringEncoding, NSUTF32BigEndianStringEncoding};
|
|
||||||
id obj;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i=0 ; i<(sizeof(encs) / sizeof(NSStringEncoding)) ; i++)
|
NSString *json = @"\
|
||||||
|
{\
|
||||||
|
\"Image\": {\
|
||||||
|
\"Width\": 800,\
|
||||||
|
\"Height\": 600,\
|
||||||
|
\"Title\": \"View from 15th Floor\",\
|
||||||
|
\"Thumbnail\": {\
|
||||||
|
\"Url\": \"http://www.example.com/image/481989943\",\
|
||||||
|
\"Height\": 125,\
|
||||||
|
\"Width\": \"100\"\
|
||||||
|
},\
|
||||||
|
},\
|
||||||
|
\"emptyString\":\"\",\"emptyArray\":[],\"emptyObject\":{},\
|
||||||
|
\"IDs\": [116, 943, 234, 38793],\
|
||||||
|
\"escapeTest\": \"\\\"\\u0001\"\
|
||||||
|
}";
|
||||||
|
NSStringEncoding encs[] = {\
|
||||||
|
NSUTF8StringEncoding,\
|
||||||
|
NSUTF16LittleEndianStringEncoding,\
|
||||||
|
NSUTF16BigEndianStringEncoding,\
|
||||||
|
NSUTF32LittleEndianStringEncoding,\
|
||||||
|
NSUTF32BigEndianStringEncoding};
|
||||||
|
NSInputStream *is;
|
||||||
|
NSData *data;
|
||||||
|
id obj;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < (sizeof(encs) / sizeof(NSStringEncoding)); i++)
|
||||||
|
{
|
||||||
|
NSError *e;
|
||||||
|
id tmp;
|
||||||
|
|
||||||
|
data = [json dataUsingEncoding: encs[i]];
|
||||||
|
tmp = [NSJSONSerialization JSONObjectWithData: data options: 0 error: &e];
|
||||||
|
if (i > 0)
|
||||||
{
|
{
|
||||||
NSData *data = [json dataUsingEncoding: encs[i]];
|
PASS([tmp isEqual: obj],
|
||||||
NSError *e;
|
"Decoding in different encodings give the same result");
|
||||||
id tmp = [NSJSONSerialization JSONObjectWithData: data options: 0 error: &e];
|
|
||||||
if (i > 0)
|
|
||||||
{
|
|
||||||
PASS([tmp isEqual: obj], "Decoding in different encodings give the same result");
|
|
||||||
}
|
|
||||||
obj = tmp;
|
|
||||||
}
|
}
|
||||||
PASS([obj count] == 3, "Decoded dictionary had the right number of elements");
|
obj = tmp;
|
||||||
PASS([NSJSONSerialization isValidJSONObject: obj], "Can serialise deserialised JSON");
|
}
|
||||||
NSData *data = [NSJSONSerialization dataWithJSONObject: obj options: NSJSONWritingPrettyPrinted error: 0];
|
|
||||||
PASS([obj isEqual: [NSJSONSerialization JSONObjectWithData: data options: 0 error: 0]], "Round trip worked with pretty printing");
|
PASS([obj count] == 6,
|
||||||
data = [NSJSONSerialization dataWithJSONObject: obj options: 0 error: 0];
|
"Decoded dictionary had the right number of elements");
|
||||||
PASS([obj isEqual: [NSJSONSerialization JSONObjectWithData: data options: 0 error: 0]], "Round trip worked with ugly printing");
|
PASS([NSJSONSerialization isValidJSONObject: obj],
|
||||||
PASS([obj isEqual: [NSJSONSerialization JSONObjectWithStream: [NSInputStream inputStreamWithData:data] options: 0 error: 0]], "Round trip worked through stream");
|
"Can serialise deserialised JSON");
|
||||||
return 0;
|
|
||||||
|
data = [NSJSONSerialization dataWithJSONObject: obj
|
||||||
|
options: NSJSONWritingPrettyPrinted error: 0];
|
||||||
|
PASS_EQUAL([NSJSONSerialization JSONObjectWithData: data options: 0 error: 0],
|
||||||
|
obj, "Round trip worked with pretty printing");
|
||||||
|
data = [NSJSONSerialization dataWithJSONObject: obj options: 0 error: 0];
|
||||||
|
PASS_EQUAL([NSJSONSerialization JSONObjectWithData: data options: 0 error: 0],
|
||||||
|
obj, "Round trip worked with ugly printing");
|
||||||
|
is = [NSInputStream inputStreamWithData: data];
|
||||||
|
PASS_EQUAL([NSJSONSerialization JSONObjectWithStream: is options: 0 error: 0],
|
||||||
|
obj, "Round trip worked through stream");
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue