13

When I try to convert a basic object to json, it seems to return null with everything I have tried. What's wrong?

Person.java

public class Person {
    public String Name;
    public String Address;
}

Main code

Person person = new Person() {
    {
        Name = "John";
        Address = "London";
    }
};

Gson gson = new Gson();
String jsonPerson = gson.toJson(person);
durron597
  • 31,968
  • 17
  • 99
  • 158
Karl Green
  • 260
  • 1
  • 4
  • 11
  • Is not working because you don't have a constructor on your base class...just add a parameter less constructor after `Address` e.g: `Person(){}` and try again... – Hackerman Sep 16 '16 at 19:19
  • 1
    post actual compilable code, at the very least – njzk2 Sep 16 '16 at 19:28
  • 1
    To whoever downvoted my answer, just because OP is a beginner doesn't make this a bad question, and it certainly doesn't make my answer bad. Flag / vote to close as duplicate if you want – durron597 Sep 16 '16 at 19:34

2 Answers2

21

Gson doesn't support serializing anonymous types.

See the duplicate linked by Sotirios Delimanolis. Note that the double brace initializer you were using effectively creates an anonymous subclass, which has some nasty side effects like creating new classes every time you use it, and breaking things like Gson.

It would work if you created a constructor like this:

class Ideone
{
    public class Person {
        public String Name;
        public String Address;

        public Person(String Name, String Address) {
            this.Name = Name;
            this.Address = Address;
        }
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        Person person = new Person("John", "London");

        // System.out.println(person.Name);
        Gson gson = new Gson();
        String jsonPerson = gson.toJson(person);
    }
}

As an aside, you should not name your fields with capital letters; begin classes with capital letters and fields with lowercase letters.

Have a look at the Google Java Style Guide for a good reference on naming conventions

durron597
  • 31,968
  • 17
  • 99
  • 158
  • That's strange, it was compiling for me, not throwing an exception and proceeding to code after those statements. Your solution has fixed it for me though so thanks for that and the style guide document – Karl Green Sep 16 '16 at 19:32
  • 2
    -1 you forgot about Java's initializer blocks (see [the question you linked to](http://stackoverflow.com/a/1988268/1521179)), which are frequently used to *simulate* named arguments for constructors. [See this actually runnable version of your code](https://ideone.com/mHWoD8) that uses an initializer block as in OP's question, and declares the nested `Person` class as static so that it can be referenced in `main`. – amon Sep 16 '16 at 19:47
5

The way you are instantiating your object creates an anonymous class so you loose the type. You can instead use a TypeToken to fix this as follows

Type personType = new TypeToken<Person>(){}.getType();
Gson gson = new Gson();
String jsonPerson = gson.toJson(person, personType);
blue
  • 539
  • 3
  • 7