JsonCodecLoader experiment

Signed-off-by: slinkydeveloper <francescoguard@gmail.com>
This commit is contained in:
slinkydeveloper
2019-04-11 13:41:13 +02:00
committed by Julien Viet
parent 49f341609c
commit 5b83683c7d
14 changed files with 546 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
package io.vertx.core.json;
import io.vertx.core.buffer.Buffer;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.function.Function;
public class JsonCodecLoader {
Map<Class, JsonCodec> jsonCodecMap;
private JsonCodecLoader(Map<Class, JsonCodec> jsonCodecMap) {
this.jsonCodecMap = jsonCodecMap;
}
public static JsonCodecLoader loadCodecsFromSPI() {
Map<Class, JsonCodec> codecs = new HashMap<>();
ServiceLoader<JsonCodec> codecServiceLoader = ServiceLoader.load(JsonCodec.class);
for (JsonCodec j : codecServiceLoader) {
codecs.put(j.getTargetClass(), j);
}
return new JsonCodecLoader(codecs);
}
@SuppressWarnings("unchecked")
public <T> T decode(Buffer value, Class<T> c) {
Object json = value.toJson();
try {
return (T) jsonCodecMap.get(c).decode(json);
} catch (Exception e) {
throw new IllegalStateException("Unable to find codec for class " + c.getCanonicalName(), e);
}
}
@SuppressWarnings("unchecked")
public Buffer encode(Object value) {
try {
return Buffer.buffer(jsonCodecMap.get(value.getClass()).encode(value).toString());
} catch (Exception e) {
throw new IllegalStateException("Unable to find codec for class " + value.getClass().getCanonicalName(), e);
}
}
}

View File

@@ -16,4 +16,6 @@ public interface JsonDecoder<TARGET_TYPE, JSON_TYPE> {
* @throws IllegalArgumentException when it cannot decode the value
*/
TARGET_TYPE decode(JSON_TYPE value) throws IllegalArgumentException;
Class<TARGET_TYPE> getTargetClass();
}

View File

@@ -16,4 +16,6 @@ public interface JsonEncoder<TARGET_TYPE, JSON_TYPE> {
* @throws IllegalArgumentException when it cannot encode the value
*/
JSON_TYPE encode(TARGET_TYPE value) throws IllegalArgumentException;
Class<TARGET_TYPE> getTargetClass();
}

View File

@@ -0,0 +1,35 @@
package io.vertx.core.json;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.codecs.MyBooleanPojo;
import io.vertx.core.json.codecs.MyCharPojo;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class JsonCodecLoaderTest {
JsonCodecLoader jsonCodecLoader;
@Before
public void setUp() throws Exception {
this.jsonCodecLoader = JsonCodecLoader.loadCodecsFromSPI();
}
@Test
public void booleanCodecTest() {
MyBooleanPojo pojo = new MyBooleanPojo();
pojo.setValue(true);
assertEquals(Buffer.buffer("true"), jsonCodecLoader.encode(pojo));
assertEquals(pojo, jsonCodecLoader.decode(Buffer.buffer("true"), MyBooleanPojo.class));
}
@Test
public void charCodecTest() {
MyCharPojo pojo = new MyCharPojo();
pojo.setValue('a');
assertEquals('a', jsonCodecLoader.decode(Buffer.buffer(new byte[] {(byte)'a'}), MyCharPojo.class));
}
}

View File

