Skip to content

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

@madhavjha-git

Description

@madhavjha-git

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions