Moshi: Ran into "JsonDataException: Expected a name but was NULL" serializing simple object

Created on 6 Apr 2017  Â·  2Comments  Â·  Source: square/moshi

Simply trying to serializing from JSON string into a object that has Date as well as BigDecimal. Created my own DateAdapter and BigDecimalAdapter, both of them should be able to handle null. But when run the test below, I got a JsonDataException: Expected a name but was NULL.

Using: java 1.8, Moshi 1.4.0

import com.squareup.moshi.*;
import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;


public class MoshiJsonAdaptorTest {

    String json = "{\"date\":1342396800000,\"value\":null}";

    private static final Moshi MOSHI = new Moshi.Builder().
            add(Date.class, new TimeStampDateAdapter()).
            add(BigDecimal.class, new BigDecimalAdapter()).build();

    private static final JsonAdapter<TestClass> TEST_CLASS_JSON_ADAPTER =
            MOSHI.adapter(TestClass.class);

    private static class TestClass {
        private Date date;
        private BigDecimal value;

        public TestClass() {
        }

        public Date getDate() {
            return date;
        }

        public void setDate(Date date) {
            this.date = date;
        }

        public BigDecimal getValue() {
            return value;
        }

        public void setValue(BigDecimal value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "TestClass{" +
                    "date=" + date +
                    ", value=" + value +
                    '}';
        }
    }
    @Test
    public void test() throws IOException {
        System.out.println(json);
        //ApplicationDetails applicationDetails = APPLICATION_DETAILS_JSON_ADAPTER.fromJson(json);
        TestClass testClass = TEST_CLASS_JSON_ADAPTER.fromJson(json);
        System.out.println(testClass);
    }

     static class TimeStampDateAdapter extends JsonAdapter<Date> {

        public TimeStampDateAdapter() {
        }

        public synchronized Date fromJson(JsonReader reader) throws IOException {
            long val = 0;
            try {
                val = reader.nextLong();
            } catch (Exception e) {
                //ignore
                return null;
            }

            return new Date(val);
        }

        public synchronized void toJson(JsonWriter writer, Date value) throws IOException {
            writer.value(value.getTime());
        }
    }

    static class BigDecimalAdapter extends JsonAdapter<BigDecimal> {

        @Override
        public BigDecimal fromJson(JsonReader reader) throws IOException {
            String s = null;
            try {
                s = reader.nextString();
            } catch (JsonDataException e) {
                //when its null we will have this so ignore it.
                return null;
            }


            return new BigDecimal(s);
        }

        @Override
        public void toJson(JsonWriter writer, BigDecimal value) throws IOException {
            if(value == null){
                writer.value("");
            }else {
                writer.value(value.toPlainString());
            }
        }
    }

Most helpful comment

Or don't handle null in either method and call nullSafe() on the adapter
before registering it.

On Wed, Apr 5, 2017, 8:46 PM Eric Cochran notifications@github.com wrote:

Your BigDecimalAdapter is calling reader.nextString() instead of peeking
for null.
You probably wanted something like:

if (reader.peek() == JsonReader.Token.NULL) return reader.nextNull();return new BigDecimal(reader.nextString());

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/276#issuecomment-292037590, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAEEES4x1S6R9W4tI2QjToeVLry5RQazks5rtDXQgaJpZM4M09iP
.

All 2 comments

Your BigDecimalAdapter is calling reader.nextString() instead of peeking for null.
You probably wanted something like:

if (reader.peek() == JsonReader.Token.NULL) return reader.nextNull();
return new BigDecimal(reader.nextString());

Or don't handle null in either method and call nullSafe() on the adapter
before registering it.

On Wed, Apr 5, 2017, 8:46 PM Eric Cochran notifications@github.com wrote:

Your BigDecimalAdapter is calling reader.nextString() instead of peeking
for null.
You probably wanted something like:

if (reader.peek() == JsonReader.Token.NULL) return reader.nextNull();return new BigDecimal(reader.nextString());

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/276#issuecomment-292037590, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAEEES4x1S6R9W4tI2QjToeVLry5RQazks5rtDXQgaJpZM4M09iP
.

Was this page helpful?
0 / 5 - 0 ratings