本文整理了Java中com.google.gson.Gson.toJsonTree()
方法的一些代码示例,展示了Gson.toJsonTree()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Gson.toJsonTree()
方法的具体详情如下:
包路径:com.google.gson.Gson
类名称:Gson
方法名:toJsonTree
[英]This method serializes the specified object into its equivalent representation as a tree of JsonElements. This method should be used when the specified object is not a generic type. This method uses Class#getClass() to get the type for the specified object, but the getClass() loses the generic type information because of the Type Erasure feature of Java. Note that this method works fine if the any of the object fields are of generic type, just the object itself should not be of a generic type. If the object is of generic type, use #toJsonTree(Object,Type) instead.
[中]此方法将指定的对象序列化为JsonElements树的等效表示形式。当指定的对象不是泛型类型时,应使用此方法。此方法使用类#getClass()获取指定对象的类型,但由于Java的类型擦除功能,getClass()会丢失泛型类型信息。请注意,如果任何对象字段都是泛型类型,则此方法可以正常工作,只是对象本身不应是泛型类型。如果对象是泛型类型,请改用#toJsonTree(对象,类型)。
代码示例来源:origin: gocd/gocd
public JsonElement toJSON() {
return GSON.toJsonTree(this);
}
代码示例来源:origin: gocd/gocd
public JsonElement toJSON() {
return GSON.toJsonTree(this);
}
代码示例来源:origin: gocd/gocd
public JsonElement toJSON() {
return GSON.toJsonTree(this);
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public JsonElement getValueAsJson() {
return GSON.toJsonTree(value);
}
}
代码示例来源:origin: gocd/gocd
public JsonReader jsonReaderFrom(Map map) {
try {
return new JsonReader(GSON.toJsonTree(map).getAsJsonObject());
} catch (Exception e) {
throw new JsonParseException(e);
}
}
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingMaterials()
{
CRMaterial value = customHg;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is(CRHgMaterial.TYPE_NAME));
}
@Test
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingMaterials()
{
CRMaterial value = named;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is(CRConfigMaterial.TYPE_NAME));
}
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingMaterials()
{
CRMaterial value = customSvn;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is(CRSvnMaterial.TYPE_NAME));
}
@Test
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingMaterials()
{
CRMaterial value = dependsOnPipeline;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is(CRDependencyMaterial.TYPE_NAME));
}
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingNantTask()
{
CRTask value = nantWithPath;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is("nant"));
}
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingMaterials()
{
CRMaterial value = veryCustomGit;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is(CRGitMaterial.TYPE_NAME));
}
@Test
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingMaterials()
{
CRMaterial value = pluggableGit;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is(CRPluggableScmMaterial.TYPE_NAME));
}
@Test
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingRakeTask()
{
CRTask value = rakeTask;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is("rake"));
}
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingTask()
{
CRTask value = curl;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is(CRPluggableTask.TYPE_NAME));
}
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingTasks()
{
CRTask value = simpleExecWithArgs;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is(CRExecTask.TYPE_NAME));
}
@Test
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingMaterials()
{
CRMaterial value = customTfs;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is(CRTfsMaterial.TYPE_NAME));
}
@Test
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingMaterials()
{
CRMaterial value = p4custom;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is(CRP4Material.TYPE_NAME));
}
@Test
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingAntTask()
{
CRTask value = antTask;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is("ant"));
}
@Test
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingMaterials()
{
CRMaterial value = packageMaterial;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is(CRPackageMaterial.TYPE_NAME));
}
@Test
代码示例来源:origin: gocd/gocd
@Test
public void shouldIgnoreWhenJsonHasUnknownElements()
{
Map<String,T> examples = getExamples();
for(Map.Entry<String,T> example : examples.entrySet()) {
T value = example.getValue();
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
jsonObject.add("extraProperty", new JsonPrimitive("This is not part of message type"));
String json = gson.toJson(jsonObject);
T deserializedValue = (T)gson.fromJson(json,value.getClass());
assertThat(String.format("Example %s - Deserialized value should equal to value before serialization", example.getKey()),
deserializedValue, is(value));
}
}
内容来源于网络,如有侵权,请联系作者删除!