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.