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}]}");
@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(); } } }
@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(); } } }
@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()); }
@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()); }
@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(); } } }
@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(); } } }
@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()); }
@Override public Map<String, Object> apply(String input) { try { return json.fromJson(input, new TypeLiteral<Map<String, Object>>() { }.getType()); } catch (Exception ex) { return null; } } };
@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); }
public void testDeserializeEnumWithParser() { assertEquals(json.fromJson("{enumValue : \"FOO\"}", EnumInsideWithParser.class).enumValue, EnumInsideWithParser.Test.FOO); }
public void testDeserializeEnumWithParserAndBadValue() { assertEquals(json.fromJson("{enumValue : \"sd\"}", EnumInsideWithParser.class).enumValue, EnumInsideWithParser.Test.UNRECOGNIZED); }
@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; } } }
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); }
public void testContainerWithVolumesNull() { Container container = json.fromJson("{ \"Id\": \"foo\", \"Volumes\": null }", Container.class); assertNotNull(container); assertEquals(container.id(), "foo"); assertEquals(container.volumes(), ImmutableMap.of()); }
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)); }
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)); }
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)); }
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); }
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)); }