Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson force use int instead of double

Hi can i configure gson that he use int instead of double got data like:

{fn: chat, data: {roomId: 1, text: "Some brabble"}}

I got a PacketClass to deserialize it to:

public class DataPacket {
  private String fn;
  private Object p; // will be a StringMap
}

and decode like:

DataPacket pkg = gson.fromJson(message, DataPacket.class);

for better typehandling i got a data pojo for the DataPacket.p field for example:

public class ChatPacket {
  public int roomId;
  public String message;
  public String from;
  // getter & setter
}

to parse the data to the packet i use BeanMaps like (i removed error handling etc):

public Object getData(Class<?> pojoClass) {
  pojo = pojoClass.newInstance();
  BeanMap b = new BeanMap();
  b.setBean(pojo);
  b.putAll(this.p);
  pojo = b.getBean();
}

The problem is now that gson make the roomId: 1 to a double 1.0 in the StringMap and the beanmap try to parse it to an integer and throws a NumberFormatException, anyone got an idea how to fix this?

Thank You!

like image 676
Kani Avatar asked Aug 22 '12 06:08

Kani


People also ask

Why does Gson parse an integer as a double?

It sees all kind of numbers as a single type. That the numbers are parsed as a Double is an implementation detail of the Gson library. When it encounters a JSON number, it defaults to parsing it as a Double .

How can you prevent Gson from expressing integers as floats?

One option is to define a custom JsonDeserializer, however better would be to not use a HashMap (and definitely don't use Hashtable!) and instead give Gson more information about the type of data it's expecting. Show activity on this post. Show activity on this post. Show activity on this post.

Does Gson ignore extra fields?

3. Deserialize JSON With Extra Unknown Fields to Object. As you can see, Gson will ignore the unknown fields and simply match the fields that it's able to.

What is Gson toJson in Java?

Introduction. Gson is the main actor class of Google Gson library. It provides functionalities to convert Java objects to matching JSON constructs and vice versa. Gson is first constructed using GsonBuilder and then toJson(Object) or fromJson(String, Class) methods are used to read/write JSON constructs.


1 Answers

I think you could simply change your DataPacket class to

public class DataPacket {
  private String fn;
  private ChatPacket p; // will be a StringMap
}

(replace Object by ChatPacket) - if that suits into the rest of your application. Gson will then recognize the data types of the ChatPacket class and will trait the "1" as int.

EDIT (as you can't use full formatting features in comments :-( )

You can use a combination of Gson and it's low level helper JsonParser like this:

String testStr = "{fn: chat, data: {roomId: 1, message: \"Some brabble\"}}";
DataPacket dp = new Gson().fromJson(testStr, DataPacket.class); 
JsonParser parser = new JsonParser(); 
JsonElement e = parser.parse(testStr); 
ChatPacket cp = new Gson().fromJson(e.getAsJsonObject().getAsJsonObject("data"), ChatPacket.class);
like image 108
Steffen Blass Avatar answered Oct 26 '22 09:10

Steffen Blass