如何序列化JsonObject而不用太多引号? [英] How to serialize a JsonObject without too much quotes?
问题描述
当我序列化我的Json时,我得到:
{key1:value1,key2:value2,key3:{\innerKey \ \\value3 \}}
如何摆脱多余的引号? p>
我的代码:
public class JsonBuilder {
private final JsonObject json = new JsonObject();
private final Map< String,JsonBuilder> children = newHashMap(); $(Map.Entry< String,JsonBuilder>条目:children.entrySet()){
String value = entry.getValue()。的toJSON();
add(entry.getKey(),value);
}
children.clear();
返回json.toString();
public JsonBuilder add(String key,String value){
json.addProperty(key,value);
返回此;
}
public JsonBuilder add(String key,JsonBuilder value){
Preconditions.checkArgument(!children.containsKey(key));
children.put(key,value);
返回此;
String json = new JsonBuilder()
.add(key1,value1)
.add(key2 ,value2)
.add(key3,new JsonBuilder()
.add(innerKey,value3))
.toJson();
这里的问题是,对象位于 JsonBuilder
类中。尽可能贴近 JsonObject
,并且只是稍微扩展它。如果你想包装一个 JsonBuilder
,像这样做,尽可能少地修改 JsonObject
:
package com.craigmj.gson;
import java.util.HashMap;
import com.google.gson.JsonObject;
class JsonBuilder {
public final JsonObject json = new JsonObject();
public String toJson(){
return json.toString();
public JsonBuilder add(String key,String value){
json.addProperty(key,value);
返回此;
}
public JsonBuilder add(String key,JsonBuilder value){
json.add(key,value.json);
返回此;
public class GsonTest {
public static void main(String [] args){
System .out.println(new JsonBuilder()
.add(key1,value1)
.add(key2,value2)
.add(key3,新的JsonBuilder()
.add(innerKey,value3))
.toJson());
}
}
请注意, json
property public,这样如果你想要完整的gson功能,你仍然可以使用所有的gson方法。 (当然,你应该有一个getter。)不幸的是,不能扩展 JsonObject
,因为它是final的。
I'm writing a small fluent wrapper over com.google.gson.JsonObject.
When I serialize my Json, I get:
{"key1":"value1","key2":"value2","key3":"{\"innerKey\":\"value3\"}"}
How do I get rid of the redundant quotes?
My code:
public class JsonBuilder {
private final JsonObject json = new JsonObject();
private final Map<String, JsonBuilder> children = newHashMap();
public String toJson() {
for (Map.Entry<String, JsonBuilder> entry : children.entrySet()) {
String value = entry.getValue().toJson();
add(entry.getKey(), value);
}
children.clear();
return json.toString();
}
public JsonBuilder add(String key, String value) {
json.addProperty(key, value);
return this;
}
public JsonBuilder add(String key, JsonBuilder value) {
Preconditions.checkArgument(!children.containsKey(key));
children.put(key, value);
return this;
}
}
String json = new JsonBuilder()
.add("key1", "value1")
.add("key2", "value2")
.add("key3", new JsonBuilder()
.add("innerKey", "value3"))
.toJson();
The problem here is that you've mixed String's and Object's in your JsonBuilder
class. Rather stick as closely as possible to JsonObject
, and only extend it ever so slightly. If you want to wrap a JsonBuilder
, do it like this, with the smallest possible modification to JsonObject
:
package com.craigmj.gson;
import java.util.HashMap;
import com.google.gson.JsonObject;
class JsonBuilder {
public final JsonObject json = new JsonObject();
public String toJson() {
return json.toString();
}
public JsonBuilder add(String key, String value) {
json.addProperty(key, value);
return this;
}
public JsonBuilder add(String key, JsonBuilder value) {
json.add(key, value.json);
return this;
}
}
public class GsonTest {
public static void main(String[] args) {
System.out.println(new JsonBuilder()
.add("key1", "value1")
.add("key2", "value2")
.add("key3", new JsonBuilder()
.add("innerKey", "value3"))
.toJson());
}
}
Note that I've made the json
property public so that you can still use all the gson methods with it if you want the full gson functionality. (Of course, you should probably have a getter.) Unfortunately, one can't extends JsonObject
since it is final.
这篇关于如何序列化JsonObject而不用太多引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!