2011-11-21 10 views
6

私はPOJOからJSONに変換するためにJackson側をサーバー側で広範囲に使用していましたが、Objective C/iPhone SDK用の同様のライブラリがあるかどうか、またその逆の場合は不思議でした。目標Cは反射を提供するので、Jacksonと同様のものを作ることが可能でなければならない。iPhone用のJackson相当品ですか?

+0

ここに言語の壁がある可能性があるので、明確にしてください。宣言されたプロパティに基づいてオブジェクトをJSONに自動的にシリアル化するコードが必要ですか? – Tommy

+0

はい、私たちのサーバーがJSONエンコードされたレスポンスを送信するので、もう一方の方法も便利です。 –

答えて

4

Jacksonからインスパイアされたコンベンションオーバレイングパターンを使用して、JSONオブジェクトとObjective-Cオブジェクトを変換するGoldenFleeceを試してみることもできます。

+0

これは私にとって遅すぎると私は自己宣伝に気付くが、私はこのライブラリを書くあなたの努力に感謝する。現在の技術水準は痛いものでした。 –

2

new iOS 5 APIsは、JSONの読み書きに優れた機能を提供します。これらは基本的にiOS 4で使用できるTouchJSON libraryの再ハッシュです。例のペイロードからPOCOオブジェクトが生成されることはあまりありませんが、NSDictionaryインスタンスのファサードだけのクラスを作成できます上記の図書館が戻るでしょう。例えば

@interface PBPhoto : NSObject { 
    NSDictionary* data_; 
} 

@property (nonatomic, retain, readonly) NSDictionary *data; 

- (NSString*) photoId; 
- (NSString*) userId; 
- (NSString*) user; 
- (NSString*) title; 
- (id) initWithData:(NSDictionary*)data; 


@end 

実装:

のObjective-Cは、反射年の控えめであってもよい提供するが、多くのものは、低レベルによってのみ露出されることを
#import "PBPhoto.h" 

#define PHOTO_ID @"id" 
#define USER_ID @"user_id" 
#define USER @"user" 
#define TITLE @"title" 

@implementation PBPhoto 
@synthesize data = data_; 

- (id) initWithData:(NSDictionary*)data { 
    if ((self = [super init])) { 
     self.data = data; 
    } 

    return self; 
} 

- (NSString*) photoId { 
    return [super.data objectForKey:PHOTO_ID]; 
} 

- (NSString*) userId { 
    return [self.data objectForKey:USER_ID]; 
} 

- (NSString*) user { 
    return [self.data objectForKey:USER]; 
} 

- (NSString*) title { 
    return [self.data objectForKey:TITLE]; 
} 

- (void) dealloc { 
    [data_ release]; 
    [super dealloc]; 
} 

@end 
+1

すべてのクラスのすべてのプロパティのゲッターを実装する必要がないのは便利です。それが反射が有用な場所です。 –

1

Cのランタイムとは少し混乱しています。

その後、第三者のために自分のすべてをNSJSONSerializationにそれをオフに渡す(または他の文字列を構築し、おそらくスマート事が仲介者としてNSDictionaryを作成することで、あなたは任意のオブジェクトを取得し、JSONにそれを有効にすると仮定すると、ライブラリは、デシリアライズできるという負担のため、かなり重量があります)。

したがって、たとえば:

- (NSDictionary *)dictionaryOfPropertiesForObject:(id)object 
{ 
    // somewhere to store the results 
    NSMutableDictionary *result = [NSMutableDictionary dictionary]; 

    // we'll grab properties for this class and every superclass 
    // other than NSObject 
    Class classOfObject = [object class]; 
    while(![classOfObject isEqual:[NSObject class]]) 
    { 
     // ask the runtime to give us a C array of the properties defined 
     // for this class (which doesn't include those for the superclass) 
     unsigned int numberOfProperties; 
     objc_property_t *properties = 
        class_copyPropertyList(classOfObject, &numberOfProperties); 

     // go through each property in turn... 
     for(
       int propertyNumber = 0; 
       propertyNumber < numberOfProperties; 
       propertyNumber++) 
     { 
      // get the name and convert it to an NSString 
      NSString *nameOfProperty = [NSString stringWithUTF8String: 
            property_getName(properties[propertyNumber])]; 

      // use key-value coding to get the property value 
      id propertyValue = [object valueForKey:nameOfProperty]; 

      // add the value to the dictionary — 
      // we'll want to transmit NULLs, even though an NSDictionary 
      // can't store nils 
      [result 
        setObject:propertyValue ? propertyValue : [NSNull null] 
        forKey:nameOfProperty]; 
     } 

     // we took a copy of the property list, so... 
     free(properties); 

     // we'll want to consider the superclass too 
     classOfObject = [classOfObject superclass]; 
    } 

    // return the dictionary 
    return result; 
} 

次にあなたが返される辞書でNSJSONSerialization+ dataWithJSONObject:options:error:を使用することができます。

逆の言い方をすれば、allKeysvalueForKey:を経由してキーと値を辞書から取得して、setValue:forKey:のキー値コーディングを使用すると思います。

+0

ありがとう、ええこれは私が心に持っていたものですが、誰かが私のために頑張ってくれたことを願っていました。私はあなたの事例を基礎として使って、自分自身で何かを鞭打ちしようと思う。 –

関連する問題