1

Greetings,

I have JSONObject that contain payment information, today our customer asked for 10 digit number formatted as (1234567890.12), i have parameter total_amount , when i set this parameter value {"total_amount":123456789.23} it shown in server like {"total_amount":1.2345678923E8} , is there anyway that i can make JSONObject hold it as 123456789.23

MaJiD
  • 74
  • 1
  • 8
  • Can you show how you set it as? You might be setting it as a double. Try storing it as toString. – papaya May 08 '18 at 10:54
  • 1
    I think that's the same number. Any JSON parser should be able to handle it, and any application on top of that can format the Double as needed. Unless someone is looking at the JSON directly, it should not make a difference. – Thilo May 08 '18 at 10:56
  • 1
    @SiddarthSreeni The JSON types _number_ and _string_ are completely different types. You cannot simply switch them. Additionally, if a value is a number than treat it as such. – Seelenvirtuose May 08 '18 at 10:58
  • @Seelenvirtuose True. But Biasing is done because exponents have to be signed values in order to be able to represent both tiny and huge values, but two's complement, the usual representation for signed values, would make comparison harder. It shouldn't make a difference in interpreting them for codes. But, pretty formats is what I believe the consumer looks for. – papaya May 08 '18 at 11:00
  • @SiddarthSreeni i tried both, when string is stored as "123456789.23" which is bad for me, and when double is stored as 1.2345678923E8 and that is the main problem. – MaJiD May 08 '18 at 11:07
  • @Thilo am using the JSON in security process first in client and then in the server, so they must be the same – MaJiD May 08 '18 at 11:08
  • @MaJiD I guess: Double.parseDouble(n) would get your number back. – papaya May 08 '18 at 11:09
  • @Seelenvirtuose your comment is very useful, BUT the problem is that i'm using the JSON in security process, so it MUST be the same as the client send to my server. – MaJiD May 08 '18 at 11:11
  • @SiddarthSreeni yes i can get it back, BUT i need it in JSON for security – MaJiD May 08 '18 at 11:13
  • Please explain the "security process" and why it would fail here. That is the part that should probably be fixed. Otherwise, you could dive into the innards of the JSON library you use to stringify the object and somehow bend it to your requirement of avoiding scientific notation. Maybe there is a configuration option for that. What library do you use? – Thilo May 08 '18 at 11:16
  • @Thilo OK, process as follow, the user send JSON contain payment info, and **authentication** header, this header generated using the JSON, so i have to generate **authentication** in server using the same JSON, otherwise it will fail. and **i'm using jettison** – MaJiD May 08 '18 at 11:23
  • Try `DecimalFormat formatter = new DecimalFormat("0.00000000");` Not sure how this would help you. But, give me your views – papaya May 08 '18 at 11:26
  • You could just pass the JSON through as received from the user, then. Store it as an unparsed string. Parse to validate and inspect, but then pass on the original. Otherwise things like whitespace or order of keys might also throw you off. – Thilo May 08 '18 at 11:26
  • https://stackoverflow.com/q/11520781/3295987 have a look – Hemant Patel May 08 '18 at 14:32
  • Thanks guys, i solved the problem by using `BigDecimal.valueOf(double)` so it convert it from 1.2345678923E8 to 123456789.23 – MaJiD May 10 '18 at 10:19

1 Answers1

0

You can have a deserialiser and utilise a fasterxml(com.fasterxml.jackson.databind.annotation.JsonSerialize) annotation

deserialiser class:

    class DecimalJsonSerializer extends JsonSerializer<Double> {

    @Override
    public void serialize(Double value, JsonGenerator jsonGenerator, SerializerProvider provider)
            throws IOException {

        jsonGenerator.writeNumber(String.format("%.1f", value));
    }
}

And the annotation on your field should look like:

@JsonSerialize(using = DecimalJsonSerializer.class)
private Double total_amount;
yabets
  • 21
  • 5