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

Serializing not as expected when instancing within double braces #2242

Closed
1 of 4 tasks
tkzt opened this issue Nov 24, 2022 · 3 comments
Closed
1 of 4 tasks

Serializing not as expected when instancing within double braces #2242

tkzt opened this issue Nov 24, 2022 · 3 comments
Labels

Comments

@tkzt
Copy link

tkzt commented Nov 24, 2022

Gson version

2.10

Java / Android version

Java

Used tools

  • Maven; version: 3.8.1
  • Gradle; version:
  • ProGuard (attach the configuration file please); version:
  • ...

Description

When creating an instance of a nested class in a normal way:

class Test {
    public String name;
    public Test2 test2;

    public void setName(String name){
        this.name = name;
    }

    public void setTest2(Test2 test2){
        this.test2 = test2;
    }
}
class Test2{
    public String info;
    public void setInfo(String info){
        this.info = info;
    }
}

public class Main {
    public static void main(String[] args) {
        Gson gson = new Gson();
        
        Test test = new Test(){{setName("foo");}};
        test.setTest2(new Test2(){{setInfo("bar");}});
        String result = gson.toJson(test);
    }
}

The result is:

{"name":"foo","test2":{"info":"bar"}}

But when instancing Test2 also in a double braces way:

public class Main {
    public static void main(String[] args) {
        Gson gson = new Gson();
        
        Test test = new Test(){{ setName("foo"); setTest2(new Test2(){{ setInfo("bar"); }}); }};
        String result = gson.toJson(test);
    }
}

test2 part is gone:

{"name":"foo"}

Expected behavior

Nested part will also be serialized when in a double braces way.

Actual behavior

It will not.

@tkzt tkzt added the bug label Nov 24, 2022
@Marcono1234
Copy link
Collaborator

Double brace initialization actually creates an anonymous class, and Gson does not allow anonymous classes at the moment. Unfortunately it does not handle them in a very user friendly way and just serializes a JSON null, which is omitted from your output because new Gson() omits null values, see also GsonBuilder.serializeNulls().

There is an open discussion about this in #1510, therefore I am closing your issue. Unfortunately no backward compatible solution for this has been found yet.

@eamonnmcmanus
Copy link
Member

Just to add that double-brace initialization is usually best avoided anyway, as detailed here.

@tkzt
Copy link
Author

tkzt commented Nov 25, 2022

@eamonnmcmanus @Marcono1234
Learned a lot! Thanks~👻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants