Typically this code is called with an instance JsonUtf8Reader and everything works fine in that case. In the case of the failing test in this PR, it's called with an instance of JsonValueReader instead. In JsonValueReader.nextLong()it essentially casts a double down to a long instead of throwing an exception.
I'm happy to implement a solution but would appreciate some guidance. The dump method could be changed to something like this:
case NUMBER:
Double doubleValue = reader.nextDouble();
if (Math.floor(doubleValue) == doubleValue) {
writer.value((long)doubleValue);
} else {
writer.value(doubleValue);
}
break;
I think it would also be open to the thought that JsonValueReader should throw an exception if you call nextLong() when the value has a fractional part. If that is the preferred path, I can create an issue in the main moshi repo.
Thank you for moshi-jsonapi! Your work on it is much appreciated by the team at Yapp!
Currently just a reproduction in a failing test.
The cause of of this issue is in the
dump(JsonReader reader, JsonWriter writer)
method inMoshiHelper
Typically this code is called with an instance
JsonUtf8Reader
and everything works fine in that case. In the case of the failing test in this PR, it's called with an instance ofJsonValueReader
instead. InJsonValueReader.nextLong()
it essentially casts a double down to a long instead of throwing an exception.I'm happy to implement a solution but would appreciate some guidance. The
dump
method could be changed to something like this:I think it would also be open to the thought that
JsonValueReader
should throw an exception if you callnextLong()
when the value has a fractional part. If that is the preferred path, I can create an issue in the main moshi repo.Thank you for moshi-jsonapi! Your work on it is much appreciated by the team at Yapp!