Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException on primitive type deserialization #46

Open
foal opened this issue Jan 3, 2021 · 2 comments
Open

ClassCastException on primitive type deserialization #46

foal opened this issue Jan 3, 2021 · 2 comments
Assignees

Comments

@foal
Copy link
Contributor

foal commented Jan 3, 2021

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)
@foal
Copy link
Contributor Author

foal commented Jan 3, 2021

There is PR to fix it: #48

@foal
Copy link
Contributor Author

foal commented Jan 3, 2021

There are two errors:

  1. The first one is in BaseJsNumberArrayReader - nextInt crash on double in the array. It may be fixed with a simple change to nextDouble, but I preferred to leave nextInt for Integer and Short readers because they provide the more understandable error message if JSON array contains wrong formated INT values.
  2. The second one is related to JsArray<String>.slice() always causes ClassCastException google/elemental2#28. I am not sure that I chose the best way to convert JsArray to Java primitive array, so please check it.

@vegegoku vegegoku self-assigned this Jan 4, 2021
@vegegoku vegegoku added this to the 1.0 milestone Jan 4, 2021
@vegegoku vegegoku removed this from the 1.0 milestone Nov 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants