3

Using a rest service, we are transfering values using JSON.

At some point, we need to decide, wheter the incoming value was a long or a double. While

Double d = 17.0;

System.out.println("toString(): " + d.toString());

will result in

toString(): 17.0

the zero and the point gets removed, when wrapping the value inside a JSONObject:

JSONObject jo = new JSONObject();
jo.put("myDouble", d);
System.out.println(jo.toString());

Output:

{"myDouble":17}

The rest-Service has basically an EAV-Store, so it should not determine the type by the ValueNAME given. I testet 2 JSON-Implementations, both have the same behaviour.

can i somehow achieve, that the decimal value is appended in the JSONObject, without having to create my own implementation for it?

dognose
  • 20,360
  • 9
  • 61
  • 107
  • 1
    And you can't store d.toString() ? jo.put("myDouble", d.toString()); – gaepi May 22 '13 at 13:58
  • @gaepi: doh, youre right! :) this works for my purpose, but ofc. is just a workaround. (Then ofc. the object stored is not of Type Double, but a String, which can be bad, if you continue working with the json object or do some maths, etc. – dognose May 22 '13 at 14:09
  • 1
    Use a different JSON library (JSON Simple, Gson, Jackson). All the mentioned preserve at leave one decimal place of precision when serializing floating point numbers. – Perception May 22 '13 at 14:44
  • @Perception Perfect, `JSON Simple` does the trick. (tested activiti and primefaces JSONObject prior) – dognose May 23 '13 at 10:21

1 Answers1

0

Kudos should go to Perception for the comment:

Use a different JSON library (JSON Simple, Gson, Jackson). All the mentioned preserve at leave one decimal place of precision when serializing floating point numbers.

Used JSON Simple, which worked as expected.

dognose
  • 20,360
  • 9
  • 61
  • 107