-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGWSJSONCoder.m
executable file
·1643 lines (1523 loc) · 42 KB
/
GWSJSONCoder.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
Copyright (C) 2011 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <[email protected]>
Date: May 2011
This file is part of the WebServices Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
$Date: 2007-09-24 14:19:12 +0100 (Mon, 24 Sep 2007) $ $Revision: 25500 $
*/
#import <Foundation/Foundation.h>
#import "GWSPrivate.h"
NSString * const GWSJSONResultKey = @"GWSJSONResult";
static NSString * const ver1 = @"1.0";
static NSString * const ver2 = @"2.0";
static id boolN;
static id boolY;
static id null;
static Class NSArrayClass;
static Class NSDataClass;
static Class NSDateClass;
static Class NSDictionaryClass;
static Class NSNullClass;
static Class NSNumberClass;
static Class NSStringClass;
static NSTimeZone *gmt;
static BOOL useTimeZone = NO;
static NSString*
JSONQuote(NSString *str)
{
unsigned length = [str length];
unsigned output = 2;
unichar *from;
unsigned i = 0;
unichar *to;
unsigned j = 0;
if (length == 0)
{
return @"\"\"";
}
from = NSZoneMalloc (NSDefaultMallocZone(), sizeof(unichar) * length);
[str getCharacters: from];
for (i = 0; i < length; i++)
{
unichar c = from[i];
if (c == '"' || c == '\\'
|| c == '\b' || c == '\f' || c == '\n' || c == '\r' || c == '\t')
{
output += 2;
}
else if (c < 0x20 || c > 0x7f)
{
output += 6;
}
else
{
output++;
}
}
to = NSZoneMalloc (NSDefaultMallocZone(), sizeof(unichar) * output);
to[j++] = '"';
for (i = 0; i < length; i++)
{
unichar c = from[i];
if (c == '"' || c == '\\'
|| c == '\b' || c == '\f' || c == '\n' || c == '\r' || c == '\t')
{
to[j++] = '\\';
switch (c)
{
case '\\': to[j++] = '\\'; break;
case '\b': to[j++] = 'b'; break;
case '\f': to[j++] = 'f'; break;
case '\n': to[j++] = 'n'; break;
case '\r': to[j++] = 'r'; break;
case '\t': to[j++] = 't'; break;
default: to[j++] = '"'; break;
}
}
else if (c < 0x20 || c > 0x7f)
{
char buf[5];
to[j++] = '\\';
to[j++] = 'u';
sprintf(buf, "%04x", c);
to[j++] = buf[0];
to[j++] = buf[1];
to[j++] = buf[2];
to[j++] = buf[3];
}
else
{
to[j++] = c;
}
}
to[j] = '"';
str = [[NSStringClass alloc] initWithCharacters: to length: output];
NSZoneFree (NSDefaultMallocZone (), to);
[str autorelease];
NSZoneFree (NSDefaultMallocZone (), from);
return str;
}
typedef struct {
const unsigned char *buffer;
unsigned length;
unsigned line;
unsigned column;
unsigned index;
const char *error;
} context;
static inline int
get(context *ctxt)
{
if (ctxt->index < ctxt->length)
{
int c = ctxt->buffer[ctxt->index++];
ctxt->column++;
if (c == '\n')
{
ctxt->line++;
ctxt->column = 1;
}
return c;
}
return -1;
}
static inline int
skipSpace(context *ctxt)
{
while (ctxt->index < ctxt->length && isspace(ctxt->buffer[ctxt->index]))
{
get(ctxt);
}
if (ctxt->index < ctxt->length)
{
return ctxt->buffer[ctxt->index];
}
return -1;
}
static id
newParsed(context *ctxt)
{
int c;
skipSpace(ctxt);
c = get(ctxt);
if (c < 0)
{
return nil;
}
else if ('"' == c)
{
BOOL ascii = YES;
BOOL escapes = NO;
BOOL unicode = NO;
unsigned start = ctxt->index;
NSString *s;
while ((c = get(ctxt)) >= 0)
{
if ('\\' == c)
{
escapes = YES;
c = get(ctxt);
if ('u' == c)
{
unicode = YES;
}
else if (c < 0)
{
break;
}
}
else if ('"' == c)
{
break;
}
else if (c > 0x7f)
{
ascii = NO;
}
}
if (c < 0)
{
ctxt->error = "premature end of string";
ctxt->index = ctxt->length;
return nil;
}
if (NO == escapes)
{
s = [NSStringClass alloc];
if (YES == ascii)
{
s = [s initWithBytes: ctxt->buffer + start
length: ctxt->index - start - 1
encoding: NSASCIIStringEncoding];
}
else
{
s = [s initWithBytes: ctxt->buffer + start
length: ctxt->index - start - 1
encoding: NSUTF8StringEncoding];
}
}
else if (NO == unicode)
{
char *buf;
int len;
int pos = start;
int end = ctxt->index - 1;
buf = malloc(ctxt->index - start - 1);
for (len = 0; pos < end; len++)
{
buf[len] = ctxt->buffer[pos++];
if ('\\' == buf[len])
{
buf[len] = ctxt->buffer[pos++];
switch (buf[len])
{
case 'b': buf[len] = '\b'; break;
case 'f': buf[len] = '\f'; break;
case 'r': buf[len] = '\r'; break;
case 'n': buf[len] = '\n'; break;
case 't': buf[len] = '\t'; break;
default: break;
}
}
}
s = [NSStringClass alloc];
if (YES == ascii)
{
s = [s initWithBytesNoCopy: buf
length: len
encoding: NSASCIIStringEncoding
freeWhenDone: YES];
}
else
{
s = [s initWithBytesNoCopy: buf
length: len
encoding: NSUTF8StringEncoding
freeWhenDone: YES];
}
}
else
{
NSMutableString *m;
m = [NSMutableString alloc];
s = m = [m initWithBytes: ctxt->buffer + start
length: ctxt->index - start - 1
encoding: NSUTF8StringEncoding];
if (nil != s)
{
NSRange r = NSMakeRange(0, [m length]);
r = [m rangeOfString: @"\\" options: NSLiteralSearch range: r];
while (r.length > 0)
{
unsigned pos = r.location;
NSString *rep;
c = [m characterAtIndex: pos + 1];
if ('u' == c)
{
const char *hex;
unichar u;
if (pos + 6 > [m length])
{
ctxt->error = "short unicode escape in string";
ctxt->index = ctxt->length;
RELEASE(s);
return nil;
}
hex = [[m substringWithRange: NSMakeRange(pos + 2, 4)]
UTF8String];
if (isxdigit(hex[0]) && isxdigit(hex[1])
&& isxdigit(hex[2]) && isxdigit(hex[3]))
{
u = (unichar) strtol(hex, 0, 16);
}
else
{
ctxt->error = "invalid unicode escape in string";
ctxt->index = ctxt->length;
RELEASE(s);
return nil;
}
rep = [NSStringClass stringWithCharacters: &u length: 1];
r.length += 5;
}
else
{
if ('"' == c) rep = @"\"";
else if ('\\' == c) rep = @"\\";
else if ('b' == c) rep = @"\b";
else if ('f' == c) rep = @"\f";
else if ('r' == c) rep = @"\r";
else if ('n' == c) rep = @"\n";
else if ('t' == c) rep = @"\t";
else rep = [NSStringClass
stringWithFormat: @"%c", (char)c];
r.length += 1;
}
[m replaceCharactersInRange: r withString: rep];
pos++;
r = NSMakeRange(pos, [m length] - pos);
r = [m rangeOfString: @"\\"
options: NSLiteralSearch
range: r];
}
}
}
if (nil == s && 0 == ctxt->error)
{
ctxt->error = "invalid string";
ctxt->index = start;
}
return s;
}
else if ('[' == c)
{
NSMutableArray *a;
a = [[NSMutableArray alloc] initWithCapacity: 100];
for (;;)
{
id o;
if (nil != (o = newParsed(ctxt)))
{
[a addObject: o];
[o release];
}
else if (ctxt->error != 0)
{
[a release];
return nil;
}
c = skipSpace(ctxt);
if (']' == c)
{
get(ctxt);
return a;
}
if (c != ',')
{
if (c < 0)
{
ctxt->error = "premature end of array";
}
else
{
ctxt->error = "bad character in array";
}
ctxt->index = ctxt->length;
[a release];
return nil;
}
get(ctxt); // Skip past comma
}
}
else if ('{' == c)
{
NSMutableDictionary *d;
d = [[NSMutableDictionary alloc] initWithCapacity: 100];
for (;;)
{
id k;
id v;
k = newParsed(ctxt);
c = skipSpace(ctxt);
if ('}' == c && nil == k)
{
get(ctxt);
return d; // Empty
}
if (NO == [k isKindOfClass: NSStringClass])
{
[k release];
if (0 == ctxt->error)
{
ctxt->error = "non-string value for key";
ctxt->index = ctxt->length;
}
[d release];
return nil;
}
if (':' != c)
{
[k release];
ctxt->error = "missing colon after key";
ctxt->index = ctxt->length;
[d release];
return nil;
}
get(ctxt); // Skip the colon
v = newParsed(ctxt);
if (nil == v)
{
[k release];
if (0 == ctxt->error)
{
ctxt->error = "missing value after colon";
ctxt->index = ctxt->length;
}
[d release];
return nil;
}
c = skipSpace(ctxt);
if (',' == c)
{
[d setObject: v forKey: k];
[k release];
[v release];
get(ctxt);
}
else if ('}' == c)
{
[d setObject: v forKey: k];
[k release];
[v release];
get(ctxt);
return d;
}
else
{
[k release];
[v release];
if (c < 0)
{
ctxt->error = "premature end of object";
}
else
{
ctxt->error = "bad character in object";
}
ctxt->index = ctxt->length;
[d release];
return nil;
}
}
}
else if ('-' == c || isdigit(c))
{
const char *s = (const char*)ctxt->buffer + ctxt->index - 1;
char *e = 0;
NSNumber *n = nil;
double d;
long long l;
unsigned pos = ctxt->index;
BOOL tryFloat = NO;
if ('-' == 'c') pos++;
while (pos < ctxt->length && isdigit(ctxt->buffer[pos]))
{
pos++;
}
if (pos < ctxt->length && '.' == ctxt->buffer[pos])
{
tryFloat = YES;
pos++;
while (pos < ctxt->length && isdigit(ctxt->buffer[pos]))
{
pos++;
}
}
if (pos < ctxt->length
&& ('e' == ctxt->buffer[pos] || 'E' == ctxt->buffer[pos]))
{
tryFloat = YES;
pos++;
if (pos < ctxt->length
&& ('+' == ctxt->buffer[pos] || '-' == ctxt->buffer[pos]))
{
pos++;
}
while (pos < ctxt->length && isdigit(ctxt->buffer[pos]))
{
pos++;
}
}
if (YES == tryFloat)
{
d = strtod(s, &e);
if (e == s)
{
ctxt->error = "unparsable numeric value";
ctxt->index = ctxt->length;
return nil;
}
n = [[NSNumberClass alloc] initWithDouble: d];
}
else
{
l = strtoll(s, &e, 10);
if (e == s)
{
ctxt->error = "unparsable integer value";
ctxt->index = ctxt->length;
return nil;
}
n = [[NSNumberClass alloc] initWithLongLong: l];
}
if (nil == n)
{
ctxt->error = "failed to parse numeric value";
ctxt->index = ctxt->length;
return nil;
}
/* Step past the numeric value.
*/
while (ctxt->index < pos)
{
get(ctxt);
}
return n;
}
else if ('t' == c)
{
if (get(ctxt) == 'r' && get(ctxt) == 'u' && get(ctxt) == 'e')
{
return [boolY retain];
}
ctxt->error = "bad character (expecting 'true')";
ctxt->index = ctxt->length;
return nil;
}
else if ('f' == c)
{
if (get(ctxt) == 'a' && get(ctxt) == 'l' && get(ctxt) == 's'
&& get(ctxt) == 'e')
{
return [boolN retain];
}
ctxt->error = "bad character (expecting 'false')";
ctxt->index = ctxt->length;
return nil;
}
else if ('n' == c)
{
if (get(ctxt) == 'u' && get(ctxt) == 'l' && get(ctxt) == 'l')
{
return [null retain];
}
ctxt->error = "bad character (expecting 'null')";
ctxt->index = ctxt->length;
return nil;
}
else
{
ctxt->index--; // Push back character
return nil;
}
}
@implementation GWSJSONCoder
+ (void) initialize
{
NSArrayClass = [NSArray class];
NSDataClass = [NSData class];
NSDateClass = [NSDate class];
NSDictionaryClass = [NSDictionary class];
NSNullClass = [NSNull class];
NSNumberClass = [NSNumber class];
NSStringClass = [NSString class];
boolY = [[NSNumberClass numberWithBool: YES] retain];
boolN = [[NSNumberClass numberWithBool: NO] retain];
null = [[NSNullClass null] retain];
gmt = [[NSTimeZone timeZoneWithName: @"GMT"] retain];
}
- (void) appendObject: (id)o
{
NSMutableString *ms = [self mutableString];
if (nil == o || null == o || [o isKindOfClass: NSNullClass])
{
[ms appendString: @"null"];
}
else if (YES == [o isKindOfClass: NSStringClass])
{
[ms appendString: JSONQuote(o)];
}
else if (o == boolY)
{
[ms appendString: @"true"];
}
else if (o == boolN)
{
[ms appendString: @"false"];
}
else if (YES == [o isKindOfClass: NSNumberClass])
{
const char *t = [o objCType];
if (strchr("cCsSiIlLqQ", *t) != 0)
{
long long i = [(NSNumber*)o longLongValue];
[ms appendFormat: @"%lld", i];
}
else
{
[ms appendFormat: @"%g", [(NSNumber*)o doubleValue]];
}
}
else if (YES == [o isKindOfClass: NSDataClass])
{
[ms appendString: @"\""];
[ms appendString: [self encodeBase64From: o]];
[ms appendString: @"\""];
}
else if (YES == [o isKindOfClass: NSDateClass])
{
[ms appendString: @"\""];
[ms appendString: [self encodeDateTimeFrom: o]];
[ms appendString: @"\""];
}
else if (YES == [o isKindOfClass: NSArrayClass])
{
unsigned i;
unsigned c = [o count];
[ms appendString: @"["];
[self indent];
for (i = 0; i < c; i++)
{
if (i > 0)
{
[ms appendString: @","];
}
[self nl];
[self appendObject: [o objectAtIndex: i]];
}
[self unindent];
[self nl];
[ms appendString: @"]"];
}
else if (YES == [o isKindOfClass: NSDictionaryClass])
{
NSEnumerator *kEnum;
NSString *key;
BOOL first = YES;
kEnum = [[o objectForKey: GWSOrderKey] objectEnumerator];
if (kEnum == nil)
{
kEnum = [o keyEnumerator];
}
[ms appendString: @"{"];
[self indent];
while ((key = [kEnum nextObject]))
{
if (YES == first)
{
first = NO;
}
else
{
[ms appendString: @","];
[self unindent];
}
[self nl];
[ms appendString: JSONQuote([key description])];
[ms appendString: @":"];
[self indent];
[self nl];
[self appendObject: [o objectForKey: key]];
}
if (NO == first)
{
[self unindent];
}
[self unindent];
[self nl];
[ms appendString: @"}"];
}
else
{
[ms appendString: JSONQuote([o description])];
}
}
/* Build JSON-RPC request or just build a JSON object as a document.
* Return YES if it's a JSON-RPC, NO otherwise.
*/
- (BOOL) _build: (id*)container
parameters: (NSDictionary*)parameters
order: (NSArray*)order
{
NSUInteger c;
NSUInteger i;
id o;
id p;
NSString *version;
BOOL positional = YES;
*container = [NSMutableDictionary dictionaryWithCapacity: 4];
o = [parameters objectForKey: GWSOrderKey];
if (nil != o)
{
if (order != nil && [order isEqual: o] == NO)
{
NSLog(@"Parameter order specified both in the 'order' argument and using GWSOrderKey. Using the value from GWSOrderkey.");
}
order = o;
}
o = [parameters objectForKey: GWSParametersKey];
if (nil != o)
{
parameters = o;
}
/* Get the JSON-RPC version ... nil means we just want a plain object.
*/
version = [parameters objectForKey: GWSRPCVersionKey];
if (nil == version)
{
version = [self version];
}
/* If we have an RPC version, we must have an RPC request/response ID too.
*/
if (nil != version)
{
id rid;
rid = [parameters objectForKey: GWSRPCIDKey];
if (nil == rid)
{
rid = [self RPCID];
if (nil == rid)
{
rid = null;
}
}
[*container setObject: rid forKey: @"id"];
}
if (ver2 == version)
{
[*container setObject: ver2 forKey: @"jsonrpc"];
/* If this request has no order specified and is version 2
* then we encode parameters by name.
*/
if (nil == order || [order containsObject: GWSJSONResultKey])
{
positional = NO;
}
}
if (nil != version && YES == [self fault])
{
NSMutableDictionary *e;
e = [[NSMutableDictionary alloc] initWithCapacity: 3];
[*container setObject: e forKey: @"error"];
[e release];
o = [parameters objectForKey: @"code"];
if (nil == o)
{
o = [parameters objectForKey: @"faultCode"];
}
if (nil == o || NO == [o respondsToSelector: @selector(intValue)])
{
[NSException raise: NSGenericException
format: @"Bad/missing error code"];
}
[e setObject: [NSNumber numberWithInt: [o intValue]] forKey: @"code"];
o = [parameters objectForKey: @"message"];
if (nil == o)
{
o = [parameters objectForKey: @"faultString"];
}
if (NO == [o isKindOfClass: NSStringClass])
{
[NSException raise: NSGenericException
format: @"Bad/missing error message"];
}
[e setObject: o forKey: @"message"];
o = [parameters objectForKey: @"data"];
if (nil != o)
{
[e setObject: o forKey: @"data"];
}
}
else
{
if ([order count] == 0)
{
order = [parameters allKeys];
}
c = [order count];
if (c > 0)
{
if (YES == positional)
{
p = [NSMutableArray new];
}
else
{
p = [NSMutableDictionary new];
}
[*container setObject: p forKey: @"params"];
[p release];
for (i = 0; i < c; i++)
{
NSString *k = [order objectAtIndex: i];
if (NO == [k hasPrefix: @"GWSCoder"])
{
id v = [parameters objectForKey: k];
if (v != nil)
{
if (YES == positional)
{
[p addObject: v];
}
else
{
[p setObject: v forKey: k];
}
}
}
}
if (nil == version)
{
/* This is not a JSON-RPC ... make it a plain document.
*/
if ([p count] == 1)
{
/* There was a single argument ...
* we must use it as the whole JSON document.
*/
p = [p lastObject];
}
*container = p;
}
}
}
if (nil == version)
{
return NO; // Not a JSON-RPC
}
return YES; // Built JSON-RPC
}
- (NSData*) buildFaultWithCode: (GWSRPCFaultCode)code andText: (NSString*)text
{
NSDictionary *params;
params = [NSDictionary dictionaryWithObjectsAndKeys:
text, @"faultString",
[NSNumber numberWithInt: code], @"faultCode",
nil];
return [self buildFaultWithParameters: params order: nil];
}
- (NSData*) buildRequest: (NSString*)method
parameters: (NSDictionary*)parameters
order: (NSArray*)order
{
NSMutableString *ms;
id container;
[self reset];
if (NO == [self fault] && [method length] == 0)
{
return nil;
}
ms = [self mutableString];
[ms setString: @""];
if (YES == [self _build: &container parameters: parameters order: order]
&& NO == [self fault])
{
[container setObject: method forKey: @"method"];
}
[self appendObject: container];
return [ms dataUsingEncoding: NSUTF8StringEncoding];
}
- (NSData*) buildResponse: (NSString*)method
parameters: (NSDictionary*)parameters
order: (NSArray*)order;
{
NSMutableString *ms;
id container;
[self reset];
ms = [self mutableString];
[ms setString: @""];
if (YES == [self _build: &container parameters: parameters order: order])
{
if (YES == [container isKindOfClass: NSDictionaryClass])
{
id o;
/* If this is not a fault we must set the 'result' field rather
* than the normal 'params' field.
*/
if (nil == [container objectForKey: @"error"])
{
if (nil == (o = [container objectForKey: @"params"]))
{
o = null;
}
else if (YES == [o isKindOfClass: [NSDictionary class]]
&& 1 == [o count] && nil != [o objectForKey: GWSJSONResultKey])
{
o = [o objectForKey: GWSJSONResultKey];
}
[container setObject: o forKey: @"result"];
}
[container removeObjectForKey: @"params"];
if (ver1 == [self version])
{
if (nil == [container objectForKey: @"error"])
{
[container setObject: null forKey: @"error"];
}
if (nil == [container objectForKey: @"result"])
{
[container setObject: null forKey: @"result"];
}
}
o = [container objectForKey: GWSOrderKey];
if (nil == o)
{
o = [[container allKeys] sortedArrayUsingSelector:
@selector(compare:)];
[container setObject: o forKey: GWSOrderKey];
}
}
}
[self appendObject: container];
return [ms dataUsingEncoding: NSUTF8StringEncoding];
}
- (void) dealloc
{
[_jsonID release];
[super dealloc];
}
- (NSCalendarDate*) decodeDateTimeFrom: (NSString*)source
{
int year;
int month;