user1482309
user1482309

Reputation: 309

Serialize String without escape " to \"

I want to export a json String for my restful webservice.

My class looks like:

public class Animal {
  private String name;
  private JsonElement additionalProperties; //generated with gson
  private String additionalProperties2; //same as above but direct from db
}

additionalProperties is a Json field in the db and is returned to Java as String. Idea is, that every animal can have their special properties and anykind of data structure, without the need to model it on Java side.

When I try to export this is use gson.

 List<Animal> animals = database.getAllAnimals(); //simplified
 return gson.toJson(animals); // in real via javax.ws.rs.core.Response

My problem is that the output looks like:

[
  {"name": "Mia", 
   "additionalProperties: "{\"race\": \"dog\"}",
   "additionalProperties2: "{\"race\": \"dog\"}"},
  {...} ...
]

Instead I want:

[
  {"name": "Mia", 
   "additionalProperties: {"race": "dog"}"
  {...} ...
]

I alread tried to build up the JsonObjects without additionalProperties and use "add property" to add additionalProperties. But the output is the same. i guess the problem is the Java String serializen. DB output is correct. additionalProperties is a normal String without encoding of ".

I saw Jacksons property @JsonRawValue. How does this work? Can I make this also with gson?

Upvotes: 2

Views: 3125

Answers (1)

Andreas
Andreas

Reputation: 159135

First, your JsonElement should work fine, so maybe you created it the wrong way.

Second, to output a String value containing JSON text, without escaping it, you can to use a @JsonAdapter and jsonValue(String value):

Writes value directly to the writer without quoting or escaping.

Example code to show both:

public class Test {
    public static void main(String[] args) throws Exception {
        String jsonTextFromDb = "{\"race\": \"dog\"}";

        JsonElement additionalProperties = new JsonParser().parse(jsonTextFromDb);
        Animal animal = new Animal("Mia", additionalProperties, jsonTextFromDb);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        System.out.println(gson.toJson(animal));
    }
}

class Animal {
    private String name;
    private JsonElement additionalProperties;
    @JsonAdapter(JsonTextAdapter.class)
    private String additionalProperties2;
    Animal(String name, JsonElement additionalProperties, String additionalProperties2) {
        this.name = name;
        this.additionalProperties = additionalProperties;
        this.additionalProperties2 = additionalProperties2;
    }
}

class JsonTextAdapter extends TypeAdapter<String> {
    @Override
    public void write(JsonWriter out, String str) throws IOException {
        out.jsonValue(str);
    }
    @Override
    public String read(JsonReader in) throws IOException {
        return new JsonParser().parse(in).toString();
    }
}

Output

{
  "name": "Mia",
  "additionalProperties": {
    "race": "dog"
  },
  "additionalProperties2": {"race": "dog"}
}

Upvotes: 4

Related Questions