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}]}");
Json json = new Json(); json.setTypeName(null); json.setUsePrototypes(false); json.setIgnoreUnknownFields(true); json.setOutputType(OutputType.json); json.toJson(config, GameConfig.class);
public void autoValueWithBuilderNested() { Json json = Guice.createInjector(new GsonModule()).getInstance(Json.class); SerializedNamesNestedWithBuilder resource = SerializedNamesNestedWithBuilder.builder().id("1234") .snwb(ImmutableList.of(SerializedNamesWithBuilder.builder().id("5678").number(1).build())).build(); String spinalJson = "{\"nested_id_name\":\"1234\",\"serialized_name_with_builder\":[{\"custom_id_name\":\"5678\",\"number\":1}]}"; assertEquals(json.toJson(resource), spinalJson); assertEquals(json.fromJson(spinalJson, SerializedNamesNestedWithBuilder.class), resource); }
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)); }
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 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)); }
@SerializedNames({"nested_id_name", "serialized_name_with_builder"}) private static SerializedNamesNestedWithBuilder create(String id, List<SerializedNamesWithBuilder> snwb) { return builder().id(id).snwb(snwb).build(); } }
@SerializedNames({"custom_id_name", "number"}) private static SerializedNamesWithBuilder create(String id, int number) { return builder().id(id).number(number).build(); } }
@SerializedNames({ "Item", "Items" }) private static NestedSerializedNamesType create(SerializedNamesType item, List<SerializedNamesType> items) { return new AutoValue_JsonTest_NestedSerializedNamesType(item, items); } }
@SerializedNames({ "Id", "Volumes" }) private static SerializedNamesType create(String id, Map<String, String> volumes) { return new AutoValue_JsonTest_SerializedNamesType(id, volumes); } }
@SerializedNames("Id") // TODO: check things like this with error-prone, not runtime! private static SerializedNamesTooFewType create(String id, Map<String, String> volumes) { return new AutoValue_JsonTest_SerializedNamesTooFewType(id, volumes); } }
@SerializedNames({ "foo" }) static ValueTypeWithFactoryMissingSerializedNames create(List<String> foo, Map<String, String> bar) { return null; } }
@SuppressWarnings("unused") public static Test fromValue(String state) { try { return valueOf(state); } catch (IllegalArgumentException e) { return UNRECOGNIZED; } } }
public static Builder builder() { return new AutoValue_JsonTest_SerializedNamesNestedWithBuilder.Builder(); } public abstract Builder toBuilder();
public static Builder builder() { return new AutoValue_JsonTest_SerializedNamesWithBuilder.Builder(); } public abstract Builder toBuilder();
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)); }
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 load() { //instead of this line: //buttons = new Array<Button>(); Json json = new Json(); json.setTypeName(null); json.setUsePrototypes(false); json.setIgnoreUnknownFields(true); json.setOutputType(JsonWriter.OutputType.json); // set buttons here: buttons = json.fromJson(Data.class, Gdx.files.internal("buttons.json")); }
@SuppressWarnings("unused") public static Test fromValue(String state) { try { return valueOf(state); } catch (IllegalArgumentException e) { return UNRECOGNIZED; } } }