Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The generated gson type adaptor deserialize json with unknown fields #764

Closed
wolfjiang opened this issue Apr 8, 2018 · 8 comments
Closed

Comments

@wolfjiang
Copy link

wolfjiang commented Apr 8, 2018

There is a request that deserialize json put the unknown fields on a specified fields, how I do it ? for example, entity is Contact:

@Gson.TypeAdapters
@Value.Immutable
public Contact {
    Long getId();
    String getName();
    JsonObject getOtherFields();
}

The json string is :

{
    "id": 1,
    "name": "my name",
    "custField1": "cust string field",
   "custField2": 2
}

After gson deserialized, the expected Contact Object is(Json format):

    {
        "id": 1,
       "name": "my nqme",
      "otherFields": {
           "custField1": "cust string field",
           "custField2": 2
      }
    }

I dig into the generated GsonAdaptorsContact.java, found that in method eachAttribute(JsonReader in, ImmutableDepartment.Builder builder) , the unknown fields is skip by in.skipValue();. is there a configable feature than put unknows fields to a specified fields, or give me a unknown fields handler to do it myself?

Sorry for my poor Englisth, any advice is welcome, thanks.

@elucash
Copy link
Member

elucash commented Apr 8, 2018

Thank you for raising this! I thought that something like that should be supported, but it seems that it is not implemented for Gson. For Jackson it is supported using @JsonAnyGetter, but not for Gson. I can think of creating a custom adapter to do this, but this would skip any advantages of using auto-generated type adapters. I'm marking this as a feature request.

@elucash elucash added this to the 2.6 milestone Apr 8, 2018
@wolfjiang
Copy link
Author

wolfjiang commented Apr 8, 2018

@elucash thanks for reply so soon :d, I expected the auto-generated type adapter have a parameter, ig. @Gson.TypeAdapters(unknowFiledHander=UnknowFieldhander.class), and UnknowFieldhander implemt a interface with method handleUnkownField(Builder builder, String fieldName, Object fieldValue) , and if annotation with unknowFiledHander then auto-generate code new UnknowFieldhander().handleUnkownField(...) replace orginal code in.skipValue() in eachAttribute method. so we can also using auto generated type adaptor other than custom adaptor.

Sorry, I forgot there is a precondition that Entity must be implement Builder interface in order to use for UnknowFieldhander , ig:

@Gson.TypeAdapters
@Value.Immutable
public Contact {
    Long getId();
    String getName();
    JsonObject getOtherFields();
    public static interface Builder {}
}

@elucash
Copy link
Member

elucash commented Apr 8, 2018

@wolfjiang having worked with smth similar, I think it will be somewhat problematic to introduce fine-grained API to consume unknown properties, this would require handling stream, managing state and then try to apply these fields to immutable object's builder. A more tractable solution would be to discover special catch-all attributes for unknown fields and generate code in adapter to populate those. To make things clear it would look like this:

@Value.Immutable interface Obj {
  int knownAttribute();
  String andThisOne();
  @Gson.Other Map<String, Object> allTheRestAttributes();
 // this could be `com.google.gson.JsonObject` or map as shown
}

But if a structure of mapping is completely custom/special, I would suggest to just create custom Gson TypeAdapter to handle it instead of introducing complex binding rules.

@wolfjiang
Copy link
Author

@elucash cool! @Gson.Other is a good idea to consume unknown properties, it's enough for me, I expected it . thanks!

@wolfjiang
Copy link
Author

Hello @elucash, glade to see that 2.6.0-alpha3 is available , but i didn't found how to handle unknown fields in alpha3, can you give me some idea? Thanks!

@elucash
Copy link
Member

elucash commented May 17, 2018

This is not implemented yet in 2.6.0-alpha3, will be in the next milestone, currently working on completing issues for the next release, stay tuned )

@elucash
Copy link
Member

elucash commented May 19, 2018

implemented, will be in next release

@wolfjiang
Copy link
Author

@elucash , got it. thank you very much!

@elucash elucash closed this as completed May 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants