How to serialize NSManagedObject to JSON in restkit 0.20?

How to serialize NSManagedObject to JSON in restkit 0.20 using reverse mapping?

I don't need to post anything now.

I would like to manually create a MyObjectManaged object. Set some attributes, for example: I would, name, age

Match them to my existing mapping to JSON attributes: User id, name, age

create and print JSON.

Is it possible? When, yes, how? Thank you in advance for your reply.

+3


source to share


3 answers


I recently tried to do the same :) I wanted to keep the mappings so that I can eventually connect to the server, but also reuse them to serialize objects to a file.

I did it using inverseMapping and executing it via RKMappingOperation.

First set up your mappings from JSON -> Core Data Object

RKEntityMapping mapping = [RKEntityMapping mappingForEntityForName:@"MyManagedObject" inManagedObjectStore:rkManagedObjectStore];
[self.nodeMapping addAttributeMappingsFromDictionary:@{
    @"userid": @"id",
    @"first_name": @"name",
    @"age": @"age"
}];

      

Then use reverse mapping to map an object instance (such as "myObject") to a dictionary:

NSMutableDictionary *jsonDict = [NSMutableDictionary dictionary];
RKObjectMappingOperationDataSource *dataSource = [RKObjectMappingOperationDataSource new];
RKMappingOperation *operation = [[RKMappingOperation alloc] initWithSourceObject:myObject 
                                                               destinationObject:jsonDict
                                                                         mapping:[mapping inverseMapping]];
operation.dataSource = dataSource;

NSError *error = nil;
[operation performMapping:&error];

      



Assuming there is no error, you can serialize the dictionary:

NSData *data = [RKMIMETypeSerialization dataFromObject:jsonDict 
                                              MIMEType:RKMIMETypeJSON 
                                                 error:&error];

      

Not sure what you wanted to do with it, but if you wanted to print it to a string you could do:

NSString *jsonString = [[NSString alloc] initWithData:data 
                                             encoding:NSUTF8StringEncoding]

      

Hope it helps

+7


source


John Martin's answer seems to work, but I'm having a problem with NSManagedObject instances with NSNumber property set using

[NSNumber numberWithBool:boolvalue]

      

serializes json value as 1/0 instead of true / false. Our backend couldn't handle numbers as booleans.



I solved it with built-in RestKit class: RKObjectParameterization

Using the following method, my NSManagedObjects were serialized correctly when the NSNumber property existed and was set to bool.

+ (NSString *)getJsonObjectWithDescriptor:(RKRequestDescriptor *)requestDescriptor objectToParse:(id)objectToParse {
    NSError *error = nil;
    NSDictionary *jsonDict = [RKObjectParameterization parametersWithObject:objectToParse requestDescriptor:requestDescriptor error:&error];
    NSData *data = [RKMIMETypeSerialization dataFromObject:jsonDict
                                                  MIMEType:RKMIMETypeJSON
                                                     error:&error];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

      

+3


source


You can go to the RestKit wiki and look into object mapping . See the section on Object Parameterization and Serialization for information on serialization and reverse mapping.

0


source







All Articles