Class's fields initialization from JSON using Gson?

The problem is Generic Type Erasure. GSON has no idea, what kinds of objects to create because your class contains only a List (type information regarding Task is lost).

Up vote 0 down vote favorite 1 share g+ share fb share tw.

I'm trying to use Gson to initialize my fields, but no success. String complex = "{'menu': { 'id': 'file', 'value': 'File', 'popup': { 'task': {'value': 'New', 'onclick': 'CreateNewDoc()'}, {'value': 'Open', 'onclick': 'OpenDoc()'}, {'value': 'Close', 'onclick': 'CloseDoc()'} }}}"; Trying to do that with: TasksHolder th = gson. FromJson(complex, TasksHolder.

Class); TaskHolder class: public class TasksHolder { List task; public TasksHolder() { task = new ArrayList(); } } Please advice what can be done to make task of TaskHolder to be filled. Java json gson link|improve this question asked Jan 3 '11 at 15:30siik1,8381046 91% accept rate.

The problem is Generic Type Erasure. GSON has no idea, what kinds of objects to create because your class contains only a List (type information regarding Task is lost). Here's how to deal with this problem in GSON: Type listType = new TypeToken>() {}.getType(); List tasks = gson.

FromJson(myTasks, listType); But that won't help you, since you want the parent object, not just the list. I'm afraid you will have to write your own Custom Serialization and Deserialization methods.

I think most important part for me are these tasks data, so it should be fine, thanks for the information. – siik Jan 3 '11 at 15:47 @AndroidNoob thanks is nice, upvotes are nicer – Sean Patrick Floyd Jan 3 '11 at 15:58 Although I've got exception: Caused by: java.lang. IllegalStateException: This is not a JSON Array.

– siik Jan 3 '11 at 16:00 @AndroidNoob Yes, you probably have to replace your single quotes with double quotes – Sean Patrick Floyd Jan 3 '11 at 16:01 @Sean, I changed to this and got the same exception: String complex = "{\"menu\": { \"id\": \"file\", \"value\": \"File\", \"task\": { \"task\": {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"} }}}"; – siik Jan 3 '11 at 16:05.

Your JSON does not map to class you define: it is missing couple of levels (main-level object that has 'menu' property, then menu object that actually contains TaskHolder. So you should either define class structure that JSON maps to (how else would Gson know what to map where? ), or pre-process JSON to remove stuff you don't need.

Type-erasure mentioned by the other answer should NOT be the problem here: your 'task' property is declared as List, and that should be enough for Gson to determine generic type. Yes, field declarations do retain generic type information; type erasure has more to do with runtime information available via Class. So while type reference objects are needed for root types, they are not needed for properties under root properties.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions