jerryhui633 / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

TypeAdapter issue #405

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
TypeAdapter for String.Class doesn't work on gson 2.1 (was working on previous 
version)

here a small test case:

expected output should be "test", but i got "candy" instead.
Note: this issue is only relative to the current 'stable' release.

------
public class TestCase {

    public static void main(String[] args) {

        String test = "candy";
        Gson gson = new GsonBuilder()
                .excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT)
                .registerTypeAdapter(String.class, new JsonSerializer<String>() {
                    public JsonElement serialize(String src, Type typeOfSrc, JsonSerializationContext context) {
                        return new JsonPrimitive("test");
                    }
                }).setPrettyPrinting().create();
        System.out.println(gson.toJson(test, String.class));
    }

}

Original issue reported on code.google.com by ramarr...@gmail.com on 1 Feb 2012 at 9:01