@@ -0,0 +1,50 @@
package io.vertx.core.json.codecs;
import io.vertx.core.json.JsonCodec;
import java.util.Objects;
public class MyBooleanPojo {
public static class MyBooleanPojoJsonCodec implements JsonCodec<MyBooleanPojo, Boolean> {
@Override
public MyBooleanPojo decode(Boolean value) throws IllegalArgumentException {
return new MyBooleanPojo().setValue(value);
}
@Override
public Boolean encode(MyBooleanPojo value) throws IllegalArgumentException {
return value.isValue();
}
@Override
public Class<MyBooleanPojo> getTargetClass() {
return MyBooleanPojo.class;
}
}
boolean value;
public boolean isValue() {
return value;
}
public MyBooleanPojo setValue(boolean value) {
this.value = value;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyBooleanPojo that = (MyBooleanPojo) o;
return value == that.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}

View File

@@ -0,0 +1,50 @@
package io.vertx.core.json.codecs;
import io.vertx.core.json.JsonCodec;
import java.util.Objects;
public class MyCharPojo {
public static class MyCharPojoJsonCodec implements JsonCodec<MyCharPojo, Character> {
@Override
public MyCharPojo decode(Character value) throws IllegalArgumentException {
return new MyCharPojo().setValue(value);
}
@Override
public Character encode(MyCharPojo value) throws IllegalArgumentException {
return value.getValue();
}
@Override
public Class<MyCharPojo> getTargetClass() {
return MyCharPojo.class;
}
}
char value;
public char getValue() {
return value;
}
public MyCharPojo setValue(char value) {
this.value = value;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyCharPojo that = (MyCharPojo) o;
return value == that.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}

View File

@@ -0,0 +1,50 @@
package io.vertx.core.json.codecs;
import io.vertx.core.json.JsonCodec;
import java.util.Objects;
public class MyDoublePojo {
public static class MyDoublePojoJsonCodec implements JsonCodec<MyDoublePojo, Double> {
@Override
public MyDoublePojo decode(Double value) throws IllegalArgumentException {
return new MyDoublePojo().setValue(value);
}
@Override
public Double encode(MyDoublePojo value) throws IllegalArgumentException {
return value.getValue();
}
@Override
public Class<MyDoublePojo> getTargetClass() {
return MyDoublePojo.class;
}
}
double value;
public double getValue() {
return value;
}
public MyDoublePojo setValue(double value) {
this.value = value;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyDoublePojo that = (MyDoublePojo) o;
return value == that.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}

View File

@@ -0,0 +1,50 @@
package io.vertx.core.json.codecs;
import io.vertx.core.json.JsonCodec;
import java.util.Objects;
public class MyFloatPojo {
public static class MyFloatPojoJsonCodec implements JsonCodec<MyFloatPojo, Float> {
@Override
public MyFloatPojo decode(Float value) throws IllegalArgumentException {
return new MyFloatPojo().setValue(value);
}
@Override
public Float encode(MyFloatPojo value) throws IllegalArgumentException {
return value.getValue();
}
@Override
public Class<MyFloatPojo> getTargetClass() {
return MyFloatPojo.class;
}
}
float value;
public float getValue() {
return value;
}
public MyFloatPojo setValue(float value) {
this.value = value;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyFloatPojo that = (MyFloatPojo) o;
return value == that.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}

View File

@@ -0,0 +1,50 @@
package io.vertx.core.json.codecs;
import io.vertx.core.json.JsonCodec;
import java.util.Objects;
public class MyIntegerPojo {
public static class MyIntegerPojoJsonCodec implements JsonCodec<MyIntegerPojo, Integer> {
@Override
public MyIntegerPojo decode(Integer value) throws IllegalArgumentException {
return new MyIntegerPojo().setValue(value);
}
@Override
public Integer encode(MyIntegerPojo value) throws IllegalArgumentException {
return value.getValue();
}
@Override
public Class<MyIntegerPojo> getTargetClass() {
return MyIntegerPojo.class;
}
}
int value;
public int getValue() {
return value;
}
public MyIntegerPojo setValue(int value) {
this.value = value;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyIntegerPojo that = (MyIntegerPojo) o;
return value == that.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}

View File

@@ -0,0 +1,52 @@
package io.vertx.core.json.codecs;
import io.vertx.core.json.JsonCodec;
import io.vertx.core.json.JsonArray;
import java.util.Arrays;
import java.util.Objects;
public class MyJsonArrayPojo {
public static class MyJsonArrayPojoJsonCodec implements JsonCodec<MyJsonArrayPojo, JsonArray> {
@Override
public MyJsonArrayPojo decode(JsonArray value) throws IllegalArgumentException {
return new MyJsonArrayPojo().setValue(value);
}
@Override
public JsonArray encode(MyJsonArrayPojo value) throws IllegalArgumentException {
return value.getValue();
}
@Override
public Class<MyJsonArrayPojo> getTargetClass() {
return MyJsonArrayPojo.class;
}
}
JsonArray value;
public JsonArray getValue() {
return value;
}
public MyJsonArrayPojo setValue(JsonArray value) {
this.value = value;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyJsonArrayPojo that = (MyJsonArrayPojo) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}

View File

@@ -0,0 +1,51 @@
package io.vertx.core.json.codecs;
import io.vertx.core.json.JsonCodec;
import io.vertx.core.json.JsonObject;
import java.util.Objects;
public class MyJsonObjectPojo {
public static class MyJsonObjectPojoJsonCodec implements JsonCodec<MyJsonObjectPojo, JsonObject> {
@Override
public MyJsonObjectPojo decode(JsonObject value) throws IllegalArgumentException {
return new MyJsonObjectPojo().setValue(value);
}
@Override
public JsonObject encode(MyJsonObjectPojo value) throws IllegalArgumentException {
return value.getValue();
}
@Override
public Class<MyJsonObjectPojo> getTargetClass() {
return MyJsonObjectPojo.class;
}
}
JsonObject value;
public JsonObject getValue() {
return value;
}
public MyJsonObjectPojo setValue(JsonObject value) {
this.value = value;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyJsonObjectPojo that = (MyJsonObjectPojo) o;
return value == that.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}

View File

@@ -0,0 +1,50 @@
package io.vertx.core.json.codecs;
import io.vertx.core.json.JsonCodec;
import java.util.Objects;
public class MyLongPojo {
public static class MyLongPojoJsonCodec implements JsonCodec<MyLongPojo, Long> {
@Override
public MyLongPojo decode(Long value) throws IllegalArgumentException {
return new MyLongPojo().setValue(value);
}
@Override
public Long encode(MyLongPojo value) throws IllegalArgumentException {
return value.getValue();
}
@Override
public Class<MyLongPojo> getTargetClass() {
return MyLongPojo.class;
}
}
long value;
public long getValue() {
return value;
}
public MyLongPojo setValue(long value) {
this.value = value;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyLongPojo that = (MyLongPojo) o;
return value == that.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}

View File

@@ -0,0 +1,50 @@
package io.vertx.core.json.codecs;
import io.vertx.core.json.JsonCodec;
import java.util.Objects;
public class MyShortPojo {
public static class MyShortPojoJsonCodec implements JsonCodec<MyShortPojo, Short> {
@Override
public MyShortPojo decode(Short value) throws IllegalArgumentException {
return new MyShortPojo().setValue(value);
}
@Override
public Short encode(MyShortPojo value) throws IllegalArgumentException {
return value.getValue();
}
@Override
public Class<MyShortPojo> getTargetClass() {
return MyShortPojo.class;
}
}
short value;
public short getValue() {
return value;
}
public MyShortPojo setValue(short value) {
this.value = value;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyShortPojo that = (MyShortPojo) o;
return value == that.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}

View File

@@ -0,0 +1,9 @@
io.vertx.core.json.codecs.MyBooleanPojo$MyBooleanPojoJsonCodec
io.vertx.core.json.codecs.MyCharPojo$MyCharPojoJsonCodec
io.vertx.core.json.codecs.MyDoublePojo$MyDoublePojoJsonCodec
io.vertx.core.json.codecs.MyFloatPojo$MyFloatPojoJsonCodec
io.vertx.core.json.codecs.MyIntegerPojo$MyIntegerPojoJsonCodec
io.vertx.core.json.codecs.MyJsonArrayPojo$MyJsonArrayPojoJsonCodec
io.vertx.core.json.codecs.MyJsonObjectPojo$MyJsonObjectPojoJsonCodec
io.vertx.core.json.codecs.MyLongPojo$MyLongPojoJsonCodec
io.vertx.core.json.codecs.MyShortPojo$MyShortPojoJsonCodec