Gson requires custom deserialization for the situation in the original question. Following is one such example.
input.json:
[
{
"text":"some text"
},
{
"text":{}
}
]
Foo.java:
import java.io.FileReader;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class Foo
{
public static void main(String[] args) throws Exception
{
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(String.class, new StringDeserializer());
Gson gson = gsonBuilder.create();
Thing[] things = gson.fromJson(new FileReader("input.json"), Thing[].class);
System.out.println(gson.toJson(things));
}
}
class Thing
{
String text;
}
class StringDeserializer implements JsonDeserializer<String>
{
@Override
public String deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException
{
if (json.isJsonPrimitive()) return json.getAsString();
return "";
}
}
Output:
[{"text":"some text"},{"text":""}]
Using instead a custom deserializer for the Thing.class type would of course be possible. This would have the benefit of not adding additional processing for every String, but then you'd be stuck with "manual" processing all of the other attributes of Thing.