dungnn / google-gson

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

TypeAdapter issue #404

Closed GoogleCodeExporter closed 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

GoogleCodeExporter commented 9 years ago
This was a difficult, but intentional decision. Why do you want to register a 
type adapter for String?

Original comment by limpbizkit on 2 Feb 2012 at 6:27

GoogleCodeExporter commented 9 years ago
sometimes a string can contain data (xml) that have to be handled in a specific 
way.

In my scenario, i use xml -> string to serialize an xml via socket between 2 
app (and writing them on DB)

xml implementation isn't serializable (so, i have to do a conversion to string)

now, after all the trip (sending data via java serialization, write them on db, 
sending results back) i want to dump the result to a js client, that work 
better with json.

on 1.7.2 i add a typeadapter that try to detect if a string is an xml (contains 
/> or > or something like that, i haven't the code here) and if so, do a custom 
serialization before send it out.

Obviusly i can rewrote all the serialization layer, but this will be time 
consuming.
What i have found convenient, is to wrap my request/response bean, make the 
string that contain xml not gson_able (via notation) and make a getter to write 
on the wrapper the XML element that i need.
Then, i use the typeAdapter registered with Xml.Class and make here the custom 
serialization.

The only issue is that i have to write more code :(

The other option is to make XML serializable, and this will solve all my issue. 
Unfortunatly this solution isn't feasable due to implementation of XML library 
(can be probably wrapped as well, with some externalizable stuff, but again is 
time consuming)

Original comment by ramarr...@gmail.com on 6 Feb 2012 at 8:05

GoogleCodeExporter commented 9 years ago
Presumably you know which of your strings are eligible to be XML. Create a type 
XmlOrString and use that in the appropriate fields. Then register a type 
adapter for that type.

class XmlOrString {
  Document xml;
  String string;
}

Original comment by limpbizkit on 6 Feb 2012 at 1:42

GoogleCodeExporter commented 9 years ago
In your example, XmlOrString isn't serializable (as i state before,
XML /our impl of XML dom/ isn't serializable, due to high resource
need to perform the serialization. Same issue appen on dom4j for
example)

Original comment by ramarr...@gmail.com on 6 Feb 2012 at 7:00

GoogleCodeExporter commented 9 years ago
What does serialization have to do with Gson?

I'm marking this as won't fix just 'cause I don't think your use case is 
typical!

Original comment by limpbizkit on 11 Feb 2012 at 6:30