domino-jackson
domino-jackson copied to clipboard
ClassCastException on primitive type deserialization
Below simple test case which fails with java.lang.ClassCastException
/*
* Copyright 2013 Nicolas Morel
* Copyright 2020 Stanislav Spiridonov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dominokit.jacksonapt.client.deser.array;
import org.dominokit.jacksonapt.JsonDeserializer;
import org.dominokit.jacksonapt.client.deser.AbstractJsonDeserializerTest;
import org.dominokit.jacksonapt.deser.array.cast.PrimitiveDoubleArrayJsonDeserializer;
/**
* @author Stanislav Spiridonov
*/
@SuppressWarnings("nls")
public class DoubleArrayJsonDeserializerTest extends AbstractJsonDeserializerTest<double[]> {
private static final double DELTA = 0.00000000000001;
@Override
protected JsonDeserializer<double[]> createDeserializer() {
return PrimitiveDoubleArrayJsonDeserializer.getInstance();
}
@Override
public void testDeserializeValue() {
double[] orig = new double[]{-100, -0.5365623643346, 0, 0.5365623643346, 100};
double[] deserialize = deserialize("[-100, -0.5365623643346, 0, 0.5365623643346, 100]");
assertEquals(orig[0], deserialize[0], DELTA);
assertEquals(orig[1], deserialize[1], DELTA);
assertEquals(orig[2], deserialize[2], DELTA);
assertEquals(orig[3], deserialize[3], DELTA);
assertEquals(orig[4], deserialize[4], DELTA);
}
public void testDeserializeValueEmptyArray() {
double[] deserialize = deserialize("[]");
assertTrue(deserialize.length == 0);
}
}
exception is
java.lang.ClassCastException
at java.lang.Throwable.Throwable(Throwable.java:66)
at java.lang.Exception.Exception(Exception.java:29)
at java.lang.RuntimeException.RuntimeException(RuntimeException.java:29)
at java.lang.ClassCastException.ClassCastException(ClassCastException.java:27)
at javaemul.internal.InternalPreconditions.checkCriticalType(InternalPreconditions.java:154)
at com.google.gwt.lang.Cast.castToDouble(InternalPreconditions.java:138)
at org.dominokit.jacksonapt.deser.array.cast.BaseJsNumberArrayReader.$readNumberArray(BaseJsNumberArrayReader.java:25)
at org.dominokit.jacksonapt.deser.array.AbstractArrayJsonDeserializer.doDeserialize(JsDoubleArrayReader.java:19)
at org.dominokit.jacksonapt.JsonDeserializer.$deserialize(JsonDeserializer.java:57)
at org.dominokit.jacksonapt.client.deser.array.DoubleArrayJsonDeserializerTest.testDeserializeValue(JsonDeserializer.java:40)
at Unknown.anonymous(GWTTestMetadataImpl.java:8)
at com.google.gwt.junit.client.impl.GWTTestAccessor.$invoke(GWTTestAccessor.java:35)
at com.google.gwt.junit.client.impl.GWTRunner.$executeTestMethod(GWTRunner.java:226)
at com.google.gwt.junit.client.GWTTestCase.$doRunTest(GWTTestCase.java:157)
at com.google.gwt.junit.client.GWTTestCase.$runBare(TestCase.java:59)
at com.google.gwt.junit.client.GWTTestCase.$__doRunTest(GWTTestCase.java:115)
at com.google.gwt.junit.client.impl.GWTRunner.$runTest(GWTRunner.java:302)
at com.google.gwt.junit.client.impl.GWTRunner.$doRunTest(GWTRunner.java:235)
at com.google.gwt.junit.client.impl.GWTRunner$TestBlockListener.$onSuccess(GWTRunner.java:106)
at com.google.gwt.junit.client.impl.GWTRunner$InitialResponseListener.$onSuccess(GWTRunner.java:61)
at com.google.gwt.junit.client.impl.GWTRunner$InitialResponseListener.onSuccess(GWTRunner.java:59)
at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.$onResponseReceived(RequestCallbackAdapter.java:232)
at com.google.gwt.http.client.Request.$fireOnResponseReceived(Request.java:250)
at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)
at Unknown.anonymous(XMLHttpRequest.java:329)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java:309)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:368)
at Unknown.anonymous(Impl.java:78)
There is PR to fix it: #48
There are two errors:
- The first one is in
BaseJsNumberArrayReader
-nextInt
crash on double in the array. It may be fixed with a simple change tonextDouble
, but I preferred to leavenextInt
for Integer and Short readers because they provide the more understandable error message if JSON array contains wrong formated INT values. - The second one is related to https://github.com/google/elemental2/issues/28. I am not sure that I chose the best way to convert JsArray<JsNumber> to Java primitive array, so please check it.