Refine search
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}]}");
public void testList() { String json = "{\"list\":[8431,8433]}"; // gson deserialized numbers to double, so integers end up changed to fractions assertEquals(handler.apply(HttpResponse.builder().statusCode(200).message("ok").payload(json).build()), ImmutableMap.<String, Object> of("list", ImmutableList.of(8431d, 8433d))); assertEquals(mapper.toJson(ImmutableMap.<String, Object> of("list", ImmutableList.of(8431, 8433))), json); }
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)); }
@SuppressWarnings("unchecked") @Override public <R extends HttpRequest> R bindToRequest(R request, Object arg) { CreateRecord<?> in = CreateRecord.class.cast(checkNotNull(arg, "record to create")); URI path = uriBuilder(request.getEndpoint()) .build(ImmutableMap.<String, Object> builder() .put("type", in.getType()) .put("fqdn", in.getFQDN()).build()); return (R) request.toBuilder() .endpoint(path) .payload(json.toJson(ImmutableMap.of("rdata", in.getRData(), "ttl", in.getTTL()))).build(); } }
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 test1() { String json = "{\"my_key\":\"my_data\",\"id\":\"item1\"}"; DatabagItem item = new DatabagItem("item1", json); assertEquals(handler.apply(HttpResponse.builder().statusCode(200).message("ok").payload(json).build()), item); assertEquals(mapper.toJson(item), json); } }
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 testExcluder() { Json excluder = Guice.createInjector(new GsonModule(), new AbstractModule() { protected void configure() { bind(DefaultExclusionStrategy.class).to(ExcludeStringValue.class); } }).getInstance(Json.class); ObjectNoDefaultConstructor obj = new ObjectNoDefaultConstructor("foo", 1); assertEquals(excluder.toJson(obj), "{\"intValue\":1}"); }
public void testDeserializeEnumWithParser() { assertEquals(json.fromJson("{enumValue : \"FOO\"}", EnumInsideWithParser.class).enumValue, EnumInsideWithParser.Test.FOO); }
.method("GET").endpoint(baseEndpointUri) .addHeader(VERSION_NEGOTIATION_HEADER, "true").build(); InputStream response = client.invoke(negotiationRequest).getPayload().openStream(); VersionsJsonResponse versions = json.fromJson(Strings2.toStringAndClose(response), VersionsJsonResponse.class); for (VersionsJsonResponse.Version version : versions.versions) { if (apiVersion.equals(version.id)) {
@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(); } } }
Hashtable<String, String> hashTable = new Hashtable<String, String>(); Json json = new Json(); hashTable.put("test", json.toJson(ints) ); //here you are serializing the array ... //putting the map into preferences String serializedInts = Gdx.app.getPreferences("preferences").getString("test"); int[] deserializedInts = json.fromJson(int[].class, serializedInts); //you need to pass the class type - be aware of it!
@Override public String apply(Object input) { String encodedHeader = String.format("{\"alg\":\"%s\",\"typ\":\"JWT\"}", alg); String encodedClaimSet = json.toJson(input); encodedHeader = base64Url().omitPadding().encode(encodedHeader.getBytes(UTF_8)); encodedClaimSet = base64Url().omitPadding().encode(encodedClaimSet.getBytes(UTF_8)); byte[] signature = alg.equals("none") ? null : sha256(privateKey.get(), on(".").join(encodedHeader, encodedClaimSet).getBytes(UTF_8)); String encodedSignature = signature != null ? base64Url().omitPadding().encode(signature) : ""; // the final assertion in base 64 encoded {header}.{claimSet}.{signature} format return on(".").join(encodedHeader, encodedClaimSet, encodedSignature); }
Json json = new Json(); json.setTypeName(null); json.setUsePrototypes(false); json.setIgnoreUnknownFields(true); json.setOutputType(OutputType.json); json.toJson(config, GameConfig.class);
public void testHash() { String json = "{\"tomcat6\":{\"ssl_port\":8433}}"; // gson deserialized numbers to double, so integers end up changed to fractions assertEquals(handler.apply(HttpResponse.builder().statusCode(200).message("ok").payload(json).build()), ImmutableMap.<String, Object> of("tomcat6", ImmutableMap.of("ssl_port", 8433d))); assertEquals(mapper.toJson(ImmutableMap.<String, Object> of("tomcat6", ImmutableMap.of("ssl_port", 8433))), json); }
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)); }