如何在restkit 0.20中将NSManagedObject序列化为JSON ?

时间:2022-10-24 15:17:32

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

如何在restkit 0.20中使用反向映射将NSManagedObject序列化为JSON ?

Right now I don't need to post anything anywhere.

现在我不需要在任何地方张贴任何东西。

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

我想手动创建对象MyObjectManaged。设置一些属性,例如:id、name、age。

Map them with existing mapping my mapping to JSON attributes: userid, first_name, age

将它们与现有的映射映射到JSON属性:userid、first_name、age

create and print JSON.

创建和打印JSON。

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

是可能的吗?是的,怎么了?提前谢谢你的答复。

3 个解决方案

#1


7  

I've recently been trying to do the same thing :) I wanted to keep the mappings so that eventually I can hook up to a server, but also reuse them for serializing objects out to a file.

我最近一直在尝试做同样的事情:)我希望保留映射,以便最终能够连接到服务器,但也要重用它们将对象序列化到文件中。

I did this using the inverseMapping and running it through an RKMappingOperation.

我使用inverseMapping来实现这一点,并通过RKMappingOperation运行它。

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

首先从JSON ->核心数据对象设置映射

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

Then use the inverse mapping to map your object instance (e.g. "myObject") to a dictionary:

然后使用反向映射映射来映射对象实例(例如。一个字典“myObject”):

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's no error, you can then serialize the dictionary:

假设没有错误,你可以序列化字典:

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

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

不确定你想用它做什么,但是如果你想把它打印到一个字符串你可以这样做:

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

Hope that helps

希望这有助于

#2


3  

John Martin answer seems to work but I got the problem that NSManagedObject instances with a NSNumber property that is set with

John Martin的回答似乎很有效,但是我遇到了NSManagedObject实例的问题,该实例使用NSNumber属性设置

[NSNumber numberWithBool:boolvalue]

serializes a json as value 1/0 instead of true/false. Our backend could not handle numbers as booleans.

将json序列化为值1/0而不是true/false。我们的后端不能像布尔那样处理数字。

I solved this with using the RestKit built in class: RKObjectParameterization

我使用类中构建的RestKit解决了这个问题:RKObjectParameterization

Using the follow method my NSManagedObjects were properly serialized when there was an NSNumber property that was set as a bool.

使用follow方法,当有一个NSNumber属性被设置为bool时,NSManagedObjects将被正确序列化。

+ (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


0  

You can head over to the RestKit wiki and have a look in the object mapping. In the paragraph "Object Parameterization & Serialization" you'll find the information about the serialization and inverse mapping.

您可以前往RestKit wiki,查看对象映射。在“对象参数化和序列化”一节中,您将找到关于序列化和逆映射的信息。

#1


7  

I've recently been trying to do the same thing :) I wanted to keep the mappings so that eventually I can hook up to a server, but also reuse them for serializing objects out to a file.

我最近一直在尝试做同样的事情:)我希望保留映射,以便最终能够连接到服务器,但也要重用它们将对象序列化到文件中。

I did this using the inverseMapping and running it through an RKMappingOperation.

我使用inverseMapping来实现这一点,并通过RKMappingOperation运行它。

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

首先从JSON ->核心数据对象设置映射

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

Then use the inverse mapping to map your object instance (e.g. "myObject") to a dictionary:

然后使用反向映射映射来映射对象实例(例如。一个字典“myObject”):

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's no error, you can then serialize the dictionary:

假设没有错误,你可以序列化字典:

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

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

不确定你想用它做什么,但是如果你想把它打印到一个字符串你可以这样做:

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

Hope that helps

希望这有助于

#2


3  

John Martin answer seems to work but I got the problem that NSManagedObject instances with a NSNumber property that is set with

John Martin的回答似乎很有效,但是我遇到了NSManagedObject实例的问题,该实例使用NSNumber属性设置

[NSNumber numberWithBool:boolvalue]

serializes a json as value 1/0 instead of true/false. Our backend could not handle numbers as booleans.

将json序列化为值1/0而不是true/false。我们的后端不能像布尔那样处理数字。

I solved this with using the RestKit built in class: RKObjectParameterization

我使用类中构建的RestKit解决了这个问题:RKObjectParameterization

Using the follow method my NSManagedObjects were properly serialized when there was an NSNumber property that was set as a bool.

使用follow方法,当有一个NSNumber属性被设置为bool时,NSManagedObjects将被正确序列化。

+ (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


0  

You can head over to the RestKit wiki and have a look in the object mapping. In the paragraph "Object Parameterization & Serialization" you'll find the information about the serialization and inverse mapping.

您可以前往RestKit wiki,查看对象映射。在“对象参数化和序列化”一节中,您将找到关于序列化和逆映射的信息。