2

I have following classes:

public class MyProperty
{
   public String Key;
   public String Value;
}

public class MyModel
{
   public String Name;
   public List<MyProperty> Properties;
}

When I try to serialize an object of type MyObject like this:

MyModel m = new MyModel(){{
   Name="aaaa";
   Properties = new ArrayList<MyProperty>();
}};

m.Properties = new ArrayList<MyProperty>();

m.Properties.add(new MyProperty() {{ Key="a"; Value="1"; }});
m.Properties.add(new MyProperty() {{ Key="b"; Value="11"; }});
m.Properties.add(new MyProperty() {{ Key="c"; Value="111"; }});

String json1 = g.toJson(m, MyModel.class);

I'm getting following result:

{"Name":"aaaa","Properties":[null,null,null]}

Why is the list of properties serialized to list of null's when the source objects are definitely not null?

Deserialization of a string

{"Name":"aaaa","Properties":[{"Key":"a","Value":"1" etc }]} 

works fine.

Stefan Michev
  • 4,795
  • 3
  • 35
  • 30

2 Answers2

0

The problem you're likely hitting is polymorphism - you're model says that the Properties are of type "MyProperty" but your code fragment uses "SyncProperty". There are some gotchas to doing this with Gson - have a look at the discussion here: How to handle deserializing with polymorphism?

Community
  • 1
  • 1
Owais Ali
  • 724
  • 1
  • 6
  • 14
0

I will tell you why this code has the output you are looking for and not the code you have in question.

Code

import java.util.ArrayList;

import com.google.gson.Gson;

public class TestOneDrive {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        MyModel model = new MyModel();
        model.setName("someName");

        ArrayList<MyProperty> myProperties = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            MyProperty myProperty = new MyProperty();
            myProperty.setKey("Key_" + i);
            myProperty.setValue("Value_" + i);
            myProperties.add(myProperty);
        }
        model.setProperties(myProperties);

        String result = (new Gson()).toJson(model);
        System.out.println("" + result);

    }

}

class MyProperty {

    public String Key;
    public String Value;

    public String getKey() {
        return Key;
    }

    public void setKey(String key) {
        Key = key;
    }

    public String getValue() {
        return Value;
    }

    public void setValue(String value) {
        Value = value;
    }
}

class MyModel {
    public String Name;
    public ArrayList<MyProperty> Properties;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public ArrayList<MyProperty> getProperties() {
        return Properties;
    }

    public void setProperties(ArrayList<MyProperty> properties) {
        Properties = properties;
    }
}

Output

{
  "Name": "someName",
  "Properties": [
    {
      "Key": "Key_0",
      "Value": "Value_0"
    },
    {
      "Key": "Key_1",
      "Value": "Value_1"
    },
    {
      "Key": "Key_2",
      "Value": "Value_2"
    },
    {
      "Key": "Key_3",
      "Value": "Value_3"
    },
    {
      "Key": "Key_4",
      "Value": "Value_4"
    }
  ]
}
Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30