From f2d258a20561231260cb2d3021e03fe7151488c5 Mon Sep 17 00:00:00 2001 From: Javier Date: Mon, 3 Dec 2018 21:33:53 +0100 Subject: [PATCH] BAEL-2412 Working with primitive values in Gson. --- .../fromJsonPrimitiveTypes.java | 17 +++++++++++ .../toJsonPrimitiveTypes.java | 13 +++++++++ ...JsonIntegerRepresentationBooleanValue.java | 17 +++++++++++ ...nInvalidValueWithinStringBooleanValue.java | 16 +++++++++++ .../JsonYesRepresentationBooleanValue.java | 16 +++++++++++ .../byte_value/ToJsonBitString.java | 27 ++++++++++++++++++ .../byte_value/fromJsonBitString.java | 28 +++++++++++++++++++ .../char_value/JsonUnicodeCharValue.java | 16 +++++++++++ .../models/GsonBitString.java | 9 ++++++ .../models/GsonBoolean.java | 9 ++++++ .../gsonprimitivetypes/models/GsonBundle.java | 19 +++++++++++++ .../gsonprimitivetypes/models/GsonFloat.java | 9 ++++++ .../models/GsonLatinChar.java | 9 ++++++ .../JsonNonCompatibleNumberTypeValue.java | 15 ++++++++++ .../number_value/JsonOverflowValue.java | 15 ++++++++++ .../JsonPrecissionMismatchValue.java | 15 ++++++++++ .../special_value/JsonEmptyValue.java | 15 ++++++++++ .../special_value/JsonInvalidStringValue.java | 15 ++++++++++ .../special_value/JsonInvalidValue.java | 15 ++++++++++ .../special_value/JsonNullValue.java | 15 ++++++++++ .../special_value/JsonValidStringValue.java | 15 ++++++++++ 21 files changed, 325 insertions(+) create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/all_primitive_values/fromJsonPrimitiveTypes.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/all_primitive_values/toJsonPrimitiveTypes.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonIntegerRepresentationBooleanValue.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonInvalidValueWithinStringBooleanValue.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonYesRepresentationBooleanValue.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/byte_value/ToJsonBitString.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/byte_value/fromJsonBitString.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/char_value/JsonUnicodeCharValue.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBitString.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBoolean.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBundle.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonFloat.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonLatinChar.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonNonCompatibleNumberTypeValue.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonOverflowValue.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonPrecissionMismatchValue.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonEmptyValue.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonInvalidStringValue.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonInvalidValue.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonNullValue.java create mode 100644 gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonValidStringValue.java diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/all_primitive_values/fromJsonPrimitiveTypes.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/all_primitive_values/fromJsonPrimitiveTypes.java new file mode 100644 index 0000000000..259a9ca1ce --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/all_primitive_values/fromJsonPrimitiveTypes.java @@ -0,0 +1,17 @@ +package com.baeldung.gson_primitive_types.all_primitive_values; + +import com.baeldung.gson_primitive_types.models.GsonBundle; +import com.google.gson.Gson; + +public class fromJsonPrimitiveTypes { + public static void main(String[] args) { + String json = "{\"value\": 17, \"shortValue\": 3, \"intValue\": 3, " + + "\"longValue\": 3, \"floatValue\": 3.5" + ", \"doubleValue\": 3.5" + + ", \"booleanValue\": true, \"charValue\": \"a\"}"; + + Gson gson = new Gson(); + GsonBundle gsonBundle = gson.fromJson(json, GsonBundle.class); + + System.out.println(gsonBundle); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/all_primitive_values/toJsonPrimitiveTypes.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/all_primitive_values/toJsonPrimitiveTypes.java new file mode 100644 index 0000000000..a55ba85e4f --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/all_primitive_values/toJsonPrimitiveTypes.java @@ -0,0 +1,13 @@ +package com.baeldung.gson_primitive_types.all_primitive_values; + +import com.baeldung.gson_primitive_types.models.GsonBundle; +import com.google.gson.Gson; + +public class toJsonPrimitiveTypes { + public static void main(String[] args) { + GsonBundle gsonBundle = new GsonBundle(); + Gson gson = new Gson(); + + System.out.println(gson.toJson(gsonBundle)); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonIntegerRepresentationBooleanValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonIntegerRepresentationBooleanValue.java new file mode 100644 index 0000000000..5a90e39fce --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonIntegerRepresentationBooleanValue.java @@ -0,0 +1,17 @@ +package com.baeldung.gson_primitive_types.boolean_value; + +import com.baeldung.gson_primitive_types.models.GsonBoolean; +import com.baeldung.gson_primitive_types.models.GsonFloat; +import com.google.gson.Gson; + +public class JsonIntegerRepresentationBooleanValue { + public static void main(String[] args) { + // Raises exception. + String json = "{\"value\": 1}"; + Gson gson = new Gson(); + + GsonBoolean model = gson.fromJson(json, GsonBoolean.class); + + System.out.println(model); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonInvalidValueWithinStringBooleanValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonInvalidValueWithinStringBooleanValue.java new file mode 100644 index 0000000000..58fbed6a8f --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonInvalidValueWithinStringBooleanValue.java @@ -0,0 +1,16 @@ +package com.baeldung.gson_primitive_types.boolean_value; + +import com.baeldung.gson_primitive_types.models.GsonBoolean; +import com.google.gson.Gson; + +public class JsonInvalidValueWithinStringBooleanValue { + public static void main(String[] args) { + // It is ignored. + String json = "{\"value\": \"15x\"}"; + Gson gson = new Gson(); + + GsonBoolean model = gson.fromJson(json, GsonBoolean.class); + + System.out.println(model); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonYesRepresentationBooleanValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonYesRepresentationBooleanValue.java new file mode 100644 index 0000000000..f6061c1cea --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonYesRepresentationBooleanValue.java @@ -0,0 +1,16 @@ +package com.baeldung.gson_primitive_types.boolean_value; + +import com.baeldung.gson_primitive_types.models.GsonBoolean; +import com.google.gson.Gson; + +public class JsonYesRepresentationBooleanValue { + public static void main(String[] args) { + // It fails silently. + String json = "{\"value\": yes}"; + Gson gson = new Gson(); + + GsonBoolean model = gson.fromJson(json, GsonBoolean.class); + + System.out.println(model); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/byte_value/ToJsonBitString.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/byte_value/ToJsonBitString.java new file mode 100644 index 0000000000..f9b8045d6c --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/byte_value/ToJsonBitString.java @@ -0,0 +1,27 @@ +package com.baeldung.gson_primitive_types.byte_value; + +import com.baeldung.gson_primitive_types.models.GsonBitString; +import com.google.gson.*; + +import java.lang.reflect.Type; + +public class ToJsonBitString { + public static void main(String[] args) { + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(GsonBitString.class, new GsonBitStringSerializer()); + + Gson gson = builder.create(); + GsonBitString model = new GsonBitString(); + model.value = (byte) 0b1111; + + System.out.println(gson.toJson(model)); + } + + static class GsonBitStringSerializer implements JsonSerializer { + @Override public JsonElement serialize(GsonBitString gsonBundle, Type type, JsonSerializationContext jsonSerializationContext) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("value", Integer.toBinaryString(gsonBundle.value)); + return jsonObject; + } + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/byte_value/fromJsonBitString.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/byte_value/fromJsonBitString.java new file mode 100644 index 0000000000..4bd404d811 --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/byte_value/fromJsonBitString.java @@ -0,0 +1,28 @@ +package com.baeldung.gson_primitive_types.byte_value; + +import com.baeldung.gson_primitive_types.models.GsonBitString; +import com.google.gson.*; + +import java.lang.reflect.Type; + +public class fromJsonBitString { + public static void main(String[] args) { + String json = "{\"value\": \"1111\"}"; + GsonBuilder gsonBuilder = new GsonBuilder(); + gsonBuilder.registerTypeAdapter(GsonBitString.class, new GsonBitStringDeserializer()); + + Gson gson = gsonBuilder.create(); + + System.out.println(gson.fromJson(json, GsonBitString.class)); + } + + static class GsonBitStringDeserializer implements JsonDeserializer { + @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); + return gsonBitString; + } + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/char_value/JsonUnicodeCharValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/char_value/JsonUnicodeCharValue.java new file mode 100644 index 0000000000..d98fd823d1 --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/char_value/JsonUnicodeCharValue.java @@ -0,0 +1,16 @@ +package com.baeldung.gson_primitive_types.char_value; + +import com.baeldung.gson_primitive_types.models.GsonBitString; +import com.baeldung.gson_primitive_types.models.GsonLatinChar; +import com.google.gson.Gson; + +public class JsonUnicodeCharValue { + public static void main(String[] args) { + // The field is converted. + Gson gson = new Gson(); + String json = "{\"value\": \"\\u00AE\"}"; + GsonLatinChar model = gson.fromJson(json, GsonLatinChar.class); + + System.out.println(model); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBitString.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBitString.java new file mode 100644 index 0000000000..7598b718be --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBitString.java @@ -0,0 +1,9 @@ +package com.baeldung.gson_primitive_types.models; + +public class GsonBitString { + public byte value = (byte) 1; + + public String toString() { + return "{byte: " + value + "}"; + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBoolean.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBoolean.java new file mode 100644 index 0000000000..ce54276228 --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBoolean.java @@ -0,0 +1,9 @@ +package com.baeldung.gson_primitive_types.models; + +public class GsonBoolean { + public boolean value; + + public String toString() { + return "{boolean: " + value + "}"; + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBundle.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBundle.java new file mode 100644 index 0000000000..51c68198ca --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBundle.java @@ -0,0 +1,19 @@ +package com.baeldung.gson_primitive_types.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 String toString() { + return "{" + "byte: " + byteValue + ", " + "short: " + shortValue + ", " + + "int: " + intValue + ", " + "long: " + longValue + ", " + "float: " + + floatValue + ", " + "double: " + doubleValue + ", " + "boolean: " + + booleanValue + ", " + "char: " + charValue + "}"; + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonFloat.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonFloat.java new file mode 100644 index 0000000000..bf4abf9fbb --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonFloat.java @@ -0,0 +1,9 @@ +package com.baeldung.gson_primitive_types.models; + +public class GsonFloat { + public float value; + + public String toString() { + return "{float: " + value + "}"; + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonLatinChar.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonLatinChar.java new file mode 100644 index 0000000000..73b73f6bb4 --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonLatinChar.java @@ -0,0 +1,9 @@ +package com.baeldung.gson_primitive_types.models; + +public class GsonLatinChar { + public char value; + + public String toString() { + return "{char: " + value + "}"; + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonNonCompatibleNumberTypeValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonNonCompatibleNumberTypeValue.java new file mode 100644 index 0000000000..848248fd77 --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonNonCompatibleNumberTypeValue.java @@ -0,0 +1,15 @@ +package com.baeldung.gson_primitive_types.number_value; + +import com.baeldung.gson_primitive_types.models.GsonBitString; +import com.google.gson.Gson; + +public class JsonNonCompatibleNumberTypeValue { + public static void main(String[] args) { + // Raises an exception. + Gson gson = new Gson(); + String json = "{\"value\": 2.3}"; + GsonBitString model = gson.fromJson(json, GsonBitString.class); + + System.out.println(model); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonOverflowValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonOverflowValue.java new file mode 100644 index 0000000000..6b63ee791e --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonOverflowValue.java @@ -0,0 +1,15 @@ +package com.baeldung.gson_primitive_types.number_value; + +import com.baeldung.gson_primitive_types.models.GsonBitString; +import com.google.gson.Gson; + +public class JsonOverflowValue { + public static void main(String[] args) { + // Overflow happens unnoticed. + Gson gson = new Gson(); + String json = "{\"value\": \"300\"}"; + GsonBitString model = gson.fromJson(json, GsonBitString.class); + + System.out.println(model); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonPrecissionMismatchValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonPrecissionMismatchValue.java new file mode 100644 index 0000000000..6abe7101ee --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonPrecissionMismatchValue.java @@ -0,0 +1,15 @@ +package com.baeldung.gson_primitive_types.number_value; + +import com.baeldung.gson_primitive_types.models.GsonFloat; +import com.google.gson.Gson; + +public class JsonPrecissionMismatchValue { + public static void main(String[] args) { + String json = "{\"value\": 12.123456789123456}"; + Gson gson = new Gson(); + + GsonFloat model = gson.fromJson(json, GsonFloat.class); + + System.out.println(model); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonEmptyValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonEmptyValue.java new file mode 100644 index 0000000000..401e355efb --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonEmptyValue.java @@ -0,0 +1,15 @@ +package com.baeldung.gson_primitive_types.special_value; + +import com.baeldung.gson_primitive_types.models.GsonBitString; +import com.google.gson.Gson; + +public class JsonEmptyValue { + public static void main(String[] args) { + // Raises an exception. + Gson gson = new Gson(); + String json = "{\"value\": \"\"}"; + GsonBitString model = gson.fromJson(json, GsonBitString.class); + + System.out.println(model); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonInvalidStringValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonInvalidStringValue.java new file mode 100644 index 0000000000..3100d0b939 --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonInvalidStringValue.java @@ -0,0 +1,15 @@ +package com.baeldung.gson_primitive_types.special_value; + +import com.baeldung.gson_primitive_types.models.GsonBitString; +import com.google.gson.Gson; + +public class JsonInvalidStringValue { + public static void main(String[] args) { + // Raises an exception. + Gson gson = new Gson(); + String json = "{\"value\": \"15x\"}"; + GsonBitString model = gson.fromJson(json, GsonBitString.class); + + System.out.println(model); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonInvalidValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonInvalidValue.java new file mode 100644 index 0000000000..cfa88258d4 --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonInvalidValue.java @@ -0,0 +1,15 @@ +package com.baeldung.gson_primitive_types.special_value; + +import com.baeldung.gson_primitive_types.models.GsonBitString; +import com.google.gson.Gson; + +public class JsonInvalidValue { + public static void main(String[] args) { + // Raises an exception. + Gson gson = new Gson(); + String json = "{\"value\": s15s}"; + GsonBitString model = gson.fromJson(json, GsonBitString.class); + + System.out.println(model); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonNullValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonNullValue.java new file mode 100644 index 0000000000..075a029f5e --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonNullValue.java @@ -0,0 +1,15 @@ +package com.baeldung.gson_primitive_types.special_value; + +import com.baeldung.gson_primitive_types.models.GsonBitString; +import com.google.gson.Gson; + +public class JsonNullValue { + public static void main(String[] args) { + // The field will just be ignored. + Gson gson = new Gson(); + String json = "{\"value\": null}"; + GsonBitString model = gson.fromJson(json, GsonBitString.class); + + System.out.println(model); + } +} diff --git a/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonValidStringValue.java b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonValidStringValue.java new file mode 100644 index 0000000000..8b13a2b84d --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonValidStringValue.java @@ -0,0 +1,15 @@ +package com.baeldung.gson_primitive_types.special_value; + +import com.baeldung.gson_primitive_types.models.GsonBitString; +import com.google.gson.Gson; + +public class JsonValidStringValue { + public static void main(String[] args) { + // The field is converted. + Gson gson = new Gson(); + String json = "{\"value\": \"15\"}"; + GsonBitString model = gson.fromJson(json, GsonBitString.class); + + System.out.println(model); + } +}