Package org.baeldung.gson.deserialization

Examples of org.baeldung.gson.deserialization.Foo


    @Test
    public final void whenDeserializingToSimpleObject_thenCorrect() {
        final String json = "{\"intValue\":1,\"stringValue\":\"one\"}";

        final Foo targetObject = new Gson().fromJson(json, Foo.class);

        assertEquals(targetObject.intValue, 1);
        assertEquals(targetObject.stringValue, "one");
    }
View Full Code Here


    }

    @Test
    public final void givenJsonHasExtraValues_whenDeserializing_thenCorrect() {
        final String json = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}";
        final Foo targetObject = new Gson().fromJson(json, Foo.class);

        assertEquals(targetObject.intValue, 1);
        assertEquals(targetObject.stringValue, "one");
    }
View Full Code Here

    public final void givenJsonHasNonMatchingFields_whenDeserializingWithCustomDeserializer_thenCorrect() {
        final String json = "{\"valueInt\":7,\"valueString\":\"seven\"}";

        final GsonBuilder gsonBldr = new GsonBuilder();
        gsonBldr.registerTypeAdapter(Foo.class, new FooDeserializerFromJsonWithDifferentFields());
        final Foo targetObject = gsonBldr.create().fromJson(json, Foo.class);

        assertEquals(targetObject.intValue, 7);
        assertEquals(targetObject.stringValue, "seven");
    }
View Full Code Here

    @Test
    public final void givenJsonArrayOfFoos_whenDeserializingToArray_thenCorrect() {
        final String json = "[{\"intValue\":1,\"stringValue\":\"one\"}," + "{\"intValue\":2,\"stringValue\":\"two\"}]";
        final Foo[] targetArray = new GsonBuilder().create().fromJson(json, Foo[].class);

        assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(1, "one")));
        assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(2, "two")));
        assertThat(Lists.newArrayList(targetArray), not(hasItem(new Foo(1, "two"))));
    }
View Full Code Here

        final JsonElement jElement = jParser.parse(jsonSourceObject);
        final JsonObject jObject = jElement.getAsJsonObject();
        final int intValue = jObject.get("valueInt").getAsInt();
        final String stringValue = jObject.get("valueString").getAsString();

        final Foo targetObject = new Foo(intValue, stringValue);

        assertEquals(targetObject.intValue, 7);
        assertEquals(targetObject.stringValue, "seven");
    }
View Full Code Here

TOP

Related Classes of org.baeldung.gson.deserialization.Foo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.