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