0

完全なオブジェクトではない「辞書」を含むオブジェクトを逆シリアル化する方法を見つけようとしています。

たとえば、私たちのアプリには、Mantle を介して JSON からデシリアライズしている JSON オブジェクトがたくさんあります。単純なモデルは次のようになります。

@interface Artist : MTLModel<MTLJSONSerializing>

@property (nonatomic, strong, nonnull)   NSString    *name;
@property (nonatomic, strong, nullable)   NSURL       *image;

@end

コレクションクラスでは、次のようなものがあるかもしれません:

@interface SomeCollection : MTLModel<MTLJSONSerializing>

@property (nonatomic, strong, nonnull)   NSString    *title; 
@property (nonatomic, strong, nonnull)   NSArray<Artist *>    *listOfArtists;

@end

関連.mするものは次のとおりです。

+ (NSValueTransformer *)listOfArtistsJSONTransformer {
    return [MTLJSONAdapter arrayTransformerWithModelClass:[Artist class]];
}

ここではすべてが良いです。

たとえば、JSON が次のようになっているとします。

{
    "title": "my collection with an array",
    "listOfArtists": [
            {
                "name": "Some Artist",
                "image": "http://www.google.com"
            },
            {
                "name": "Another Artist",
                "image": "http://www.artists.com"
            },
            {
                "name": "Jane Painter",
                "image": "http://www.jpainter.com"
            },
    ]
}

オブジェクトは好きなように逆シリアル化されます (listOfArtistsプロパティにはArtist *.

ただし、次のような別のコレクションがある場合、呪文を理解しようとしています。

@interface SomeOtherCollection : MTLModel<MTLJSONSerializing>

@property (nonatomic, strong, nonnull)   NSString    *title; 
@property (nonatomic, strong, nonnull)   NSDictionary<NSString *, Artist *>    *dictionaryOfArtists;

@end

そして、次のような JSON ファイル

{
    "title": "my collection with a dictionary",
    "dictionaryOfArtists": {
            "139380bf-29ef-4cfc-95af-aa00f78f15f6": {
                "name": "Some Artist",
                "image": "http://www.google.com"
            },
            "4cdbc728-13e7-49c8-b45e-32ff0650ca67": {
                "name": "Another Artist",
                "image": "http://www.artists.com"
            },
            "2f2ec6f9-3af1-4789-b5de-399e14902ea8": {
                "name": "Jane Painter",
                "image": "http://www.jpainter.com"
            },
    }
}

listOfArtistsJSONTransformerメソッドはどのようになりますか?

ありがとう。

4

1 に答える 1