本文整理了Java中org.jclouds.json.Json.fromJson()
方法的一些代码示例,展示了Json.fromJson()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Json.fromJson()
方法的具体详情如下:
包路径:org.jclouds.json.Json
类名称:Json
方法名:fromJson
[英]Deserialize the object from json. If the object is a generic type, use #fromJson(Object,Type)
[中]从json反序列化对象。如果对象是泛型类型,请使用#fromJson(对象,类型)
代码示例来源:origin: stackoverflow.com
Json json = new Json();
json.setTypeName(null);
json.setUsePrototypes(false);
json.setIgnoreUnknownFields(true);
json.setOutputType(OutputType.json);
// I'm using your file as a String here, but you can supply the file as well
Data data = json.fromJson(Data.class, "{\"table\": [{\"id\": 1},{\"id\": 2},{\"id\": 3},{\"id\": 4}]}");
代码示例来源:origin: com.amysta.jclouds/jclouds-core
@SuppressWarnings("unchecked")
public <V> V apply(InputStream stream, Type type) throws IOException {
try {
return (V) json.fromJson(stream, type);
} finally {
if (stream != null)
stream.close();
}
}
}
代码示例来源:origin: jclouds/legacy-jclouds
@SuppressWarnings("unchecked")
public <V> V apply(InputStream stream, Type type) throws IOException {
try {
return (V) json.fromJson(Strings2.toStringAndClose(stream), type);
} finally {
if (stream != null)
stream.close();
}
}
}
代码示例来源:origin: jclouds/legacy-jclouds
@Provides
@Singleton
public Map<OsFamily, Map<String, String>> provideOsVersionMap(ComputeServiceConstants.ReferenceData data, Json json) {
return json.fromJson(data.osVersionMapJson, new TypeLiteral<Map<OsFamily, Map<String, String>>>() {
}.getType());
}
代码示例来源:origin: apache/jclouds
@Provides
@Singleton
public final Map<OsFamily, Map<String, String>> provideOsVersionMap(ComputeServiceConstants.ReferenceData data, Json json) {
return json.fromJson(data.osVersionMapJson, new TypeLiteral<Map<OsFamily, Map<String, String>>>() {
}.getType());
}
代码示例来源:origin: io.cloudsoft.jclouds/jclouds-core
@SuppressWarnings("unchecked")
public <V> V apply(InputStream stream, Type type) throws IOException {
try {
return (V) json.fromJson(Strings2.toStringAndClose(stream), type);
} finally {
if (stream != null)
stream.close();
}
}
}
代码示例来源:origin: org.jclouds/jclouds-core
@SuppressWarnings("unchecked")
public <V> V apply(InputStream stream, Type type) throws IOException {
try {
return (V) json.fromJson(Strings2.toStringAndClose(stream), type);
} finally {
if (stream != null)
stream.close();
}
}
}
代码示例来源:origin: org.apache.jclouds/jclouds-compute
@Provides
@Singleton
public final Map<OsFamily, Map<String, String>> provideOsVersionMap(ComputeServiceConstants.ReferenceData data, Json json) {
return json.fromJson(data.osVersionMapJson, new TypeLiteral<Map<OsFamily, Map<String, String>>>() {
}.getType());
}
代码示例来源:origin: org.apache.jclouds.provider/google-compute-engine
@Override
public Map<String, Object> apply(String input) {
try {
return json.fromJson(input, new TypeLiteral<Map<String, Object>>() {
}.getType());
} catch (Exception ex) {
return null;
}
}
};
代码示例来源:origin: org.jclouds.api/chef
@Override
public List<String> apply(String from) {
DatabagItem bootstrapConfig = bootstrapConfigForGroup.apply(from);
Map<String, JsonBall> config = json.fromJson(bootstrapConfig.toString(),
BootstrapConfigForGroup.BOOTSTRAP_CONFIG_TYPE);
JsonBall runlist = config.get("run_list");
return json.fromJson(runlist.toString(), RUN_LIST_TYPE);
}
代码示例来源:origin: jclouds/legacy-jclouds
public void testDeserializeEnumWithParser() {
assertEquals(json.fromJson("{enumValue : \"FOO\"}", EnumInsideWithParser.class).enumValue,
EnumInsideWithParser.Test.FOO);
}
代码示例来源:origin: jclouds/legacy-jclouds
public void testDeserializeEnumWithParserAndBadValue() {
assertEquals(json.fromJson("{enumValue : \"sd\"}", EnumInsideWithParser.class).enumValue,
EnumInsideWithParser.Test.UNRECOGNIZED);
}
代码示例来源:origin: com.amysta.jclouds/jclouds-core
@Override public Credentials apply(ByteSource from) {
try {
String creds = (checkNotNull(from)).asCharSource(Charsets.UTF_8).read();
return json.fromJson(creds, Credentials.class);
} catch (Exception e) {
logger.warn(e, "ignoring problem retrieving credentials");
return null;
}
}
}
代码示例来源:origin: apache/jclouds
public void autoValueSerializedNames() {
Json json = Guice.createInjector(new GsonModule()).getInstance(Json.class);
SerializedNamesType resource = SerializedNamesType.create("1234", Collections.<String, String>emptyMap());
String spinalJson = "{\"Id\":\"1234\",\"Volumes\":{}}";
assertEquals(json.toJson(resource), spinalJson);
assertEquals(json.fromJson(spinalJson, SerializedNamesType.class), resource);
}
代码示例来源:origin: apache/jclouds
public void testContainerWithVolumesNull() {
Container container = json.fromJson("{ \"Id\": \"foo\", \"Volumes\": null }", Container.class);
assertNotNull(container);
assertEquals(container.id(), "foo");
assertEquals(container.volumes(), ImmutableMap.of());
}
代码示例来源:origin: jclouds/legacy-jclouds
public void testPropertiesSerializesDefaults() {
Properties props = new Properties();
props.put("string", "string");
props.put("number", "1");
props.put("boolean", "true");
assertEquals(json.toJson(props), "{\"string\":\"string\",\"boolean\":\"true\",\"number\":\"1\"}");
Properties props3 = new Properties(props);
assertEquals(json.toJson(props3), "{\"string\":\"string\",\"boolean\":\"true\",\"number\":\"1\"}");
Properties props2 = json.fromJson(json.toJson(props), Properties.class);
assertEquals(props2, props);
assertEquals(json.toJson(props2), json.toJson(props));
}
代码示例来源:origin: jclouds/legacy-jclouds
public void testMapStringObjectWithBooleanKeysConvertToStrings() {
Map<String, Object> map = ImmutableMap.<String, Object> of("map", ImmutableMap.of(true, "value"));
assertEquals(json.toJson(map), "{\"map\":{\"true\":\"value\"}}");
Map<String, Object> map2 = json.fromJson(json.toJson(map), new TypeLiteral<Map<String, Object>>() {
}.getType());
// note conversion.. ensures valid
assertEquals(map2, ImmutableMap.<String, Object> of("map", ImmutableMap.of("true", "value")));
assertEquals(json.toJson(map2), json.toJson(map));
}
代码示例来源:origin: jclouds/legacy-jclouds
public void testMapStringObjectWithNumericalKeysConvertToStrings() {
Map<String, Object> map = ImmutableMap.<String, Object> of("map", ImmutableMap.of(1, "value"));
assertEquals(json.toJson(map), "{\"map\":{\"1\":\"value\"}}");
Map<String, Object> map2 = json.fromJson(json.toJson(map), new TypeLiteral<Map<String, Object>>() {
}.getType());
// note conversion.. ensures valid
assertEquals(map2, ImmutableMap.<String, Object> of("map", ImmutableMap.of("1", "value")));
assertEquals(json.toJson(map2), json.toJson(map));
}
代码示例来源:origin: jclouds/legacy-jclouds
public void testByteList() {
ByteList bl = new ByteList();
bl.checksum = asList(base16().lowerCase().decode("1dda05ed139664f1f89b9dec482b77c0"));
assertEquals(json.toJson(bl), "{\"checksum\":\"1dda05ed139664f1f89b9dec482b77c0\"}");
assertEquals(json.fromJson(json.toJson(bl), ByteList.class).checksum, bl.checksum);
}
代码示例来源:origin: jclouds/legacy-jclouds
public void testObjectNoDefaultConstructor() {
ObjectNoDefaultConstructor obj = new ObjectNoDefaultConstructor("foo", 1);
assertEquals(json.toJson(obj), "{\"stringValue\":\"foo\",\"intValue\":1}");
ObjectNoDefaultConstructor obj2 = json.fromJson(json.toJson(obj), ObjectNoDefaultConstructor.class);
assertEquals(obj2, obj);
assertEquals(json.toJson(obj2), json.toJson(obj));
}
内容来源于网络,如有侵权,请联系作者删除!