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

json string to map,int Type conversion Double #1823

Closed
lsm1998 opened this issue Nov 23, 2020 · 4 comments
Closed

json string to map,int Type conversion Double #1823

lsm1998 opened this issue Nov 23, 2020 · 4 comments

Comments

@lsm1998
Copy link

lsm1998 commented Nov 23, 2020

The cases are as follows:

`
**package test;

import com.google.gson.Gson;
import org.junit.Test;

import java.util.Map;

public class MyTest
{
private Integer id;
private String name;
private float number;

@Test
public void test()
{
    Gson gson=new Gson();

    MyTest test=new MyTest();
    test.number = 3.14f;
    test.id = 10;
    test.name = "lsm";
    System.out.println(gson.toJson(test));

    String str="{\"id\":10,\"name\":\"lsm\",\"number\":3.14}";
    Map map = gson.fromJson(str, Map.class);
    map.forEach((k,v)-> System.out.println(k+" type is"+v.getClass()));
}

}**
`

id type is int,but the print result is Double

@Marcono1234
Copy link
Collaborator

Relates to #968 and #1290

@komminen
Copy link

Hi, we will volunteer to work on this issue in October/November. For the project maintainer, please let us know if this is okay and if you want it fixed.

@Marcono1234
Copy link
Collaborator

I think this is partially resolved now with #1290 being merged. In the next version you can use ToNumberPolicy.LONG_OR_DOUBLE; this should give a long instead of a double as result.
The reporter wants an int, that would require adding a ToNumberPolicy.INT_OR_LONG_OR_DOUBLE (or similar), but I am not sure if that is worth it; the main issue #1290 tried to solve was that an integral JSON number was converted to a double.

Without more context regarding the use case of @lsm1998, the proper solution would be to use gson.fromJson(str, MyTest.class) instead of deserializing it as Map.

@lsm1998, could you please provide a bit more information about why you want to deserialize the data as Map instead of MyTest?


Note that I am not a member of this project.

@Marcono1234
Copy link
Collaborator

Closing because this is most likely solved (at least to some extent) with the introduction of ToNumberPolicy and the author did not provide more information about their use case.

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

3 participants