My favorites | Sign in
Project Logo
                
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
//
// Proxy.m
// JSON
//
// Created by Stig Brautaset on 25/05/2009.
// Copyright 2009 Stig Brautaset. All rights reserved.
//

#import "ProxyTest.h"
#import "JSON.h"

@interface True : NSObject
@end

@implementation True
- (id)jsonRepresentationProxy {
return [NSNumber numberWithBool:YES];
}
@end

@interface False : NSObject
@end

@implementation False
- (id)jsonRepresentationProxy {
return [NSNumber numberWithBool:NO];
}
@end

@interface Bool : NSObject
@end

@implementation Bool
- (id)jsonRepresentationProxy {
return [NSArray arrayWithObjects:[True new], [False new], nil];
}
@end

@implementation NSDate (Private)
- (id)jsonRepresentationProxy {
return [self description];
}
@end



@implementation ProxyTest

- (void)setUp {
writer = [SBJsonWriter new];
}

- (void)testUnsupportedWithoutProxy {
STAssertNil([writer stringWithObject:[NSArray arrayWithObject:[NSObject new]]], nil);
STAssertEquals([[writer.errorTrace objectAtIndex:0] code], (NSInteger)EUNSUPPORTED, nil);
}

- (void)testUnsupportedWithProxy {
STAssertEqualObjects([writer stringWithObject:[NSArray arrayWithObject:[True new]]], @"[true]", nil);
}

- (void)testUnsupportedWithNestedProxy {
STAssertEqualObjects([writer stringWithObject:[NSArray arrayWithObject:[Bool new]]], @"[[true,false]]", nil);
}

- (void)testUnsupportedWithProxyAsCategory {
STAssertNotNil([writer stringWithObject:[NSArray arrayWithObject:[NSDate date]]], nil);
}

@end
Show details Hide details

Change log

r515 by sbrautaset on May 25, 2009   Diff
Test that it works with existing
foundation classes too, by implementing
the jsonRepresentationProxy as a category.
Go to: 
Sign in to write a code review

Older revisions

r514 by sbrautaset on May 25, 2009   Diff
fix copyright
r513 by sbrautaset on May 25, 2009   Diff
Implement a simple way of getting a
json representation from your custom
objects.
All revisions of this file

File info

Size: 1464 bytes, 70 lines
Hosted by Google Code