question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.IllegalStateException: Nesting problem.

See original GitHub issue

I want to use custom TypeAdapter to serialize a nest class. But an exception occured:

Exception in thread "main" java.lang.IllegalStateException: Nesting problem.
	at com.google.gson.stream.JsonWriter.beforeValue(JsonWriter.java:655)
	at com.google.gson.stream.JsonWriter.open(JsonWriter.java:326)
	at com.google.gson.stream.JsonWriter.beginObject(JsonWriter.java:309)
	at com.example.gsonlib._01_basic_use.Test$AddressTypeAdapter.write(Test.java:30)
	at com.example.gsonlib._01_basic_use.Test$StudentTypeAdapter.write(Test.java:53)
	at com.example.gsonlib._01_basic_use.Test$StudentTypeAdapter.write(Test.java:41)
	at com.google.gson.Gson.toJson(Gson.java:704)
	at com.google.gson.Gson.toJson(Gson.java:683)
	at com.google.gson.Gson.toJson(Gson.java:638)
	at com.google.gson.Gson.toJson(Gson.java:618)
	at com.example.gsonlib._01_basic_use.Test.main(Test.java:22)

My code is the following:

public class Test {
    public static void main(String[] args) {
        Student student = new Student("wzc", 18, new Student.Address("earth"));
        Gson gson2 = new GsonBuilder()
                .serializeNulls()
                .registerTypeAdapter(Student.class, new StudentTypeAdapter(new AddressTypeAdapter()))
                .create();
        String json2 = gson2.toJson(student);
        System.out.println(json2);
    }

    static class AddressTypeAdapter extends TypeAdapter<Student.Address> {

        @Override
        public void write(JsonWriter out, Student.Address value) throws IOException {
            out.beginObject();
            out.name("address").value(value.nation);
            out.endObject();
        }

        @Override
        public Student.Address read(JsonReader in) throws IOException {
            return null;
        }
    }

    static class StudentTypeAdapter extends TypeAdapter<Student> {
        private AddressTypeAdapter mAddressTypeAdapter;

        public StudentTypeAdapter(AddressTypeAdapter addressTypeAdapter) {
            mAddressTypeAdapter = addressTypeAdapter;
        }

        @Override
        public void write(JsonWriter out, Student student) throws IOException {
            out.beginObject()
                    .name("name").value(student.name)
                    .name("age").value(student.age);
            mAddressTypeAdapter.write(out, student.adress);
            out.endObject();
        }

        @Override
        public Student read(JsonReader in) throws IOException {
            return null;
        }
    }
}
public class Student {
    public String name;
    public int age;
    public Address adress;

    public Student() {
    }

    public Student(String name, int age, Address adress) {
        this.name = name;
        this.age = age;
        this.adress = adress;
    }

    public static class Address {
        public String nation;

        public Address(String nation) {
            this.nation = nation;
        }
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jhwsxcommented, May 28, 2020

Thanks a lot.

0reactions
lyubomyr-shaydarivcommented, May 28, 2020

@jhwsx yes, if you’re writing a JSON object (in curly braces {...}), you must write key/value pairs. You can’t put anonymous values in objects as this is disallowed by the JSON grammar, not Gson (any sane JSON tool would reject such a JSON document).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nesting error on write GSON file by JsonWriter - Stack Overflow
Nesting error on write GSON file by JsonWriter ; you are putting key/value pairs in an array. you can't do that, you need...
Read more >
java.lang.IllegalStateException: Nesting problem
Has anyone encountered an error like that shown below. What does it mean? 05-31 22:05:04.406: W/System.err(2818): java.lang.
Read more >
Nesting problem in ChartController : TW-55309 - YouTrack
java.lang.IllegalStateException: Nesting problem in ChartController ... This exception seems to be caused by closed connection during writing response. Added a ...
Read more >
78520 – java.lang.IllegalStateException after nesting classes ...
Open popup menu for 'Parent' class element and invoke 'Navigate to Source'. java.lang.IllegalStateException will rise.
Read more >
JsonWriter.java example - Javatips.net
This class describes the usage of JsonWriter.java. ... if (context != nonempty && context != empty) { throw new IllegalStateException("Nesting problem.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found