Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

This issue was closed by revision r991.

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

  • Changed state: Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant