Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make gson NOT put quotes around field names

Tags:

java

json

gson

Say I have a basic class in Java:

public class Person{
    public String name;
}

When I give an object, instantiated with the name of "bob", to gson to serialize, it comes back as :

{"name" : "bob"}

How can I make it so it gives me:

{name:"bob"}

I know this is a simple question, but I'm not finding anything to help me in the API, and I apparently don't know the terminology well enough for json to do searches good enough to find the answer.

like image 761
Indigenuity Avatar asked Mar 16 '12 16:03

Indigenuity


1 Answers

Yeah, as mentioned above, the JSON spec expects quotes.

Now, if you really want your stuff, you can try creating your own JSONWriter and passing it to Gson.toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOException

like image 99
Guillaume Polet Avatar answered Nov 20 '22 19:11

Guillaume Polet