0

Class:

public class MyList {

    public int type;
    public String text;
    public boolean[] arr;

    public MyList(int type, String text) {
        this.type = type;
        this.text = text;
        switch (type) {
        case 0:
            arr = new boolean[5];
            break;
        case 1:
            arr = new boolean[10];
            break;
        }
    }
}

JSON Data:

{ "list": [ { "type": 0, "text": "Text 1" }, { "type": 1, "question": "Text 2" } ] }

Parser:

ArrayList<MyList> getList(){
    Gson gson = new Gson();
    MyListsArray mLists = gson.fromJson(bufferString, MyListsArray.class);
    return mLists.list;
}

Class to hold the list items:

public class MyListsArray {
    public ArrayList<MyList> list;
}

Everything goes fine, I get proper values for type and text which are present in the JSON String. But the arr remains null.

10
  • The JSON sample does not contain any boolean arrays, therefore the empty result Commented Apr 15, 2014 at 11:37
  • But isn't it supposed to go through the constructor and initiate the boolean array? Commented Apr 15, 2014 at 11:37
  • 1
    @bhutto Yeah. But I thought this process will go through the Constructor and eventually create a boolean array. So, how to initiate data which is not present in the Json string? Commented Apr 15, 2014 at 11:43
  • 2
    @Archie.bpgc Gson uses the properties not getter/setter and Gson instantiate the class object by invoking the default constructor Commented Apr 15, 2014 at 11:46
  • 1
    @Archie.bpgc GSON use ONLY fields(private,public,protected). Doc says : There are good arguments to support properties as well. We intend to enhance Gson in a latter version to support properties as an alternate mapping for indicating Json fields. For now, Gson is fields-based see doc here sites.google.com/site/gson/gson-design-document Commented Apr 16, 2014 at 10:50

2 Answers 2

2

GSon never calls a constructor of a MyList class in your code. That's why arr gets the default value. Which is null.

Sign up to request clarification or add additional context in comments.

Comments

1

You can initiate data which is not present in the Json string as below write CustomDeserializer

class CustomDeserializer implements JsonDeserializer<List<MyList>> {

    @Override
    public List<MyList> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {

       JsonArray jsonArray =    jsonElement.getAsJsonObject().getAsJsonArray("list");

       List<MyList>  list=new ArrayList<>(30);
       Gson gson = new Gson();

       for (JsonElement element : jsonArray) {

                MyList ob =  gson.fromJson(element,  MyList.class);
                switch (Integer.valueOf(ob.type)) {
                case 0:
                    ob.arr = new boolean[5];
                    break;
                case 1:
                    ob.arr = new boolean[10];
                    break;
                }

               list.add(ob);
        }

        return list;
    }

Finally parse it

 Type typeOf = new TypeToken   <List<MyList>>(){}.getType();
 Gson gson = new GsonBuilder().registerTypeAdapter(typeOf, new CustomDeserializer()).create();
 List<MyList> list = gson.fromJson(builder.toString(),typeOf);

Gson documentation says, if your class doesn't have an no-args constructor and you have not registered any InstanceCreater objects, then it will create an ObjectConstructor (which constructs your Object) with an UnsafeAllocator which uses Reflection to get the allocateInstance method of the class sun.misc.Unsafe to create your class' instance.

If you do have a no-args constructor, Gson will use an ObjectConstructor which uses that default Constructor by calling

yourClassType.getDeclaredConstructor(); // ie. empty, no-args

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.