Deserialize error eith No-arg constructor does not exist

124 views
Skip to first unread message

Yoichi

unread,
Dec 10, 2009, 4:15:58 AM12/10/09
to google-gson
I am trying to do:

Gson gson = new Gson();
Type listType = new TypeToken<List<MyPOJO>>() {}.getType();
List<MyPOJO> centres = gson.fromJson(json, listType);

The MyPOJO has only prinitive types as members. The json is a String
or an InputStreamReader generated from the List<MyPOJO> that was
serialized to JSON previously. I am trying to restore the List.

I get an error message:

No-args constructor for class com.google.appengine.api.datastore.Text
does not exist. Register an InstanceCreator with Gson for this type to
fix this problem.

This sounds like it is trying to Deserilize Text to Java String but it
fails.

Any idea or suggestinon, as to why this happens and how to fix it?

qiongju

unread,
Dec 10, 2009, 9:24:31 PM12/10/09
to google-gson
would you show us the full exception?
I guess it points to class MyPOJO.you write a constructor with
parameters in it.
but if you can't register an InstanceCreator of class MyPOJO,there
must be a constructor with no-tags.

Yoichi Takayama

unread,
Dec 11, 2009, 5:01:20 PM12/11/09
to googl...@googlegroups.com
Thanks you very much for replying.

The problem was solved and it is really good that to report it.

It was caused by my POJO being JDO and using Google class:

com.google.appengine.api.datastore.Text

I did this:

GsonBuilder gb = new GsonBuilder();
gb.registerTypeAdapter(Text.class, new TextToStringAdapter());
gb.serializeNulls();
Gson gson = gb.create();

Now, it deserializes properly.

BTW, I could not find the TextToStringAdaptor easily but in the com.google.ytb suite as one of the inner classes. So I extracted it as:

package com.google.ytb;

import java.lang.reflect.Type;

import com.google.appengine.api.datastore.Text;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class TextToStringAdapter implements JsonSerializer<Text>,
JsonDeserializer<Text>
{
public JsonElement toJson(Text text, Type type, JsonSerializationContext context)
{
return serialize(text, type, context);
}

public Text fromJson(JsonElement json, Type type, JsonDeserializationContext context)
{
return deserialize(json, type, context);
}

@Override
public JsonElement serialize(Text text, Type type, JsonSerializationContext context)
{
return new JsonPrimitive(text.getValue());
}

@Override
public Text deserialize(JsonElement json, Type type, JsonDeserializationContext context)
{
try
{
return new Text(json.getAsString());
}
catch (JsonParseException e)
{
// TODO: This is kind of a hacky way of reporting back a parse
// exception.
return new Text(e.toString());
}
}
}


This reminds me that I actually haven't checked if an error text is in the deserialized Text instead of the proper one, although it seems it is working  :-(

Cheer,
Yoichi


--

You received this message because you are subscribed to the Google Groups "google-gson" group.
To post to this group, send email to googl...@googlegroups.com.
To unsubscribe from this group, send email to google-gson...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-gson?hl=en.



Reply all
Reply to author
Forward
0 new messages