Skip to content

Commit

Permalink
BAEL-2412
Browse files Browse the repository at this point in the history
Working with primitive values in Gson.
  • Loading branch information
javiermediavilla2 committed Dec 3, 2018
1 parent ab821cb commit f2d258a
Show file tree
Hide file tree
Showing 21 changed files with 325 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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 + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.gson_primitive_types.models;

public class GsonBoolean {
public boolean value;

public String toString() {
return "{boolean: " + value + "}";
}
}
Original file line number Diff line number Diff line change
@@ -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 + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.gson_primitive_types.models;

public class GsonFloat {
public float value;

public String toString() {
return "{float: " + value + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.gson_primitive_types.models;

public class GsonLatinChar {
public char value;

public String toString() {
return "{char: " + value + "}";
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit f2d258a

Please sign in to comment.