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

set JsonInclude annotation on proto fields that are wrapped in POJO #85

Open
assafcoh opened this issue Oct 16, 2021 · 2 comments
Open

Comments

@assafcoh
Copy link

assafcoh commented Oct 16, 2021

Hi,
thanks for this very useful library :)

As stated in the official protobuf java guide : "If you want to add richer behavior to a generated class, the best way to do this is to wrap the generated protocol buffer class in an application-specific class"

So i have a MyPojo which has a protobuf field named AccountInfo, see example below:

public class MyPojo {
   @JsonProperty("tenant_id")
   String tenantId;
   @JsonProperty("size")
   Integer size;
   @JsonProperty("account_info")
   private com.examples.protobuf.AccountInfo accountInfo;
}

I would like to exclude the default values in protobuf AccountInfo, but to include the default values of all other members in MyPojo, like "size" member for example.
When building jackson ObjectMapper as in snippet below, all default members are excluded. For example if "size" member is zero (default Integer value) it is not included in the serialized json.

MyPojo myPojo = populateMyPojo(); // simple method to populate MyPojo

objectMapper = new ObjectMapper()
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                .setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
                .setSerializationInclusion(JsonInclude.Include.NON_DEFAULT)
                .registerModule(new ProtobufModule());

String json = objectMapper.writeValueAsString(myPojo),

So, when sereliazing MyPojo, how do i exclude the default members of all proto members (like accountInfo) but include the default values of other non-proto members (like size)?
In other words, how can i remove the JsonInclude settings from the ObjectMapper and configure it only for the ProtobufModule?

@assafcoh assafcoh changed the title Support JsonInclude.Include.NON_DEFAULT for inner fields Support JsonInclude.Include.NON_DEFAULT specifically on ProtobufModule Oct 16, 2021
@assafcoh
Copy link
Author

assafcoh commented Oct 21, 2021

still did not find a solution for this. Would greatly appreciate help with this issue. Thank you

@assafcoh assafcoh changed the title Support JsonInclude.Include.NON_DEFAULT specifically on ProtobufModule set JsonInclude annotation on proto that is wrapped in POJO Oct 21, 2021
@assafcoh assafcoh changed the title set JsonInclude annotation on proto that is wrapped in POJO set JsonInclude annotation on proto fields that are wrapped in POJO Oct 24, 2021
@assafcoh
Copy link
Author

i noticed the line below in MessageSerializer serialize method:

Include include = serializerProvider.getConfig().getDefaultPropertyInclusion().getValueInclusion();

to set JsonInclude.Include.NON_DEFAULT just on protobuf classes i will have to override and change both class ProtobufModule and class MessageSerializer. This is a bad solution, because we will not get future changes made to these classes in jackson-datatype-protobuf

There must be a better solution. Your help is much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant