|
|
|
@ -50,10 +50,10 @@ public class UnitTest {
|
|
|
|
|
|
|
|
|
|
@Test public void toJsonByteToBitString() {
|
|
|
|
|
GsonBuilder builder = new GsonBuilder();
|
|
|
|
|
builder.registerTypeAdapter(GsonBitString.class, new GsonBitStringSerializer());
|
|
|
|
|
builder.registerTypeAdapter(ByteExample.class, new GsonBitStringSerializer());
|
|
|
|
|
|
|
|
|
|
Gson gson = builder.create();
|
|
|
|
|
GsonBitString model = new GsonBitString();
|
|
|
|
|
ByteExample model = new ByteExample();
|
|
|
|
|
model.value = (byte) 0b1111;
|
|
|
|
|
|
|
|
|
|
assertEquals("{\"value\":\"1111\"}", gson.toJson(model));
|
|
|
|
@ -62,11 +62,11 @@ public class UnitTest {
|
|
|
|
|
@Test public void fromJsonByteFromBitString() {
|
|
|
|
|
String json = "{\"value\": \"1111\"}";
|
|
|
|
|
GsonBuilder gsonBuilder = new GsonBuilder();
|
|
|
|
|
gsonBuilder.registerTypeAdapter(GsonBitString.class, new GsonBitStringDeserializer());
|
|
|
|
|
gsonBuilder.registerTypeAdapter(ByteExample.class, new GsonBitStringDeserializer());
|
|
|
|
|
|
|
|
|
|
Gson gson = gsonBuilder.create();
|
|
|
|
|
|
|
|
|
|
GsonBitString model = gson.fromJson(json, GsonBitString.class);
|
|
|
|
|
ByteExample model = gson.fromJson(json, ByteExample.class);
|
|
|
|
|
|
|
|
|
|
assertEquals(15, model.value);
|
|
|
|
|
}
|
|
|
|
@ -74,14 +74,14 @@ public class UnitTest {
|
|
|
|
|
@Test public void fromJsonPrecissionMismatch() {
|
|
|
|
|
String json = "{\"value\": 12.123456789123456}";
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
GsonFloat model = gson.fromJson(json, GsonFloat.class);
|
|
|
|
|
FloatExample model = gson.fromJson(json, FloatExample.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);
|
|
|
|
|
ByteExample model = gson.fromJson(json, ByteExample.class);
|
|
|
|
|
|
|
|
|
|
assertEquals(44, model.value);
|
|
|
|
|
}
|
|
|
|
@ -90,7 +90,7 @@ public class UnitTest {
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
String json = "{\"value\": 2.3}";
|
|
|
|
|
try {
|
|
|
|
|
gson.fromJson(json, GsonBitString.class);
|
|
|
|
|
gson.fromJson(json, ByteExample.class);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
assertTrue(ex instanceof JsonSyntaxException);
|
|
|
|
|
assertTrue(ex.getCause() instanceof NumberFormatException);
|
|
|
|
@ -103,7 +103,7 @@ public class UnitTest {
|
|
|
|
|
@Test public void fromJsonUnicodeChar() {
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
String json = "{\"value\": \"\\u00AE\"}";
|
|
|
|
|
GsonChar model = gson.fromJson(json, GsonChar.class);
|
|
|
|
|
CharExample model = gson.fromJson(json, CharExample.class);
|
|
|
|
|
|
|
|
|
|
assertEquals('\u00AE', model.value);
|
|
|
|
|
}
|
|
|
|
@ -111,7 +111,7 @@ public class UnitTest {
|
|
|
|
|
@Test public void fromJsonNull() {
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
String json = "{\"value\": null}";
|
|
|
|
|
GsonBitString model = gson.fromJson(json, GsonBitString.class);
|
|
|
|
|
ByteExample model = gson.fromJson(json, ByteExample.class);
|
|
|
|
|
|
|
|
|
|
assertEquals(1, model.value);
|
|
|
|
|
}
|
|
|
|
@ -119,13 +119,13 @@ public class UnitTest {
|
|
|
|
|
@Test(expected = JsonSyntaxException.class) public void fromJsonEmptyString() {
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
String json = "{\"value\": \"\"}";
|
|
|
|
|
gson.fromJson(json, GsonBitString.class);
|
|
|
|
|
gson.fromJson(json, ByteExample.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test public void fromJsonValidValueWithinString() {
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
String json = "{\"value\": \"15\"}";
|
|
|
|
|
GsonBitString model = gson.fromJson(json, GsonBitString.class);
|
|
|
|
|
ByteExample model = gson.fromJson(json, ByteExample.class);
|
|
|
|
|
|
|
|
|
|
assertEquals(15, model.value);
|
|
|
|
|
}
|
|
|
|
@ -133,13 +133,13 @@ public class UnitTest {
|
|
|
|
|
@Test(expected = JsonSyntaxException.class) public void fromJsonInvalidValueWithinString() {
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
String json = "{\"value\": \"15x\"}";
|
|
|
|
|
gson.fromJson(json, GsonBitString.class);
|
|
|
|
|
gson.fromJson(json, ByteExample.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected = JsonSyntaxException.class) public void fromJsonInvalidValueNotInAString() {
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
String json = "{\"value\": s15s}";
|
|
|
|
|
gson.fromJson(json, GsonBitString.class);
|
|
|
|
|
gson.fromJson(json, ByteExample.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test public void fromJsonBooleanFrom2ValueInteger() {
|
|
|
|
@ -147,7 +147,7 @@ public class UnitTest {
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
gson.fromJson(json, GsonBoolean.class);
|
|
|
|
|
gson.fromJson(json, BooleanExample.class);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
assertTrue(ex instanceof JsonSyntaxException);
|
|
|
|
|
assertTrue(ex.getCause() instanceof IllegalStateException);
|
|
|
|
@ -160,11 +160,11 @@ public class UnitTest {
|
|
|
|
|
@Test public void fromJsonBooleanFrom2ValueIntegerSolution() {
|
|
|
|
|
String json = "{\"value\": 1}";
|
|
|
|
|
GsonBuilder builder = new GsonBuilder();
|
|
|
|
|
builder.registerTypeAdapter(GsonBoolean.class, new GsonBoolean2ValueIntegerDeserializer());
|
|
|
|
|
builder.registerTypeAdapter(BooleanExample.class, new BooleanAs2ValueIntegerDeserializer());
|
|
|
|
|
|
|
|
|
|
Gson gson = builder.create();
|
|
|
|
|
|
|
|
|
|
GsonBoolean model = gson.fromJson(json, GsonBoolean.class);
|
|
|
|
|
BooleanExample model = gson.fromJson(json, BooleanExample.class);
|
|
|
|
|
|
|
|
|
|
assertTrue(model.value);
|
|
|
|
|
}
|
|
|
|
@ -173,7 +173,7 @@ public class UnitTest {
|
|
|
|
|
String json = "{\"value\": yes}";
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
|
|
|
|
|
GsonBoolean model = gson.fromJson(json, GsonBoolean.class);
|
|
|
|
|
BooleanExample model = gson.fromJson(json, BooleanExample.class);
|
|
|
|
|
|
|
|
|
|
// pay attention here that we are deserializing yes.
|
|
|
|
|
assertFalse(model.value);
|
|
|
|
@ -183,31 +183,31 @@ public class UnitTest {
|
|
|
|
|
String json = "{\"value\": \"15x\"}";
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
|
|
|
|
|
GsonBoolean model = gson.fromJson(json, GsonBoolean.class);
|
|
|
|
|
BooleanExample model = gson.fromJson(json, BooleanExample.class);
|
|
|
|
|
|
|
|
|
|
assertFalse(model.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @formatter:off
|
|
|
|
|
static class GsonBitStringDeserializer implements JsonDeserializer<GsonBitString> {
|
|
|
|
|
@Override public GsonBitString deserialize(
|
|
|
|
|
static class GsonBitStringDeserializer implements JsonDeserializer<ByteExample> {
|
|
|
|
|
@Override public ByteExample deserialize(
|
|
|
|
|
JsonElement jsonElement,
|
|
|
|
|
Type type,
|
|
|
|
|
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
|
|
|
|
|
|
|
|
|
GsonBitString gsonBitString = new GsonBitString();
|
|
|
|
|
gsonBitString.value = (byte) Integer.parseInt(
|
|
|
|
|
ByteExample byteExample = new ByteExample();
|
|
|
|
|
byteExample.value = (byte) Integer.parseInt(
|
|
|
|
|
jsonElement.getAsJsonObject()
|
|
|
|
|
.getAsJsonPrimitive("value")
|
|
|
|
|
.getAsString()
|
|
|
|
|
, 2);
|
|
|
|
|
return gsonBitString;
|
|
|
|
|
return byteExample;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class GsonBitStringSerializer implements JsonSerializer<GsonBitString> {
|
|
|
|
|
static class GsonBitStringSerializer implements JsonSerializer<ByteExample> {
|
|
|
|
|
@Override public JsonElement serialize(
|
|
|
|
|
GsonBitString model,
|
|
|
|
|
ByteExample model,
|
|
|
|
|
Type type,
|
|
|
|
|
JsonSerializationContext jsonSerializationContext) {
|
|
|
|
|
|
|
|
|
@ -217,13 +217,13 @@ public class UnitTest {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class GsonBoolean2ValueIntegerDeserializer implements JsonDeserializer<GsonBoolean> {
|
|
|
|
|
@Override public GsonBoolean deserialize(
|
|
|
|
|
static class BooleanAs2ValueIntegerDeserializer implements JsonDeserializer<BooleanExample> {
|
|
|
|
|
@Override public BooleanExample deserialize(
|
|
|
|
|
JsonElement jsonElement,
|
|
|
|
|
Type type,
|
|
|
|
|
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
|
|
|
|
|
|
|
|
|
GsonBoolean model = new GsonBoolean();
|
|
|
|
|
BooleanExample model = new BooleanExample();
|
|
|
|
|
int value = jsonElement.getAsJsonObject().getAsJsonPrimitive("value").getAsInt();
|
|
|
|
|
if (value == 0) {
|
|
|
|
|
model.value = false;
|
|
|
|
|