-
Notifications
You must be signed in to change notification settings - Fork 273
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
Comments
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 |
@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 Sorry, I forgot there is a precondition that Entity must be implement Builder interface in order to use for UnknowFieldhander , ig:
|
@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 |
@elucash cool! @Gson.Other is a good idea to consume unknown properties, it's enough for me, I expected it . thanks! |
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! |
This is not implemented yet in |
implemented, will be in next release |
@elucash , got it. thank you very much! |
There is a request that deserialize json put the unknown fields on a specified fields, how I do it ? for example, entity is Contact:
The json string is :
After gson deserialized, the expected Contact Object is(Json format):
I dig into the generated GsonAdaptorsContact.java, found that in method
eachAttribute(JsonReader in, ImmutableDepartment.Builder builder)
, the unknown fields is skip byin.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.
The text was updated successfully, but these errors were encountered: