-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGRLJSONValueTransformer.m
More file actions
72 lines (56 loc) · 1.22 KB
/
GRLJSONValueTransformer.m
File metadata and controls
72 lines (56 loc) · 1.22 KB
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
//
// GRLJSONValueTransformer.m
// GRLJSON
//
// Created by Måns Severin on 2010-05-11.
// Copyright 2010 Graphiclife. All rights reserved.
//
#import "GRLJSONValueTransformer.h"
#import "GRLJSON.h"
@implementation GRLJSONValueTransformer
+ (void)initialize
{
[NSValueTransformer setValueTransformer:[[[GRLJSONValueTransformer alloc] init] autorelease] forName:@"GRLJSONValueTransformer"];
}
+ (Class)transformedValueClass
{
return [NSObject class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (id)transformedValue:(id)value
{
if ( value == nil )
return nil;
NSData *result = nil;
if ( [value conformsToProtocol:@protocol(GRLJSONCoding)] )
{
result = [GRLJSON serializeObject:value];
}
else if ( [value isKindOfClass:[NSDictionary class]] )
{
result = [GRLJSON serializeDictionary:value];
}
else if ( [value isKindOfClass:[NSArray class]] )
{
result = [GRLJSON serializeArray:value];
}
return result;
}
- (id)reverseTransformedValue:(id)value
{
if ( value == nil )
return nil;
if ( ![value isKindOfClass:[NSData class]] )
return nil;
NSError *error = nil;
id json;
if ( (json = [GRLJSON deserializeData:value error:&error]) == nil )
{
return nil;
}
return json;
}
@end