0

I'm trying to figure out how to deserialize an object that contains a "dictionary" that is not a full object.

For example, in our app, we have a bunch of JSON objects that we're deserializing from JSON via Mantle. A simple model might look like:

@interface Artist : MTLModel<MTLJSONSerializing>

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

@end

in a collection class, we might have something like this:

@interface SomeCollection : MTLModel<MTLJSONSerializing>

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

@end

and an associated .m would have:

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

Everything is good here.

For example, if the JSON looks like:

{
    "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"
            },
    ]
}

The object deserializes just as we like (where the listOfArtists property contains an array of Artist *.

However, I'm trying to figure out the incantation if we have a different collection that has:

@interface SomeOtherCollection : MTLModel<MTLJSONSerializing>

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

@end

and a JSON file that might look like

{
    "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"
            },
    }
}

What would the listOfArtistsJSONTransformer method look like?

Thanks.

SuperDuperTango
  • 1,398
  • 1
  • 14
  • 32

1 Answers1

0

I don't know much (anything) about Mantle, but it looks like you can use...

+ (NSValueTransformer *)assigneeJSONTransformer {
    return [MTLJSONAdapter dictionaryTransformerWithModelClass:[Artist class]];
}
danh
  • 62,181
  • 10
  • 95
  • 136
  • Unfortunately, I haven't been able to make `dictionaryTransformerWithModelClass` work. That call is normally used to create a single object from a JSON dictionary, not make a dictionary with "random" (of a particular type) keys and values. – SuperDuperTango Mar 17 '17 at 03:47