fyhack / google-gson

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

all should be strings #139

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Version 1.3 does a good job of converting an Object to JSON string 
following JSON standards.. However it does not suit my requirement, that 
is is all the data should be strings, meaning all the keys and values 
should be enclosed by double quotes including for numbers, float, and 
integers...
example:
"amount":1.3300000000000000710542735760100185871124267578125  - current
"amount":"1.3300000000000000710542735760100185871124267578125" - needed

How can i achieve this? Can i change the source code to get this behavior? 
where do i start? thanks a bunch in advance...
-

Original issue reported on code.google.com by pudur.ra...@gmail.com on 30 Jul 2009 at 9:31

GoogleCodeExporter commented 9 years ago
You should be able to accomplish this without modifying any of the Gson code, 
but
rather creating your own serializers for longs, float, double, etc.

Here is an example of a Long serializer:
public class MyLongSerializer implements JsonSerializer<Long> {
  public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext
context) {
    return new JsonPrimitive(String.valueOf(src));
  }
}

You would then register these custom serializers using the GsonBuilder.

Hope this helps,
Joel

Original comment by joel.leitch@gmail.com on 18 Aug 2009 at 6:01

GoogleCodeExporter commented 9 years ago
Having Gson output a number as a number and not as a string is a feature not a 
bug.

Original comment by inder123 on 23 Sep 2009 at 7:05