0

The data returned from my application API contains the product price stored as a String for example:

{
   price:"1500"
}

This causes a NumberFormatException because the data type used in the Product model is a BigDecimal. How do I parse this string from the database into a BigDecimal before using it within the application?

I am using Retrofit and GSON, and have tried the solutions here:

NumberFormatException in GSON when converting String to double

Solution 1:

GsonBuilder gb = new GsonBuilder();
    gb.registerTypeAdapter(Float.class, new TypeAdapter<Float>() {

        @Override
        public Float read(JsonReader reader) throws IOException {
            if (reader.peek() == JsonToken.NULL) {
                reader.nextNull();
                return null;
            }
            String stringValue = reader.nextString();
            try {
                Float value = Float.valueOf(stringValue);
                return value;
            } catch (NumberFormatException e) {
                return null;
            }
        }

        @Override
        public void write(JsonWriter writer, Float value) throws IOException {
            if (value == null) {
                writer.nullValue();
                return;
            }
            writer.value(value);
        }

    });

This solution seems correct but I failed to return the parsed value when the exception occurs, instead of returning null.

Solution 2:

.registerTypeAdapter(Double.class, new JsonSerializer<Double>() {
            public JsonElement serialize(Double number, Type type, JsonSerializationContext context) {
                return new JsonPrimitive(Double.valueOf(number));
            }
        })

For this solution, BigDecimal isn't a supported JsonPrimitive datatype.

  • 1
    _How do I parse this string from the database into a BigDecimal before using it within the application?_ you just store it in a string and parse it as BigDecimal – some user Feb 14 '20 at 05:09
  • I understand how to do this. My question is how to use this to parse a json string into a big decimal. –  Feb 14 '20 at 05:16
  • Are you sure you are not trying to parse the json string? Log the string before you parse so that you can u can understand what is being parsed – npk Feb 14 '20 at 05:50
  • The price is a string. My question is how to parse it into a big decimal using either a type adapter or a json serializer –  Feb 14 '20 at 06:00

2 Answers2

0

It's working fine for me

String total=“1500.00”;

BigDecimal sPrice = new BigDecimal(total);
Rohit Soni
  • 1,300
  • 6
  • 12
0

Try this

GsonBuilder gb = new GsonBuilder();
gb.registerTypeAdapter(String.class, new TypeAdapter<String>() {

    @Override
    public String read(JsonReader reader) throws IOException {
        if (reader.peek() == JsonToken.NULL) {
            reader.nextNull();
            return null;
        }
        String stringValue = reader.nextString();
        try {
            BigDecimal value = new BigDecimal(stringValue);
            globalVariable = value;  //globalVariable must be of type BigDecimal OR provide some callback mechanism here
            return stringValue;
        } catch (NumberFormatException e) {
            return null;
        }
    }

    @Override
    public void write(JsonWriter writer, Float value) throws IOException {
        if (value == null) {
            writer.nullValue();
            return;
        }
        writer.value(value);
    }

});
VVB
  • 7,363
  • 7
  • 49
  • 83
  • You cannot return a big decimal where you declared a type adapter for a string –  Feb 15 '20 at 10:53