Skip to content

Instantly share code, notes, and snippets.

@leviyehonatan
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leviyehonatan/1dcbb146fe34838ca534 to your computer and use it in GitHub Desktop.
Save leviyehonatan/1dcbb146fe34838ca534 to your computer and use it in GitHub Desktop.
gson serialization yields null
package test;
import java.util.List;
public class LoginInfo {
public class DeviceInfo {
public String deviceId;
public String deviceType;
public String deviceOs;
public int deviceVersion;
public boolean deviceScanAlways;
}
public class SdkIdentity {
public String sdkKey;
public String userId;
}
public class Data {
public DeviceInfo deviceInfo;
public List<SdkIdentity> sdk_identities;
}
public Data data;
}
package test;
import java.util.ArrayList;
import com.google.gson.Gson;
public class Test {
public static void main(String[] args) {
LoginInfo loginInfo = new LoginInfo() {
{
data = new Data() {
{
deviceInfo = new DeviceInfo() {
{
deviceId = "dfs";
deviceOs = "";
deviceScanAlways = false;
deviceVersion = 5;
deviceType = "type";
}
};
sdk_identities = new ArrayList<SdkIdentity>() {
{
add(new SdkIdentity() {
{
sdkKey = "xxx";
userId = "none";
}
});
}
};
}
};
}
};
Gson gson = new Gson();
System.out.println(gson.toJson(loginInfo));
}
}
@MaximShoustin
Copy link

public static void main(String[] args) {

    LoginInfo loginInfo = new LoginInfo();      

    Data data = loginInfo.new Data();
    data.sdk_identities = new ArrayList<LoginInfo.SdkIdentity>();

    loginInfo.data = data;

    Gson gson = new Gson();
    System.out.println(gson.toJson(loginInfo));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment