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

Map numeric data value in Object type to proper data type instead of GSON's default double data type #1252

Closed
madhavjha-git opened this issue Jan 13, 2023 · 3 comments

Comments

@madhavjha-git
Copy link
Contributor

Is your feature request related to a problem? Please describe.
GSON by default maps any numeric value defined in Object type to Double.
Same problem is inherited by Javers as it uses GSON under the hood.

GSON provided the solution for it long back. Javers should adopt it for better results with shadow api.
google/gson#1290

Example:

class TypeValue{
    Integer id;
    Object value;
}

Json:
var jsonStr="{\n" +
" "id":1,\n" +
" "value":9999999999\n" +
" }";

Result without setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)

        Gson gson= new GsonBuilder()
               // .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
                .create();
        var json=gson.fromJson(jsonStr,TypeValue.class);

{
"id": 1,
"value": 9.999999999E9
}

Result with setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)

        Gson gson= new GsonBuilder()
                .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
                .create();
      var json=gson.fromJson(jsonStr,TypeValue.class);

{
"id": 1,
"value": 9999999999
}

Describe the solution you'd like
A clear and concise description of what you want to happen.
Adding this should solve it.

public JsonConverterBuilder() {
    this.gsonBuilder = new GsonBuilder();
    this.gsonBuilder.setExclusionStrategies(new SkipFieldExclusionStrategy());
  this.gsonBuilder.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE);
    registerBuiltInAdapters(Java8TypeAdapters.adapters());
    registerBuiltInAdapters((List)UtilTypeCoreAdapters.adapters());
    registerJsonAdvancedTypeAdapter(new OptionalTypeAdapter());
}

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

@bartoszwalacik
Copy link
Member

Hi @madhavjha-git, feel free to contribute a PR

@madhavjha-git
Copy link
Contributor Author

Hi @bartoszwalacik raised PR #1280 for the same.

@bartoszwalacik
Copy link
Member

bartoszwalacik commented Mar 22, 2023

released in 6.13.0, @madhavjha-git thanks for your high quality contribution

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