moshi-lazy-adapters icon indicating copy to clipboard operation
moshi-lazy-adapters copied to clipboard

Conflicting fields

Open adrienbrunJP opened this issue 6 years ago • 1 comments

Hi,

I used you example in LazyAdaptersRetrofitTest but ended up with the following issue: if 'item' or 'item2' in the test "unwrapNestedJsonAdapter" contain more than one element, then in the "Nested" class you have to do something like:

 @Json(name = "item") @Wrapped(path = "foo1") String foo1;
 @Json(name = "item") @Wrapped(path = "foo2") String foo2;

But then you get an exception from Moshi saying there are conflicting fields because of the 2 "item". Am I missing something ?

adrienbrunJP avatar Jul 13 '18 14:07 adrienbrunJP

Sorry for the late reply. Somehow I missed this in my notifications. This will not work since moshi consumes the json field as soon as it's parsed.

You can probably work around this, by creating your own WrappedJsonAdapter that will leverage from this https://github.com/square/moshi/blob/master/moshi/src/main/java/com/squareup/moshi/JsonReader.java#L420. The idea would be to first read the full json that contains the field item with readJsonValue() (essentially buffering everything in a map) and then parsing it twice. I didn't try it my self, and not 100% sure if that will work, but it may work.

serj-lotutovici avatar Aug 01 '18 08:08 serj-lotutovici