BAEL-2412

Working with primitive values in Gson.
This commit is contained in:
Javier 2018-12-03 21:33:53 +01:00
parent ab821cb4e5
commit f2d258a205
21 changed files with 325 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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<GsonBitString> {
@Override public JsonElement serialize(GsonBitString gsonBundle, Type type, JsonSerializationContext jsonSerializationContext) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("value", Integer.toBinaryString(gsonBundle.value));
return jsonObject;
}
}
}

View File

@ -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<GsonBitString> {
@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;
}
}
}

View File

@ -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);
}
}

View File

@ -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 + "}";
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.gson_primitive_types.models;
public class GsonBoolean {
public boolean value;
public String toString() {
return "{boolean: " + value + "}";
}
}

View File

@ -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 + "}";
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.gson_primitive_types.models;
public class GsonFloat {
public float value;
public String toString() {
return "{float: " + value + "}";
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.gson_primitive_types.models;
public class GsonLatinChar {
public char value;
public String toString() {
return "{char: " + value + "}";
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}