jerryhui633 / google-gson

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

Gson.toJson(Object) emits surprising results for JsonElement values #362

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This may not be a bug, but the behavior surprised me (so I figured I should 
mention it).

The Gson.toJson(Object) method handles JsonElement values differently than the 
Gson.toJson(JsonElement) method.  This can come up when you have heterogeneous 
collections, for example, where only some (but not all) of the elements are 
JsonElement values.

Here's a sample program.

import com.google.gson.*;

public class Test
{
    public static void
    main(String[] args)
    {
        String input = "{\"name\": \"lars\", \"age\": 12}";
        Object obj = new JsonParser().parse(input).getAsJsonObject();       
        String output = new Gson().toJson(obj);

        System.out.println(output);
        /*
            expected output:
                either `{"name":"lars","age":12}`
                or `{"age":12,"name":"lars"}`

            actual output:
                `{"members":{"name":{"value":"lars"},"age":{"value":12}}}`
        */
}

(In case it's not clear, I was surprised to see the `members` and the `value` 
fields in the output.)

Original issue reported on code.google.com by von...@gmail.com on 31 Aug 2011 at 5:14

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r991.

Original comment by limpbizkit on 2 Oct 2011 at 5:00