From 19ed35aef4979947448d0d13b50add52db428069 Mon Sep 17 00:00:00 2001 From: Javier Date: Thu, 6 Dec 2018 10:13:20 +0100 Subject: [PATCH] BAEL-2412 Change name of a class. Move class init code to tests. Add solution to deserializing a boolean from an 2-value integer. Change formatting for long sentences, mainly the serializers/deserializers. --- .../{GsonBundle.java => PrimitiveBundle.java} | 18 ++-- .../baeldung/gson/primitives/UnitTest.java | 87 ++++++++++++++++--- 2 files changed, 83 insertions(+), 22 deletions(-) rename gson/src/main/java/org/baeldung/gson/primitives/models/{GsonBundle.java => PrimitiveBundle.java} (55%) diff --git a/gson/src/main/java/org/baeldung/gson/primitives/models/GsonBundle.java b/gson/src/main/java/org/baeldung/gson/primitives/models/PrimitiveBundle.java similarity index 55% rename from gson/src/main/java/org/baeldung/gson/primitives/models/GsonBundle.java rename to gson/src/main/java/org/baeldung/gson/primitives/models/PrimitiveBundle.java index 1fff9a8a29..30bc4d0d9f 100644 --- a/gson/src/main/java/org/baeldung/gson/primitives/models/GsonBundle.java +++ b/gson/src/main/java/org/baeldung/gson/primitives/models/PrimitiveBundle.java @@ -1,14 +1,14 @@ package org.baeldung.gson.primitives.models; -public class GsonBundle { - public byte byteValue = (byte) 0x00001111; - public short shortValue = (short) 3; - public int intValue = 3; - public long longValue = 3; - public float floatValue = 3.5f; - public double doubleValue = 3.5; - public boolean booleanValue = true; - public char charValue = 'a'; +public class PrimitiveBundle { + public byte byteValue; + public short shortValue; + public int intValue; + public long longValue; + public float floatValue; + public double doubleValue; + public boolean booleanValue; + public char charValue; public String toString() { return "{" + "byte: " + byteValue + ", " + "short: " + shortValue + ", " diff --git a/gson/src/test/java/org/baeldung/gson/primitives/UnitTest.java b/gson/src/test/java/org/baeldung/gson/primitives/UnitTest.java index da91f02447..890e6eefd7 100644 --- a/gson/src/test/java/org/baeldung/gson/primitives/UnitTest.java +++ b/gson/src/test/java/org/baeldung/gson/primitives/UnitTest.java @@ -10,28 +10,42 @@ import static junit.framework.TestCase.*; public class UnitTest { @Test public void toJsonAllPrimitives() { - GsonBundle gsonBundle = new GsonBundle(); + PrimitiveBundle primitiveBundle = new PrimitiveBundle(); + + // @formatter:off + primitiveBundle.byteValue = (byte) 0x00001111; + primitiveBundle.shortValue = (short) 3; + primitiveBundle.intValue = 3; + primitiveBundle.longValue = 3; + primitiveBundle.floatValue = 3.5f; + primitiveBundle.doubleValue = 3.5; + primitiveBundle.booleanValue = true; + primitiveBundle.charValue = 'a'; + // @formatter:on + Gson gson = new Gson(); String expected = "{\"byteValue\":17,\"shortValue\":3,\"intValue\":3," + "\"longValue\":3,\"floatValue\":3.5" + ",\"doubleValue\":3.5" + ",\"booleanValue\":true,\"charValue\":\"a\"}"; - assertEquals(expected, gson.toJson(gsonBundle)); + assertEquals(expected, gson.toJson(primitiveBundle)); } @Test public void fromJsonAllPrimitives() { String json = "{\"byteValue\": 17, \"shortValue\": 3, \"intValue\": 3, " + "\"longValue\": 3, \"floatValue\": 3.5" + ", \"doubleValue\": 3.5" + ", \"booleanValue\": true, \"charValue\": \"a\"}"; Gson gson = new Gson(); - GsonBundle model = gson.fromJson(json, GsonBundle.class); + PrimitiveBundle model = gson.fromJson(json, PrimitiveBundle.class); - assertEquals(17, model.byteValue); - assertEquals(3, model.shortValue); - assertEquals(3, model.intValue); - assertEquals(3, model.longValue); + // @formatter:off + assertEquals(17, model.byteValue); + assertEquals(3, model.shortValue); + assertEquals(3, model.intValue); + assertEquals(3, model.longValue); assertEquals(3.5, model.floatValue, 0.0001); assertEquals(3.5, model.doubleValue, 0.0001); - assertTrue(model.booleanValue); + assertTrue( model.booleanValue); assertEquals('a', model.charValue); + // @formatter:on } @Test public void toJsonByteToBitString() { @@ -143,7 +157,19 @@ public class UnitTest { fail(); } - @Test public void fromJsonBooleanfromYes() { + @Test public void fromJsonBooleanFrom2ValueIntegerSolution() { + String json = "{\"value\": 1}"; + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(GsonBoolean.class, new GsonBoolean2ValueIntegerDeserializer()); + + Gson gson = builder.create(); + + GsonBoolean model = gson.fromJson(json, GsonBoolean.class); + + assertTrue(model.value); + } + + @Test public void fromJsonBooleanFromYes() { String json = "{\"value\": yes}"; Gson gson = new Gson(); @@ -162,19 +188,54 @@ public class UnitTest { assertFalse(model.value); } + // @formatter:off static class GsonBitStringDeserializer implements JsonDeserializer { - @Override public GsonBitString deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { + @Override public GsonBitString deserialize( + JsonElement jsonElement, + Type type, + JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { + GsonBitString gsonBitString = new GsonBitString(); - gsonBitString.value = (byte) Integer.parseInt(jsonElement.getAsJsonObject().getAsJsonPrimitive("value").getAsString(), 2); + gsonBitString.value = (byte) Integer.parseInt( + jsonElement.getAsJsonObject() + .getAsJsonPrimitive("value") + .getAsString() + , 2); return gsonBitString; } } static class GsonBitStringSerializer implements JsonSerializer { - @Override public JsonElement serialize(GsonBitString gsonBundle, Type type, JsonSerializationContext jsonSerializationContext) { + @Override public JsonElement serialize( + GsonBitString model, + Type type, + JsonSerializationContext jsonSerializationContext) { + JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("value", Integer.toBinaryString(gsonBundle.value)); + jsonObject.addProperty("value", Integer.toBinaryString(model.value)); return jsonObject; } } + + static class GsonBoolean2ValueIntegerDeserializer implements JsonDeserializer { + @Override public GsonBoolean deserialize( + JsonElement jsonElement, + Type type, + JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { + + GsonBoolean model = new GsonBoolean(); + int value = jsonElement.getAsJsonObject().getAsJsonPrimitive("value").getAsInt(); + if (value == 0) { + model.value = false; + } else if (value == 1) { + model.value = true; + } else { + throw new JsonParseException("Unexpected value. Trying to deserialize " + + "a boolean from an integer different than 0 and 1."); + } + + return model; + } + } + // @formatter:on }