0

I am getting below error,

com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: Infinite or NaN

When I am giving Json request: {"sendAmt" : ""} and storing that json object to some other Object using gson.fromJson(inputStreamReader,msgClass)

Note that sendAmt is a BigDecimal type.

I tried gson builder using registering TypeAdapters but I could not get that solved.

Hema
  • 988
  • 1
  • 16
  • 38

1 Answers1

0

I have tried gson builder using TypeAdapter for BigDecimal data type. and it is working fine.

Here is my TypeAdapter code:

builder.registerTypeAdapter(BigDecimal.class, new TypeAdapter<BigDecimal>() {
  @Override
  public BigDecimal read(JsonReader reader) throws IOException {
    JsonToken token = reader.peek();
    if (token == JsonToken.STRING) {
      String stringNum = reader.nextString();
      if (stringNum == null || stringNum.isEmpty()) {
        return null;
      } else {
        return new BigDecimal(stringNum);
      }
    } else if(token == JsonToken.NUMBER) {
      return new BigDecimal(reader.nextInt());
    } else {
      reader.skipValue();
      return null;
    }
  }

  @Override
  public void write(JsonWriter writer, BigDecimal num) throws IOException {
  }
});
Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45