Objective-C序列化复杂对象列表 [英] Objective-C serialize list of complex objects

查看:95
本文介绍了Objective-C序列化复杂对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的班级列表:

I have a list of classes looking like this:

@interface AISlideItem: NSObject
{
    NSString*  PlaceHolderName;
    NSUInteger PlaceHolderID;
}
@property (nonatomic, strong) NSString* PlaceHolderName;
@property (nonatomic) NSUInteger PlaceHolderID;

@end

@interface AITitleSlideItem : AISlideItem
{
    NSString* Title;
}
@property (nonatomic, strong) NSString* Title;
@end

@interface AIParagraphSlideItem : AISlideItem
{
    NSMutableArray* Paragraphs;
}
@property (nonatomic, strong) NSMutableArray* Paragraphs;
@end

@interface AITextSlideItem : AISlideItem
{
    NSString* Text;
}
@property (nonatomic, strong) NSString* Text;
@end

@interface AIPictureSlideItem : AISlideItem
{
    NSMutableData* Picture;
}
@property (nonatomic, strong) NSMutableData* Picture;
@end

@interface AISlideContent : NSObject
{
    NSString*       LayoutName;
    NSMutableArray* Items;
}
@property (nonatomic,strong) NSString* LayoutName;
@property (nonatomic,strong) NSMutableArray* Items;
@end

@interface ContentParaghraph : NSObject
{
    NSInteger Level;
    NSString* Text;
}
@property (nonatomic) NSInteger Level;
@property (nonatomic, strong) NSString* Text;
@end

我想将包含AISlideContent对象的NSMutableArray序列化为json. json中每个项目的名称应与变量的名称相同.

I want to serialize into json an NSMutableArray holding AISlideContent objects. Every item's name in the json should be the same as the name of the variable.

我该怎么做?

这是一个示例JSON:

This is an example JSON:

{
  d: [
     {
          Items: [
          {
              placeHolderName: "1",
              Title: "Title Slide"
          },
          {
              placeHolderName: "2",
              Paragraphs: [
                 {
                     Level: 0,
                     Text: "Title content"
                 }
              ]
          }
       ],
       LayoutName: "Title"
     },
     {
         Items: [
         {
              placeHolderName: "1",
              Title: "Slide 2"
     },
     {
              placeHolderName: "2",
              Paragraphs: [
              {
                 Level: 0,
                 Text: "Line 1"
              },
              {
                 Level: 0,
                 Text: "Line 2"
              },
              {
                 Level: 1,
                 Text: "Line 2 indented"
              },
              {
                 Level: 2,
                 Text: "Line 3 more indented"
              },
              {
                 Level: 0,
                 Text: "Line 4"
              }
              ]
     }
      ],
      LayoutName: "Title and Content"
     },
     {
           Items: [
           {
                placeHolderName: "1",
                Title: "Slide 3"
           },
           {
                placeHolderName: "3",
                Picture: [

                ]
           }
       ],
       LayoutName: "Picture above Caption"
      }
      ]
}

p.s.我已启用ARC

p.s. I have ARC enabled

推荐答案

Objective-C JSON库定义了Objective-C类与相应JSON元素之间的映射.例如,将NSArray映射到JSON数组,将NSDictionary映射到JSON对象,将NSString映射到JSON String等.

Objective-C JSON libraries define the mapping between Objective-C classes and the corresponding JSON elements. For example, a NSArray will be mapped to a JSON Array, a NSDictionary will be mapped to a JSON Object, a NSString to a JSON String and so force.

JSON序列化程序的实现将遍历对象层次结构,并使用内省法确定Objective-C类的类型,或者在内部为那些可以序列化的Objective-C类实现一个Category,然后调用/调用适当的序列化以便将字符写入流中的方法.

An implementation of a JSON serializer will walk the object hierarchy and either figure the Objective-C class type using introspection, or internally implement a Category for those Objective-C classes which can be serialized, and then invoking/calling the appropriate serialization method in order to write the characters into a stream.

现在,拥有一个包含具有任意类的对象的对象层次结构并尝试运行序列化程序可能会失败,因为它不知道例如如何序列化NSDate.

Now, having an object hierarchy containing objects with arbitrary classes and trying to run the serializer will likely fail, since it doesn't know how to serialize a NSDate for example.

对此问题的一种可能的解决方案是看一下您正在使用的JSON库的文档,并弄清楚是否以及如何实现这一点. AFAIK,可可的内置NSJSONSerialization无法做到这一点.

A possible solution to this problem is to take a look at the documentation of the JSON library you are using and figure out if and how this can be accomplished. AFAIK, Cocoa's built-in NSJSONSerialization cannot do this.

最近的 JSONKit 可能对您有用,它使用了回调机制.不过,我看起来并不太深.

Recent JSONKit may work for you, which uses a callback mechanism. I didn't look too deep into it, though.

JPJson库使用类别方法.您只需为自定义类定义一个类别,然后为要序列化(例如NSDate)并实现协议的内置Cocoa类定义一个类别.一旦实现(在JPJson中非常容易),即使在深度嵌套的层次结构中,此类的对象也可以正确序列化,并维护其他序列化选项,例如漂亮打印",Unicode编码,字符串转义等.

JPJson library on the other hand uses the Category approach. You simply define a Category for your custom class respectively a built-in Cocoa class which you want to serialize (e.g. NSDate) and implement a protocol. Once implemented (which is quite easy in JPJson) objects of this class will be correctly serialized even within a deeply nested hierarchy, maintaining other serialization options - like "pretty printing", Unicode encoding, string escaping, etc.

查看您的示例,使用JPJson似乎很容易实现. JPJson软件包位于GitHub上,并且还有一个示例,显示了如何序列化这些自定义类.

Looking at your example, it seems quite easy to implement with JPJson. The JPJson package is on GitHub and also has a sample which shows how to serialize those custom classes.

您可以查看 https://github.com/couchdeveloper/JPJson ,尤其是Sample6

You may take a look at https://github.com/couchdeveloper/JPJson and especially Sample6.

这篇关于Objective-C序列化复杂对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