parent
44e5a0b6a2
commit
4c0ce1d746
|
@ -1,17 +0,0 @@
|
|||
package org.baeldung.gson.primitives.allprimitives;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonBundle;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
package org.baeldung.gson.primitives.allprimitives;
|
||||
|
||||
import org.baeldung.gson.primitives.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));
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package org.baeldung.gson.primitives.booleanvalue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonBoolean;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.baeldung.gson.primitives.booleanvalue;
|
||||
|
||||
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();
|
||||
|
||||
org.baeldung.gson.primitives.models.GsonBoolean model = gson.fromJson(json, org.baeldung.gson.primitives.models.GsonBoolean.class);
|
||||
|
||||
System.out.println(model);
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package org.baeldung.gson.primitives.booleanvalue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonBoolean;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package org.baeldung.gson.primitives.bytevalue;
|
||||
|
||||
import com.google.gson.*;
|
||||
import org.baeldung.gson.primitives.models.GsonBitString;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package org.baeldung.gson.primitives.bytevalue;
|
||||
|
||||
import com.google.gson.*;
|
||||
import org.baeldung.gson.primitives.models.GsonBitString;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.baeldung.gson.primitives.charvalue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonChar;
|
||||
|
||||
public class JsonUnicodeCharValue {
|
||||
public static void main(String[] args) {
|
||||
// The field is converted.
|
||||
Gson gson = new Gson();
|
||||
String json = "{\"value\": \"\\u00AE\"}";
|
||||
GsonChar model = gson.fromJson(json, GsonChar.class);
|
||||
|
||||
System.out.println(model);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.baeldung.gson.primitives.numbervalue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonBitString;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.baeldung.gson.primitives.numbervalue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonBitString;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.baeldung.gson.primitives.numbervalue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonFloat;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.baeldung.gson.primitives.specialvalue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonBitString;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.baeldung.gson.primitives.specialvalue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonBitString;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.baeldung.gson.primitives.specialvalue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonBitString;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.baeldung.gson.primitives.specialvalue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonBitString;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.baeldung.gson.primitives.specialvalue;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.baeldung.gson.primitives.models.GsonBitString;
|
||||
|
||||
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,0 +1,180 @@
|
|||
package org.baeldung.gson.primitives;
|
||||
|
||||
import com.google.gson.*;
|
||||
import org.baeldung.gson.primitives.models.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import static junit.framework.TestCase.*;
|
||||
|
||||
public class UnitTest {
|
||||
@Test public void toJsonAllPrimitives() {
|
||||
GsonBundle gsonBundle = new GsonBundle();
|
||||
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));
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
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);
|
||||
assertEquals('a', model.charValue);
|
||||
}
|
||||
|
||||
@Test public void toJsonByteToBitString() {
|
||||
GsonBuilder builder = new GsonBuilder();
|
||||
builder.registerTypeAdapter(GsonBitString.class, new GsonBitStringSerializer());
|
||||
|
||||
Gson gson = builder.create();
|
||||
GsonBitString model = new GsonBitString();
|
||||
model.value = (byte) 0b1111;
|
||||
|
||||
assertEquals("{\"value\":\"1111\"}", gson.toJson(model));
|
||||
}
|
||||
|
||||
@Test public void fromJsonByteFromBitString() {
|
||||
String json = "{\"value\": \"1111\"}";
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.registerTypeAdapter(GsonBitString.class, new GsonBitStringDeserializer());
|
||||
|
||||
Gson gson = gsonBuilder.create();
|
||||
|
||||
GsonBitString model = gson.fromJson(json, GsonBitString.class);
|
||||
|
||||
assertEquals(15, model.value);
|
||||
}
|
||||
|
||||
@Test public void fromJsonPrecissionMismatch() {
|
||||
String json = "{\"value\": 12.123456789123456}";
|
||||
Gson gson = new Gson();
|
||||
GsonFloat model = gson.fromJson(json, GsonFloat.class);
|
||||
assertEquals(12.123457f, model.value, 0.000001);
|
||||
}
|
||||
|
||||
@Test public void fromJsonOverflow() {
|
||||
Gson gson = new Gson();
|
||||
String json = "{\"value\": \"300\"}";
|
||||
GsonBitString model = gson.fromJson(json, GsonBitString.class);
|
||||
|
||||
assertEquals(44, model.value);
|
||||
}
|
||||
|
||||
@Test public void fromJsonNonCompatibleNumberTypes() {
|
||||
Gson gson = new Gson();
|
||||
String json = "{\"value\": 2.3}";
|
||||
try {
|
||||
gson.fromJson(json, GsonBitString.class);
|
||||
} catch (Exception ex) {
|
||||
assertTrue(ex instanceof JsonSyntaxException);
|
||||
assertTrue(ex.getCause() instanceof NumberFormatException);
|
||||
return;
|
||||
}
|
||||
|
||||
fail();
|
||||
}
|
||||
|
||||
@Test public void fromJsonUnicodeChar() {
|
||||
Gson gson = new Gson();
|
||||
String json = "{\"value\": \"\\u00AE\"}";
|
||||
GsonChar model = gson.fromJson(json, GsonChar.class);
|
||||
|
||||
assertEquals('\u00AE', model.value);
|
||||
}
|
||||
|
||||
@Test public void fromJsonNull() {
|
||||
Gson gson = new Gson();
|
||||
String json = "{\"value\": null}";
|
||||
GsonBitString model = gson.fromJson(json, GsonBitString.class);
|
||||
|
||||
assertEquals(1, model.value);
|
||||
}
|
||||
|
||||
@Test(expected = JsonSyntaxException.class) public void fromJsonEmptyString() {
|
||||
Gson gson = new Gson();
|
||||
String json = "{\"value\": \"\"}";
|
||||
gson.fromJson(json, GsonBitString.class);
|
||||
}
|
||||
|
||||
@Test public void fromJsonValidValueWithinString() {
|
||||
Gson gson = new Gson();
|
||||
String json = "{\"value\": \"15\"}";
|
||||
GsonBitString model = gson.fromJson(json, GsonBitString.class);
|
||||
|
||||
assertEquals(15, model.value);
|
||||
}
|
||||
|
||||
@Test(expected = JsonSyntaxException.class) public void fromJsonInvalidValueWithinString() {
|
||||
Gson gson = new Gson();
|
||||
String json = "{\"value\": \"15x\"}";
|
||||
gson.fromJson(json, GsonBitString.class);
|
||||
}
|
||||
|
||||
@Test(expected = JsonSyntaxException.class) public void fromJsonInvalidValueNotInAString() {
|
||||
Gson gson = new Gson();
|
||||
String json = "{\"value\": s15s}";
|
||||
gson.fromJson(json, GsonBitString.class);
|
||||
}
|
||||
|
||||
@Test public void fromJsonBooleanFrom2ValueInteger() {
|
||||
String json = "{\"value\": 1}";
|
||||
Gson gson = new Gson();
|
||||
|
||||
try {
|
||||
gson.fromJson(json, GsonBoolean.class);
|
||||
} catch (Exception ex) {
|
||||
assertTrue(ex instanceof JsonSyntaxException);
|
||||
assertTrue(ex.getCause() instanceof IllegalStateException);
|
||||
return;
|
||||
}
|
||||
|
||||
fail();
|
||||
}
|
||||
|
||||
@Test public void fromJsonBooleanfromYes() {
|
||||
String json = "{\"value\": yes}";
|
||||
Gson gson = new Gson();
|
||||
|
||||
GsonBoolean model = gson.fromJson(json, GsonBoolean.class);
|
||||
|
||||
// pay attention here that we are deserializing yes.
|
||||
assertFalse(model.value);
|
||||
}
|
||||
|
||||
@Test public void fromJsonBooleanFromInvalidValue() {
|
||||
String json = "{\"value\": \"15x\"}";
|
||||
Gson gson = new Gson();
|
||||
|
||||
GsonBoolean model = gson.fromJson(json, GsonBoolean.class);
|
||||
|
||||
assertFalse(model.value);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue