From 49f341609cc1a7ab141578ed8ac249b0c18f6eb9 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Mon, 1 Apr 2019 10:46:29 +0200 Subject: [PATCH 1/9] Added JsonCodec new interfaces Signed-off-by: slinkydeveloper --- .../java/io/vertx/core/json/JsonCodec.java | 11 +++++++++++ .../java/io/vertx/core/json/JsonDecoder.java | 19 +++++++++++++++++++ .../java/io/vertx/core/json/JsonEncoder.java | 19 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/main/java/io/vertx/core/json/JsonCodec.java create mode 100644 src/main/java/io/vertx/core/json/JsonDecoder.java create mode 100644 src/main/java/io/vertx/core/json/JsonEncoder.java diff --git a/src/main/java/io/vertx/core/json/JsonCodec.java b/src/main/java/io/vertx/core/json/JsonCodec.java new file mode 100644 index 000000000..55f7e96fa --- /dev/null +++ b/src/main/java/io/vertx/core/json/JsonCodec.java @@ -0,0 +1,11 @@ +package io.vertx.core.json; + +/** + * This interface represents the Vert.x json representation of any
+ * + * To use it in {@code @ModuleGen} annotation you must provide a {@code public static final [JsonCodecType] INSTANCE} to let codegen retrieve an instance of it + * + * @param + * @param + */ +public interface JsonCodec extends JsonEncoder, JsonDecoder { } diff --git a/src/main/java/io/vertx/core/json/JsonDecoder.java b/src/main/java/io/vertx/core/json/JsonDecoder.java new file mode 100644 index 000000000..c9bbf7685 --- /dev/null +++ b/src/main/java/io/vertx/core/json/JsonDecoder.java @@ -0,0 +1,19 @@ +package io.vertx.core.json; + +/** + * Primitive for conversion JSON_TYPE -> TARGET_TYPE + * + * @param + * @param + */ +public interface JsonDecoder { + /** + * decode performs the conversion JSON_TYPE -> TARGET_TYPE
+ * It expects value not null and must not return a null value + * + * @param value + * @return + * @throws IllegalArgumentException when it cannot decode the value + */ + TARGET_TYPE decode(JSON_TYPE value) throws IllegalArgumentException; +} diff --git a/src/main/java/io/vertx/core/json/JsonEncoder.java b/src/main/java/io/vertx/core/json/JsonEncoder.java new file mode 100644 index 000000000..868d87241 --- /dev/null +++ b/src/main/java/io/vertx/core/json/JsonEncoder.java @@ -0,0 +1,19 @@ +package io.vertx.core.json; + +/** + * Primitive for conversion TARGET_TYPE -> JSON_TYPE + * + * @param + * @param + */ +public interface JsonEncoder { + /** + * encode performs the conversion TARGET_TYPE -> JSON_TYPE
+ * It expects value not null and must not return a null value + * + * @param value + * @return + * @throws IllegalArgumentException when it cannot encode the value + */ + JSON_TYPE encode(TARGET_TYPE value) throws IllegalArgumentException; +} From 5b83683c7df5f3de552b2d540c38f15d611543a3 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Thu, 11 Apr 2019 13:41:13 +0200 Subject: [PATCH 2/9] JsonCodecLoader experiment Signed-off-by: slinkydeveloper --- .../io/vertx/core/json/JsonCodecLoader.java | 45 ++++++++++++++++ .../java/io/vertx/core/json/JsonDecoder.java | 2 + .../java/io/vertx/core/json/JsonEncoder.java | 2 + .../vertx/core/json/JsonCodecLoaderTest.java | 35 +++++++++++++ .../vertx/core/json/codecs/MyBooleanPojo.java | 50 ++++++++++++++++++ .../io/vertx/core/json/codecs/MyCharPojo.java | 50 ++++++++++++++++++ .../vertx/core/json/codecs/MyDoublePojo.java | 50 ++++++++++++++++++ .../vertx/core/json/codecs/MyFloatPojo.java | 50 ++++++++++++++++++ .../vertx/core/json/codecs/MyIntegerPojo.java | 50 ++++++++++++++++++ .../core/json/codecs/MyJsonArrayPojo.java | 52 +++++++++++++++++++ .../core/json/codecs/MyJsonObjectPojo.java | 51 ++++++++++++++++++ .../io/vertx/core/json/codecs/MyLongPojo.java | 50 ++++++++++++++++++ .../vertx/core/json/codecs/MyShortPojo.java | 50 ++++++++++++++++++ .../services/io.vertx.core.json.JsonCodec | 9 ++++ 14 files changed, 546 insertions(+) create mode 100644 src/main/java/io/vertx/core/json/JsonCodecLoader.java create mode 100644 src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java create mode 100644 src/test/java/io/vertx/core/json/codecs/MyBooleanPojo.java create mode 100644 src/test/java/io/vertx/core/json/codecs/MyCharPojo.java create mode 100644 src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java create mode 100644 src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java create mode 100644 src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java create mode 100644 src/test/java/io/vertx/core/json/codecs/MyJsonArrayPojo.java create mode 100644 src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java create mode 100644 src/test/java/io/vertx/core/json/codecs/MyLongPojo.java create mode 100644 src/test/java/io/vertx/core/json/codecs/MyShortPojo.java create mode 100644 src/test/resources/META-INF/services/io.vertx.core.json.JsonCodec diff --git a/src/main/java/io/vertx/core/json/JsonCodecLoader.java b/src/main/java/io/vertx/core/json/JsonCodecLoader.java new file mode 100644 index 000000000..e26518a9e --- /dev/null +++ b/src/main/java/io/vertx/core/json/JsonCodecLoader.java @@ -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 jsonCodecMap; + + private JsonCodecLoader(Map jsonCodecMap) { + this.jsonCodecMap = jsonCodecMap; + } + + public static JsonCodecLoader loadCodecsFromSPI() { + Map codecs = new HashMap<>(); + ServiceLoader codecServiceLoader = ServiceLoader.load(JsonCodec.class); + for (JsonCodec j : codecServiceLoader) { + codecs.put(j.getTargetClass(), j); + } + return new JsonCodecLoader(codecs); + } + + @SuppressWarnings("unchecked") + public T decode(Buffer value, Class 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); + } + } +} diff --git a/src/main/java/io/vertx/core/json/JsonDecoder.java b/src/main/java/io/vertx/core/json/JsonDecoder.java index c9bbf7685..bfed61838 100644 --- a/src/main/java/io/vertx/core/json/JsonDecoder.java +++ b/src/main/java/io/vertx/core/json/JsonDecoder.java @@ -16,4 +16,6 @@ public interface JsonDecoder { * @throws IllegalArgumentException when it cannot decode the value */ TARGET_TYPE decode(JSON_TYPE value) throws IllegalArgumentException; + + Class getTargetClass(); } diff --git a/src/main/java/io/vertx/core/json/JsonEncoder.java b/src/main/java/io/vertx/core/json/JsonEncoder.java index 868d87241..513d1b49a 100644 --- a/src/main/java/io/vertx/core/json/JsonEncoder.java +++ b/src/main/java/io/vertx/core/json/JsonEncoder.java @@ -16,4 +16,6 @@ public interface JsonEncoder { * @throws IllegalArgumentException when it cannot encode the value */ JSON_TYPE encode(TARGET_TYPE value) throws IllegalArgumentException; + + Class getTargetClass(); } diff --git a/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java b/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java new file mode 100644 index 000000000..d447d033f --- /dev/null +++ b/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java @@ -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)); + } + +} diff --git a/src/test/java/io/vertx/core/json/codecs/MyBooleanPojo.java b/src/test/java/io/vertx/core/json/codecs/MyBooleanPojo.java new file mode 100644 index 000000000..c72b74532 --- /dev/null +++ b/src/test/java/io/vertx/core/json/codecs/MyBooleanPojo.java @@ -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 { + + @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 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); + } +} diff --git a/src/test/java/io/vertx/core/json/codecs/MyCharPojo.java b/src/test/java/io/vertx/core/json/codecs/MyCharPojo.java new file mode 100644 index 000000000..6a6034b16 --- /dev/null +++ b/src/test/java/io/vertx/core/json/codecs/MyCharPojo.java @@ -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 { + + @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 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); + } +} diff --git a/src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java b/src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java new file mode 100644 index 000000000..cd4d22342 --- /dev/null +++ b/src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java @@ -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 { + + @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 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); + } +} diff --git a/src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java b/src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java new file mode 100644 index 000000000..f08004c20 --- /dev/null +++ b/src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java @@ -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 { + + @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 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); + } +} diff --git a/src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java b/src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java new file mode 100644 index 000000000..b3bf0e284 --- /dev/null +++ b/src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java @@ -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 { + + @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 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); + } +} diff --git a/src/test/java/io/vertx/core/json/codecs/MyJsonArrayPojo.java b/src/test/java/io/vertx/core/json/codecs/MyJsonArrayPojo.java new file mode 100644 index 000000000..843bcb5ae --- /dev/null +++ b/src/test/java/io/vertx/core/json/codecs/MyJsonArrayPojo.java @@ -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 { + + @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 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); + } +} diff --git a/src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java b/src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java new file mode 100644 index 000000000..8f717754b --- /dev/null +++ b/src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java @@ -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 { + + @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 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); + } +} diff --git a/src/test/java/io/vertx/core/json/codecs/MyLongPojo.java b/src/test/java/io/vertx/core/json/codecs/MyLongPojo.java new file mode 100644 index 000000000..f694ddf3e --- /dev/null +++ b/src/test/java/io/vertx/core/json/codecs/MyLongPojo.java @@ -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 { + + @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 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); + } +} diff --git a/src/test/java/io/vertx/core/json/codecs/MyShortPojo.java b/src/test/java/io/vertx/core/json/codecs/MyShortPojo.java new file mode 100644 index 000000000..7741294d0 --- /dev/null +++ b/src/test/java/io/vertx/core/json/codecs/MyShortPojo.java @@ -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 { + + @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 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); + } +} diff --git a/src/test/resources/META-INF/services/io.vertx.core.json.JsonCodec b/src/test/resources/META-INF/services/io.vertx.core.json.JsonCodec new file mode 100644 index 000000000..1f061e04f --- /dev/null +++ b/src/test/resources/META-INF/services/io.vertx.core.json.JsonCodec @@ -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 From 3062540c5d250dd34fe53cbb1e8351cfabf358c1 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Thu, 11 Apr 2019 17:35:54 +0200 Subject: [PATCH 3/9] Fixed JsonCodecLoader to manage wrong types Signed-off-by: slinkydeveloper --- .../io/vertx/core/json/JsonCodecLoader.java | 60 +++++++-- .../vertx/core/json/JsonCodecLoaderTest.java | 115 +++++++++++++++++- .../core/json/codecs/MyJsonObjectPojo.java | 2 +- 3 files changed, 158 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/vertx/core/json/JsonCodecLoader.java b/src/main/java/io/vertx/core/json/JsonCodecLoader.java index e26518a9e..82729dbac 100644 --- a/src/main/java/io/vertx/core/json/JsonCodecLoader.java +++ b/src/main/java/io/vertx/core/json/JsonCodecLoader.java @@ -5,8 +5,8 @@ import io.vertx.core.buffer.Buffer; import java.util.HashMap; import java.util.Map; import java.util.ServiceLoader; -import java.util.function.Function; +@SuppressWarnings("unchecked") public class JsonCodecLoader { Map jsonCodecMap; @@ -24,22 +24,58 @@ public class JsonCodecLoader { return new JsonCodecLoader(codecs); } - @SuppressWarnings("unchecked") - public T decode(Buffer value, Class c) { - Object json = value.toJson(); + private JsonCodec retrieveCodec(Class c) { + JsonCodec codec = jsonCodecMap.get(c); + if (codec == null) throw new IllegalStateException("Unable to find codec for class " + c.getName()); + return codec; + } + + public T decode(Object json, Class c) { try { - return (T) jsonCodecMap.get(c).decode(json); - } catch (Exception e) { - throw new IllegalStateException("Unable to find codec for class " + c.getCanonicalName(), e); + return (T) retrieveCodec(c).decode(json); + } catch (ClassCastException e) { + // A codec accepts only a particular type, that could not be the same of json parameter. + // This happens in particular with decodeBuffer(), when jackson decides what he wants for the serialized type + // And it's even worse if someone configure the Jackson mapper to use always float or long or things like these + // That's why these awful tricks + if (json.getClass().equals(Double.class)) { + return (T) retrieveCodec(c).decode(((Double)json).floatValue()); + } + if (json.getClass().equals(Float.class)) { + return (T) retrieveCodec(c).decode(((Float)json).doubleValue()); + } + if (json.getClass().equals(String.class)) { + return (T) retrieveCodec(c).decode(((String)json).charAt(0)); + } + try { + return decodeNaturalNumber((Number)json, c); + } catch (ClassCastException e1) { + throw e; + } } } - @SuppressWarnings("unchecked") - public Buffer encode(Object value) { + private T decodeNaturalNumber(Number number, Class c) { 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); + return (T) retrieveCodec(c).decode(number.intValue()); + } catch (ClassCastException e) { + try { + return (T) retrieveCodec(c).decode(number.longValue()); + } catch (ClassCastException e1) { + return (T) retrieveCodec(c).decode(number.shortValue()); + } } } + + public T decodeBuffer(Buffer value, Class c) { + return decode(Json.decodeValue(value), c); + } + + public Object encode(Object value) { + return retrieveCodec(value.getClass()).encode(value); + } + + public Buffer encodeBuffer(Object value) { + return Json.encodeToBuffer(encode(value)); + } } diff --git a/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java b/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java index d447d033f..6e8949899 100644 --- a/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java +++ b/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java @@ -1,11 +1,10 @@ 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 io.vertx.core.json.codecs.*; import org.junit.Before; import org.junit.Test; +import static io.vertx.core.json.Json.encodeToBuffer; import static org.junit.Assert.assertEquals; public class JsonCodecLoaderTest { @@ -21,15 +20,119 @@ public class JsonCodecLoaderTest { 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)); + assertEquals(encodeToBuffer(true), jsonCodecLoader.encodeBuffer(pojo)); + assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer(true), MyBooleanPojo.class)); + } + + @Test(expected = ClassCastException.class) + public void booleanCodecWrongTypeTest() { + jsonCodecLoader.decodeBuffer(encodeToBuffer("aaa"), 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)); + assertEquals(encodeToBuffer('a'), jsonCodecLoader.encodeBuffer(pojo)); + assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer('a'), MyCharPojo.class)); } + @Test(expected = ClassCastException.class) + public void charCodecWrongTypeTest() { + jsonCodecLoader.decodeBuffer(encodeToBuffer(1), MyCharPojo.class); + } + + @Test + public void doubleCodecTest() { + MyDoublePojo pojo = new MyDoublePojo(); + pojo.setValue(1.2d); + assertEquals(encodeToBuffer(1.2d), jsonCodecLoader.encodeBuffer(pojo)); + assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer(1.2d), MyDoublePojo.class)); + } + + @Test(expected = ClassCastException.class) + public void doubleCodecWrongTypeTest() { + jsonCodecLoader.decodeBuffer(encodeToBuffer(1L), MyDoublePojo.class); + } + + @Test + public void floatCodecTest() { + MyFloatPojo pojo = new MyFloatPojo(); + pojo.setValue(1.2f); + assertEquals(encodeToBuffer(1.2f), jsonCodecLoader.encodeBuffer(pojo)); + assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer(1.2f), MyFloatPojo.class)); + } + + @Test(expected = ClassCastException.class) + public void floatCodecWrongTypeTest() { + jsonCodecLoader.decodeBuffer(encodeToBuffer(1L), MyFloatPojo.class); + } + + @Test + public void intCodecTest() { + MyIntegerPojo pojo = new MyIntegerPojo(); + pojo.setValue(1); + assertEquals(encodeToBuffer((int)1), jsonCodecLoader.encodeBuffer(pojo)); + assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer((int)1), MyIntegerPojo.class)); + } + + @Test(expected = ClassCastException.class) + public void intCodecWrongTypeTest() { + jsonCodecLoader.decodeBuffer(encodeToBuffer(1.2), MyIntegerPojo.class); + } + + @Test + public void longCodecTest() { + MyLongPojo pojo = new MyLongPojo(); + pojo.setValue(1L); + assertEquals(encodeToBuffer(1L), jsonCodecLoader.encodeBuffer(pojo)); + assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer(1L), MyLongPojo.class)); + } + + @Test(expected = ClassCastException.class) + public void longCodecWrongTypeTest() { + jsonCodecLoader.decodeBuffer(encodeToBuffer(1.2), MyLongPojo.class); + } + + @Test + public void shortCodecTest() { + MyShortPojo pojo = new MyShortPojo(); + pojo.setValue((short)1); + assertEquals(encodeToBuffer((short)1), jsonCodecLoader.encodeBuffer(pojo)); + assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer((short)1), MyShortPojo.class)); + } + + @Test(expected = ClassCastException.class) + public void shortCodecWrongTypeTest() { + jsonCodecLoader.decodeBuffer(encodeToBuffer(1.2), MyShortPojo.class); + } + + @Test + public void jsonArrayCodecTest() { + MyJsonArrayPojo pojo = new MyJsonArrayPojo(); + JsonArray array = new JsonArray().add(1).add(2).add(3); + pojo.setValue(array); + assertEquals(array.toBuffer(), jsonCodecLoader.encodeBuffer(pojo)); + assertEquals(pojo, jsonCodecLoader.decodeBuffer(array.toBuffer(), MyJsonArrayPojo.class)); + } + + @Test(expected = ClassCastException.class) + public void jsonArrayCodecWrongTypeTest() { + jsonCodecLoader.decodeBuffer(encodeToBuffer(2), MyJsonArrayPojo.class); + } + + @Test + public void jsonObjectCodecTest() { + MyJsonObjectPojo pojo = new MyJsonObjectPojo(); + JsonObject obj = new JsonObject().put("a", 1).put("b", "c"); + pojo.setValue(obj); + assertEquals(obj.toBuffer(), jsonCodecLoader.encodeBuffer(pojo)); + assertEquals(pojo, jsonCodecLoader.decodeBuffer(obj.toBuffer(), MyJsonObjectPojo.class)); + } + + @Test(expected = ClassCastException.class) + public void jsonObjectCodecWrongTypeTest() { + jsonCodecLoader.decodeBuffer(encodeToBuffer(2), MyJsonObjectPojo.class); + } + } diff --git a/src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java b/src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java index 8f717754b..7939fb1d8 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java @@ -41,7 +41,7 @@ public class MyJsonObjectPojo { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MyJsonObjectPojo that = (MyJsonObjectPojo) o; - return value == that.value; + return Objects.equals(value, that.value); } @Override From 3d69c9c64dc9eacc0f0e30cbbda41702fe04b5be Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Thu, 11 Apr 2019 18:17:37 +0200 Subject: [PATCH 4/9] Updated JsonCodecLoader to new restrictions of JsonCodec Signed-off-by: slinkydeveloper --- .../java/io/vertx/core/json/JsonCodec.java | 10 +++- .../io/vertx/core/json/JsonCodecLoader.java | 35 +------------ .../java/io/vertx/core/json/JsonDecoder.java | 10 +++- .../java/io/vertx/core/json/JsonEncoder.java | 10 +++- .../vertx/core/json/JsonCodecLoaderTest.java | 23 ++------- .../io/vertx/core/json/codecs/MyCharPojo.java | 50 ------------------- .../vertx/core/json/codecs/MyDoublePojo.java | 6 +-- .../vertx/core/json/codecs/MyFloatPojo.java | 6 +-- .../vertx/core/json/codecs/MyIntegerPojo.java | 6 +-- .../io/vertx/core/json/codecs/MyLongPojo.java | 6 +-- .../vertx/core/json/codecs/MyShortPojo.java | 6 +-- .../services/io.vertx.core.json.JsonCodec | 1 - 12 files changed, 48 insertions(+), 121 deletions(-) delete mode 100644 src/test/java/io/vertx/core/json/codecs/MyCharPojo.java diff --git a/src/main/java/io/vertx/core/json/JsonCodec.java b/src/main/java/io/vertx/core/json/JsonCodec.java index 55f7e96fa..737d796fc 100644 --- a/src/main/java/io/vertx/core/json/JsonCodec.java +++ b/src/main/java/io/vertx/core/json/JsonCodec.java @@ -1,7 +1,15 @@ package io.vertx.core.json; /** - * This interface represents the Vert.x json representation of any
+ * This interface represents the Vert.x json representation of TARGET_TYPE.
+ * The TARGET_TYPE could be any type, while JSON_TYPE could be one of the following: + *
    + *
  • {@link JsonObject}
  • + *
  • {@link JsonArray}
  • + *
  • {@link String}
  • + *
  • {@link Number}
  • + *
  • {@link Boolean}
  • + *
* * To use it in {@code @ModuleGen} annotation you must provide a {@code public static final [JsonCodecType] INSTANCE} to let codegen retrieve an instance of it * diff --git a/src/main/java/io/vertx/core/json/JsonCodecLoader.java b/src/main/java/io/vertx/core/json/JsonCodecLoader.java index 82729dbac..37abbac4a 100644 --- a/src/main/java/io/vertx/core/json/JsonCodecLoader.java +++ b/src/main/java/io/vertx/core/json/JsonCodecLoader.java @@ -31,40 +31,7 @@ public class JsonCodecLoader { } public T decode(Object json, Class c) { - try { - return (T) retrieveCodec(c).decode(json); - } catch (ClassCastException e) { - // A codec accepts only a particular type, that could not be the same of json parameter. - // This happens in particular with decodeBuffer(), when jackson decides what he wants for the serialized type - // And it's even worse if someone configure the Jackson mapper to use always float or long or things like these - // That's why these awful tricks - if (json.getClass().equals(Double.class)) { - return (T) retrieveCodec(c).decode(((Double)json).floatValue()); - } - if (json.getClass().equals(Float.class)) { - return (T) retrieveCodec(c).decode(((Float)json).doubleValue()); - } - if (json.getClass().equals(String.class)) { - return (T) retrieveCodec(c).decode(((String)json).charAt(0)); - } - try { - return decodeNaturalNumber((Number)json, c); - } catch (ClassCastException e1) { - throw e; - } - } - } - - private T decodeNaturalNumber(Number number, Class c) { - try { - return (T) retrieveCodec(c).decode(number.intValue()); - } catch (ClassCastException e) { - try { - return (T) retrieveCodec(c).decode(number.longValue()); - } catch (ClassCastException e1) { - return (T) retrieveCodec(c).decode(number.shortValue()); - } - } + return (T) retrieveCodec(c).decode(json); } public T decodeBuffer(Buffer value, Class c) { diff --git a/src/main/java/io/vertx/core/json/JsonDecoder.java b/src/main/java/io/vertx/core/json/JsonDecoder.java index bfed61838..ea4ae9a14 100644 --- a/src/main/java/io/vertx/core/json/JsonDecoder.java +++ b/src/main/java/io/vertx/core/json/JsonDecoder.java @@ -1,7 +1,15 @@ package io.vertx.core.json; /** - * Primitive for conversion JSON_TYPE -> TARGET_TYPE + * Primitive for conversion JSON_TYPE -> TARGET_TYPE
+ * The TARGET_TYPE could be any type, while JSON_TYPE could be one of the following: + *
    + *
  • {@link JsonObject}
  • + *
  • {@link JsonArray}
  • + *
  • {@link String}
  • + *
  • {@link Number}
  • + *
  • {@link Boolean}
  • + *
* * @param * @param diff --git a/src/main/java/io/vertx/core/json/JsonEncoder.java b/src/main/java/io/vertx/core/json/JsonEncoder.java index 513d1b49a..852022e74 100644 --- a/src/main/java/io/vertx/core/json/JsonEncoder.java +++ b/src/main/java/io/vertx/core/json/JsonEncoder.java @@ -1,7 +1,15 @@ package io.vertx.core.json; /** - * Primitive for conversion TARGET_TYPE -> JSON_TYPE + * Primitive for conversion TARGET_TYPE -> JSON_TYPE
+ * The TARGET_TYPE could be any type, while JSON_TYPE could be one of the following: + *
    + *
  • {@link JsonObject}
  • + *
  • {@link JsonArray}
  • + *
  • {@link String}
  • + *
  • {@link Number}
  • + *
  • {@link Boolean}
  • + *
* * @param * @param diff --git a/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java b/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java index 6e8949899..31e4f1ad8 100644 --- a/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java +++ b/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java @@ -29,19 +29,6 @@ public class JsonCodecLoaderTest { jsonCodecLoader.decodeBuffer(encodeToBuffer("aaa"), MyBooleanPojo.class); } - @Test - public void charCodecTest() { - MyCharPojo pojo = new MyCharPojo(); - pojo.setValue('a'); - assertEquals(encodeToBuffer('a'), jsonCodecLoader.encodeBuffer(pojo)); - assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer('a'), MyCharPojo.class)); - } - - @Test(expected = ClassCastException.class) - public void charCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(1), MyCharPojo.class); - } - @Test public void doubleCodecTest() { MyDoublePojo pojo = new MyDoublePojo(); @@ -52,7 +39,7 @@ public class JsonCodecLoaderTest { @Test(expected = ClassCastException.class) public void doubleCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(1L), MyDoublePojo.class); + jsonCodecLoader.decodeBuffer(encodeToBuffer(""), MyDoublePojo.class); } @Test @@ -65,7 +52,7 @@ public class JsonCodecLoaderTest { @Test(expected = ClassCastException.class) public void floatCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(1L), MyFloatPojo.class); + jsonCodecLoader.decodeBuffer(encodeToBuffer(""), MyFloatPojo.class); } @Test @@ -78,7 +65,7 @@ public class JsonCodecLoaderTest { @Test(expected = ClassCastException.class) public void intCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(1.2), MyIntegerPojo.class); + jsonCodecLoader.decodeBuffer(encodeToBuffer(""), MyIntegerPojo.class); } @Test @@ -91,7 +78,7 @@ public class JsonCodecLoaderTest { @Test(expected = ClassCastException.class) public void longCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(1.2), MyLongPojo.class); + jsonCodecLoader.decodeBuffer(encodeToBuffer(""), MyLongPojo.class); } @Test @@ -104,7 +91,7 @@ public class JsonCodecLoaderTest { @Test(expected = ClassCastException.class) public void shortCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(1.2), MyShortPojo.class); + jsonCodecLoader.decodeBuffer(encodeToBuffer(""), MyShortPojo.class); } @Test diff --git a/src/test/java/io/vertx/core/json/codecs/MyCharPojo.java b/src/test/java/io/vertx/core/json/codecs/MyCharPojo.java deleted file mode 100644 index 6a6034b16..000000000 --- a/src/test/java/io/vertx/core/json/codecs/MyCharPojo.java +++ /dev/null @@ -1,50 +0,0 @@ -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 { - - @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 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); - } -} diff --git a/src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java b/src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java index cd4d22342..4fdb41457 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java @@ -6,11 +6,11 @@ import java.util.Objects; public class MyDoublePojo { - public static class MyDoublePojoJsonCodec implements JsonCodec { + public static class MyDoublePojoJsonCodec implements JsonCodec { @Override - public MyDoublePojo decode(Double value) throws IllegalArgumentException { - return new MyDoublePojo().setValue(value); + public MyDoublePojo decode(Number value) throws IllegalArgumentException { + return new MyDoublePojo().setValue(value.doubleValue()); } @Override diff --git a/src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java b/src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java index f08004c20..3bf82c4d0 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java @@ -6,11 +6,11 @@ import java.util.Objects; public class MyFloatPojo { - public static class MyFloatPojoJsonCodec implements JsonCodec { + public static class MyFloatPojoJsonCodec implements JsonCodec { @Override - public MyFloatPojo decode(Float value) throws IllegalArgumentException { - return new MyFloatPojo().setValue(value); + public MyFloatPojo decode(Number value) throws IllegalArgumentException { + return new MyFloatPojo().setValue(value.floatValue()); } @Override diff --git a/src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java b/src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java index b3bf0e284..76962e9aa 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java @@ -6,11 +6,11 @@ import java.util.Objects; public class MyIntegerPojo { - public static class MyIntegerPojoJsonCodec implements JsonCodec { + public static class MyIntegerPojoJsonCodec implements JsonCodec { @Override - public MyIntegerPojo decode(Integer value) throws IllegalArgumentException { - return new MyIntegerPojo().setValue(value); + public MyIntegerPojo decode(Number value) throws IllegalArgumentException { + return new MyIntegerPojo().setValue(value.intValue()); } @Override diff --git a/src/test/java/io/vertx/core/json/codecs/MyLongPojo.java b/src/test/java/io/vertx/core/json/codecs/MyLongPojo.java index f694ddf3e..5be185c9e 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyLongPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyLongPojo.java @@ -6,11 +6,11 @@ import java.util.Objects; public class MyLongPojo { - public static class MyLongPojoJsonCodec implements JsonCodec { + public static class MyLongPojoJsonCodec implements JsonCodec { @Override - public MyLongPojo decode(Long value) throws IllegalArgumentException { - return new MyLongPojo().setValue(value); + public MyLongPojo decode(Number value) throws IllegalArgumentException { + return new MyLongPojo().setValue(value.longValue()); } @Override diff --git a/src/test/java/io/vertx/core/json/codecs/MyShortPojo.java b/src/test/java/io/vertx/core/json/codecs/MyShortPojo.java index 7741294d0..926c85d79 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyShortPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyShortPojo.java @@ -6,11 +6,11 @@ import java.util.Objects; public class MyShortPojo { - public static class MyShortPojoJsonCodec implements JsonCodec { + public static class MyShortPojoJsonCodec implements JsonCodec { @Override - public MyShortPojo decode(Short value) throws IllegalArgumentException { - return new MyShortPojo().setValue(value); + public MyShortPojo decode(Number value) throws IllegalArgumentException { + return new MyShortPojo().setValue(value.shortValue()); } @Override diff --git a/src/test/resources/META-INF/services/io.vertx.core.json.JsonCodec b/src/test/resources/META-INF/services/io.vertx.core.json.JsonCodec index 1f061e04f..0f1f78ca4 100644 --- a/src/test/resources/META-INF/services/io.vertx.core.json.JsonCodec +++ b/src/test/resources/META-INF/services/io.vertx.core.json.JsonCodec @@ -1,5 +1,4 @@ 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 From 680d286f4b440e9f82bd2c51775717e6c06075db Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Thu, 11 Apr 2019 18:53:09 +0200 Subject: [PATCH 5/9] Made JsonCodecLoader as singleton Signed-off-by: slinkydeveloper --- .../io/vertx/core/json/JsonCodecLoader.java | 30 ++++++---- .../vertx/core/json/JsonCodecLoaderTest.java | 55 ++++++++----------- 2 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/main/java/io/vertx/core/json/JsonCodecLoader.java b/src/main/java/io/vertx/core/json/JsonCodecLoader.java index 37abbac4a..f1ef8fb80 100644 --- a/src/main/java/io/vertx/core/json/JsonCodecLoader.java +++ b/src/main/java/io/vertx/core/json/JsonCodecLoader.java @@ -9,29 +9,24 @@ import java.util.ServiceLoader; @SuppressWarnings("unchecked") public class JsonCodecLoader { - Map jsonCodecMap; + private Map jsonCodecMap; private JsonCodecLoader(Map jsonCodecMap) { this.jsonCodecMap = jsonCodecMap; } - public static JsonCodecLoader loadCodecsFromSPI() { - Map codecs = new HashMap<>(); - ServiceLoader codecServiceLoader = ServiceLoader.load(JsonCodec.class); - for (JsonCodec j : codecServiceLoader) { - codecs.put(j.getTargetClass(), j); - } - return new JsonCodecLoader(codecs); - } - private JsonCodec retrieveCodec(Class c) { JsonCodec codec = jsonCodecMap.get(c); if (codec == null) throw new IllegalStateException("Unable to find codec for class " + c.getName()); return codec; } + public boolean hasCodecFor(Class c) { + return jsonCodecMap.containsKey(c); + } + public T decode(Object json, Class c) { - return (T) retrieveCodec(c).decode(json); + return (T) ((json != null) ? retrieveCodec(c).decode(json) : null); } public T decodeBuffer(Buffer value, Class c) { @@ -39,10 +34,21 @@ public class JsonCodecLoader { } public Object encode(Object value) { - return retrieveCodec(value.getClass()).encode(value); + return (value != null) ? retrieveCodec(value.getClass()).encode(value) : null; } public Buffer encodeBuffer(Object value) { return Json.encodeToBuffer(encode(value)); } + + private static JsonCodecLoader loadCodecsFromSPI() { + Map codecs = new HashMap<>(); + ServiceLoader codecServiceLoader = ServiceLoader.load(JsonCodec.class); + for (JsonCodec j : codecServiceLoader) { + codecs.put(j.getTargetClass(), j); + } + return new JsonCodecLoader(codecs); + } + + public static final JsonCodecLoader INSTANCE = JsonCodecLoader.loadCodecsFromSPI(); } diff --git a/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java b/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java index 31e4f1ad8..a2972960f 100644 --- a/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java +++ b/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java @@ -9,89 +9,82 @@ 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(encodeToBuffer(true), jsonCodecLoader.encodeBuffer(pojo)); - assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer(true), MyBooleanPojo.class)); + assertEquals(encodeToBuffer(true), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(true), MyBooleanPojo.class)); } @Test(expected = ClassCastException.class) public void booleanCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer("aaa"), MyBooleanPojo.class); + JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer("aaa"), MyBooleanPojo.class); } @Test public void doubleCodecTest() { MyDoublePojo pojo = new MyDoublePojo(); pojo.setValue(1.2d); - assertEquals(encodeToBuffer(1.2d), jsonCodecLoader.encodeBuffer(pojo)); - assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer(1.2d), MyDoublePojo.class)); + assertEquals(encodeToBuffer(1.2d), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(1.2d), MyDoublePojo.class)); } @Test(expected = ClassCastException.class) public void doubleCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(""), MyDoublePojo.class); + JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(""), MyDoublePojo.class); } @Test public void floatCodecTest() { MyFloatPojo pojo = new MyFloatPojo(); pojo.setValue(1.2f); - assertEquals(encodeToBuffer(1.2f), jsonCodecLoader.encodeBuffer(pojo)); - assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer(1.2f), MyFloatPojo.class)); + assertEquals(encodeToBuffer(1.2f), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(1.2f), MyFloatPojo.class)); } @Test(expected = ClassCastException.class) public void floatCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(""), MyFloatPojo.class); + JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(""), MyFloatPojo.class); } @Test public void intCodecTest() { MyIntegerPojo pojo = new MyIntegerPojo(); pojo.setValue(1); - assertEquals(encodeToBuffer((int)1), jsonCodecLoader.encodeBuffer(pojo)); - assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer((int)1), MyIntegerPojo.class)); + assertEquals(encodeToBuffer((int)1), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer((int)1), MyIntegerPojo.class)); } @Test(expected = ClassCastException.class) public void intCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(""), MyIntegerPojo.class); + JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(""), MyIntegerPojo.class); } @Test public void longCodecTest() { MyLongPojo pojo = new MyLongPojo(); pojo.setValue(1L); - assertEquals(encodeToBuffer(1L), jsonCodecLoader.encodeBuffer(pojo)); - assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer(1L), MyLongPojo.class)); + assertEquals(encodeToBuffer(1L), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(1L), MyLongPojo.class)); } @Test(expected = ClassCastException.class) public void longCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(""), MyLongPojo.class); + JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(""), MyLongPojo.class); } @Test public void shortCodecTest() { MyShortPojo pojo = new MyShortPojo(); pojo.setValue((short)1); - assertEquals(encodeToBuffer((short)1), jsonCodecLoader.encodeBuffer(pojo)); - assertEquals(pojo, jsonCodecLoader.decodeBuffer(encodeToBuffer((short)1), MyShortPojo.class)); + assertEquals(encodeToBuffer((short)1), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer((short)1), MyShortPojo.class)); } @Test(expected = ClassCastException.class) public void shortCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(""), MyShortPojo.class); + JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(""), MyShortPojo.class); } @Test @@ -99,13 +92,13 @@ public class JsonCodecLoaderTest { MyJsonArrayPojo pojo = new MyJsonArrayPojo(); JsonArray array = new JsonArray().add(1).add(2).add(3); pojo.setValue(array); - assertEquals(array.toBuffer(), jsonCodecLoader.encodeBuffer(pojo)); - assertEquals(pojo, jsonCodecLoader.decodeBuffer(array.toBuffer(), MyJsonArrayPojo.class)); + assertEquals(array.toBuffer(), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(array.toBuffer(), MyJsonArrayPojo.class)); } @Test(expected = ClassCastException.class) public void jsonArrayCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(2), MyJsonArrayPojo.class); + JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(2), MyJsonArrayPojo.class); } @Test @@ -113,13 +106,13 @@ public class JsonCodecLoaderTest { MyJsonObjectPojo pojo = new MyJsonObjectPojo(); JsonObject obj = new JsonObject().put("a", 1).put("b", "c"); pojo.setValue(obj); - assertEquals(obj.toBuffer(), jsonCodecLoader.encodeBuffer(pojo)); - assertEquals(pojo, jsonCodecLoader.decodeBuffer(obj.toBuffer(), MyJsonObjectPojo.class)); + assertEquals(obj.toBuffer(), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(obj.toBuffer(), MyJsonObjectPojo.class)); } @Test(expected = ClassCastException.class) public void jsonObjectCodecWrongTypeTest() { - jsonCodecLoader.decodeBuffer(encodeToBuffer(2), MyJsonObjectPojo.class); + JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(2), MyJsonObjectPojo.class); } } From c1fa1a85db804ff7eefd5d945300b12745f877bd Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Fri, 12 Apr 2019 09:36:56 +0200 Subject: [PATCH 6/9] Added small documentation about the codecs Signed-off-by: slinkydeveloper --- src/main/asciidoc/override/json.adoc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/asciidoc/override/json.adoc b/src/main/asciidoc/override/json.adoc index 820baeb1a..ad08e232f 100644 --- a/src/main/asciidoc/override/json.adoc +++ b/src/main/asciidoc/override/json.adoc @@ -116,3 +116,16 @@ You get values from a JSON array using the `getXXX` methods, for example: ==== Encoding a JSON array to a String You use {@link io.vertx.core.json.JsonArray#encode} to encode the array to a String form. + +=== JsonCodec + +If you want to define a custom way to encode and decode Json, you can define a {@link io.vertx.core.json.JsonCodec} for it. +A `JsonCodec` for type `X` is a concrete class that implements the interface `JsonCodec`, where `J` must be any of these: + +* `JsonArray` or `JsonObject` +* `Number` +* `String` +* `Boolean` + +These `JsonCodec`s can be used in several components of Vert.x stack, like in vertx-web-client, using the SPI +`io.vertx.core.json.JsonCodec`. From 9da08b8eb588968da39414162ab6e754bf8d5a89 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Tue, 4 Jun 2019 15:00:45 +0200 Subject: [PATCH 7/9] Moved JsonCodec interfaces to io.vertx.core.spi.json Signed-off-by: slinkydeveloper --- src/main/asciidoc/override/json.adoc | 4 ++-- src/main/java/io/vertx/core/json/JsonCodecLoader.java | 1 + src/main/java/io/vertx/core/{ => spi}/json/JsonCodec.java | 5 ++++- src/main/java/io/vertx/core/{ => spi}/json/JsonDecoder.java | 5 ++++- src/main/java/io/vertx/core/{ => spi}/json/JsonEncoder.java | 5 ++++- src/test/java/io/vertx/core/json/codecs/MyBooleanPojo.java | 2 +- src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java | 2 +- src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java | 2 +- src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java | 2 +- src/test/java/io/vertx/core/json/codecs/MyJsonArrayPojo.java | 3 +-- .../java/io/vertx/core/json/codecs/MyJsonObjectPojo.java | 2 +- src/test/java/io/vertx/core/json/codecs/MyLongPojo.java | 2 +- src/test/java/io/vertx/core/json/codecs/MyShortPojo.java | 2 +- ....core.json.JsonCodec => io.vertx.core.spi.json.JsonCodec} | 0 14 files changed, 23 insertions(+), 14 deletions(-) rename src/main/java/io/vertx/core/{ => spi}/json/JsonCodec.java (86%) rename src/main/java/io/vertx/core/{ => spi}/json/JsonDecoder.java (88%) rename src/main/java/io/vertx/core/{ => spi}/json/JsonEncoder.java (88%) rename src/test/resources/META-INF/services/{io.vertx.core.json.JsonCodec => io.vertx.core.spi.json.JsonCodec} (100%) diff --git a/src/main/asciidoc/override/json.adoc b/src/main/asciidoc/override/json.adoc index ad08e232f..c0b7a2a98 100644 --- a/src/main/asciidoc/override/json.adoc +++ b/src/main/asciidoc/override/json.adoc @@ -119,7 +119,7 @@ You use {@link io.vertx.core.json.JsonArray#encode} to encode the array to a Str === JsonCodec -If you want to define a custom way to encode and decode Json, you can define a {@link io.vertx.core.json.JsonCodec} for it. +If you want to define a custom way to encode and decode Json, you can define a {@link io.vertx.core.spi.json.JsonCodec} for it. A `JsonCodec` for type `X` is a concrete class that implements the interface `JsonCodec`, where `J` must be any of these: * `JsonArray` or `JsonObject` @@ -128,4 +128,4 @@ A `JsonCodec` for type `X` is a concrete class that implements the interface `Js * `Boolean` These `JsonCodec`s can be used in several components of Vert.x stack, like in vertx-web-client, using the SPI -`io.vertx.core.json.JsonCodec`. +`io.vertx.core.spi.json.JsonCodec`. diff --git a/src/main/java/io/vertx/core/json/JsonCodecLoader.java b/src/main/java/io/vertx/core/json/JsonCodecLoader.java index f1ef8fb80..70761f9d6 100644 --- a/src/main/java/io/vertx/core/json/JsonCodecLoader.java +++ b/src/main/java/io/vertx/core/json/JsonCodecLoader.java @@ -1,6 +1,7 @@ package io.vertx.core.json; import io.vertx.core.buffer.Buffer; +import io.vertx.core.spi.json.JsonCodec; import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/io/vertx/core/json/JsonCodec.java b/src/main/java/io/vertx/core/spi/json/JsonCodec.java similarity index 86% rename from src/main/java/io/vertx/core/json/JsonCodec.java rename to src/main/java/io/vertx/core/spi/json/JsonCodec.java index 737d796fc..3b55856b9 100644 --- a/src/main/java/io/vertx/core/json/JsonCodec.java +++ b/src/main/java/io/vertx/core/spi/json/JsonCodec.java @@ -1,4 +1,7 @@ -package io.vertx.core.json; +package io.vertx.core.spi.json; + +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; /** * This interface represents the Vert.x json representation of TARGET_TYPE.
diff --git a/src/main/java/io/vertx/core/json/JsonDecoder.java b/src/main/java/io/vertx/core/spi/json/JsonDecoder.java similarity index 88% rename from src/main/java/io/vertx/core/json/JsonDecoder.java rename to src/main/java/io/vertx/core/spi/json/JsonDecoder.java index ea4ae9a14..842793263 100644 --- a/src/main/java/io/vertx/core/json/JsonDecoder.java +++ b/src/main/java/io/vertx/core/spi/json/JsonDecoder.java @@ -1,4 +1,7 @@ -package io.vertx.core.json; +package io.vertx.core.spi.json; + +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; /** * Primitive for conversion JSON_TYPE -> TARGET_TYPE
diff --git a/src/main/java/io/vertx/core/json/JsonEncoder.java b/src/main/java/io/vertx/core/spi/json/JsonEncoder.java similarity index 88% rename from src/main/java/io/vertx/core/json/JsonEncoder.java rename to src/main/java/io/vertx/core/spi/json/JsonEncoder.java index 852022e74..201f9ac99 100644 --- a/src/main/java/io/vertx/core/json/JsonEncoder.java +++ b/src/main/java/io/vertx/core/spi/json/JsonEncoder.java @@ -1,4 +1,7 @@ -package io.vertx.core.json; +package io.vertx.core.spi.json; + +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; /** * Primitive for conversion TARGET_TYPE -> JSON_TYPE
diff --git a/src/test/java/io/vertx/core/json/codecs/MyBooleanPojo.java b/src/test/java/io/vertx/core/json/codecs/MyBooleanPojo.java index c72b74532..4e9542943 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyBooleanPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyBooleanPojo.java @@ -1,6 +1,6 @@ package io.vertx.core.json.codecs; -import io.vertx.core.json.JsonCodec; +import io.vertx.core.spi.json.JsonCodec; import java.util.Objects; diff --git a/src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java b/src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java index 4fdb41457..d300d0b8d 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyDoublePojo.java @@ -1,6 +1,6 @@ package io.vertx.core.json.codecs; -import io.vertx.core.json.JsonCodec; +import io.vertx.core.spi.json.JsonCodec; import java.util.Objects; diff --git a/src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java b/src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java index 3bf82c4d0..0e1fd8850 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyFloatPojo.java @@ -1,6 +1,6 @@ package io.vertx.core.json.codecs; -import io.vertx.core.json.JsonCodec; +import io.vertx.core.spi.json.JsonCodec; import java.util.Objects; diff --git a/src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java b/src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java index 76962e9aa..702d1999a 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyIntegerPojo.java @@ -1,6 +1,6 @@ package io.vertx.core.json.codecs; -import io.vertx.core.json.JsonCodec; +import io.vertx.core.spi.json.JsonCodec; import java.util.Objects; diff --git a/src/test/java/io/vertx/core/json/codecs/MyJsonArrayPojo.java b/src/test/java/io/vertx/core/json/codecs/MyJsonArrayPojo.java index 843bcb5ae..32246782b 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyJsonArrayPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyJsonArrayPojo.java @@ -1,9 +1,8 @@ package io.vertx.core.json.codecs; -import io.vertx.core.json.JsonCodec; +import io.vertx.core.spi.json.JsonCodec; import io.vertx.core.json.JsonArray; -import java.util.Arrays; import java.util.Objects; public class MyJsonArrayPojo { diff --git a/src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java b/src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java index 7939fb1d8..b69e919bb 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyJsonObjectPojo.java @@ -1,6 +1,6 @@ package io.vertx.core.json.codecs; -import io.vertx.core.json.JsonCodec; +import io.vertx.core.spi.json.JsonCodec; import io.vertx.core.json.JsonObject; import java.util.Objects; diff --git a/src/test/java/io/vertx/core/json/codecs/MyLongPojo.java b/src/test/java/io/vertx/core/json/codecs/MyLongPojo.java index 5be185c9e..2a4bf0627 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyLongPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyLongPojo.java @@ -1,6 +1,6 @@ package io.vertx.core.json.codecs; -import io.vertx.core.json.JsonCodec; +import io.vertx.core.spi.json.JsonCodec; import java.util.Objects; diff --git a/src/test/java/io/vertx/core/json/codecs/MyShortPojo.java b/src/test/java/io/vertx/core/json/codecs/MyShortPojo.java index 926c85d79..b2320d577 100644 --- a/src/test/java/io/vertx/core/json/codecs/MyShortPojo.java +++ b/src/test/java/io/vertx/core/json/codecs/MyShortPojo.java @@ -1,6 +1,6 @@ package io.vertx.core.json.codecs; -import io.vertx.core.json.JsonCodec; +import io.vertx.core.spi.json.JsonCodec; import java.util.Objects; diff --git a/src/test/resources/META-INF/services/io.vertx.core.json.JsonCodec b/src/test/resources/META-INF/services/io.vertx.core.spi.json.JsonCodec similarity index 100% rename from src/test/resources/META-INF/services/io.vertx.core.json.JsonCodec rename to src/test/resources/META-INF/services/io.vertx.core.spi.json.JsonCodec From a295087643b9fc701283bb94f8c7251371a5b9c3 Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Wed, 5 Jun 2019 15:29:34 +0200 Subject: [PATCH 8/9] Minor improvements --- src/main/asciidoc/override/json.adoc | 13 ----- .../io/vertx/core/json/JsonCodecLoader.java | 55 ------------------ .../io/vertx/core/json/JsonCodecMapper.java | 56 +++++++++++++++++++ .../io/vertx/core/spi/json/JsonCodec.java | 15 +++-- .../io/vertx/core/spi/json/JsonDecoder.java | 28 ++++++---- .../io/vertx/core/spi/json/JsonEncoder.java | 36 +++++++----- .../vertx/core/json/JsonCodecLoaderTest.java | 49 ++++++++-------- 7 files changed, 127 insertions(+), 125 deletions(-) delete mode 100644 src/main/java/io/vertx/core/json/JsonCodecLoader.java create mode 100644 src/main/java/io/vertx/core/json/JsonCodecMapper.java diff --git a/src/main/asciidoc/override/json.adoc b/src/main/asciidoc/override/json.adoc index c0b7a2a98..820baeb1a 100644 --- a/src/main/asciidoc/override/json.adoc +++ b/src/main/asciidoc/override/json.adoc @@ -116,16 +116,3 @@ You get values from a JSON array using the `getXXX` methods, for example: ==== Encoding a JSON array to a String You use {@link io.vertx.core.json.JsonArray#encode} to encode the array to a String form. - -=== JsonCodec - -If you want to define a custom way to encode and decode Json, you can define a {@link io.vertx.core.spi.json.JsonCodec} for it. -A `JsonCodec` for type `X` is a concrete class that implements the interface `JsonCodec`, where `J` must be any of these: - -* `JsonArray` or `JsonObject` -* `Number` -* `String` -* `Boolean` - -These `JsonCodec`s can be used in several components of Vert.x stack, like in vertx-web-client, using the SPI -`io.vertx.core.spi.json.JsonCodec`. diff --git a/src/main/java/io/vertx/core/json/JsonCodecLoader.java b/src/main/java/io/vertx/core/json/JsonCodecLoader.java deleted file mode 100644 index 70761f9d6..000000000 --- a/src/main/java/io/vertx/core/json/JsonCodecLoader.java +++ /dev/null @@ -1,55 +0,0 @@ -package io.vertx.core.json; - -import io.vertx.core.buffer.Buffer; -import io.vertx.core.spi.json.JsonCodec; - -import java.util.HashMap; -import java.util.Map; -import java.util.ServiceLoader; - -@SuppressWarnings("unchecked") -public class JsonCodecLoader { - - private Map jsonCodecMap; - - private JsonCodecLoader(Map jsonCodecMap) { - this.jsonCodecMap = jsonCodecMap; - } - - private JsonCodec retrieveCodec(Class c) { - JsonCodec codec = jsonCodecMap.get(c); - if (codec == null) throw new IllegalStateException("Unable to find codec for class " + c.getName()); - return codec; - } - - public boolean hasCodecFor(Class c) { - return jsonCodecMap.containsKey(c); - } - - public T decode(Object json, Class c) { - return (T) ((json != null) ? retrieveCodec(c).decode(json) : null); - } - - public T decodeBuffer(Buffer value, Class c) { - return decode(Json.decodeValue(value), c); - } - - public Object encode(Object value) { - return (value != null) ? retrieveCodec(value.getClass()).encode(value) : null; - } - - public Buffer encodeBuffer(Object value) { - return Json.encodeToBuffer(encode(value)); - } - - private static JsonCodecLoader loadCodecsFromSPI() { - Map codecs = new HashMap<>(); - ServiceLoader codecServiceLoader = ServiceLoader.load(JsonCodec.class); - for (JsonCodec j : codecServiceLoader) { - codecs.put(j.getTargetClass(), j); - } - return new JsonCodecLoader(codecs); - } - - public static final JsonCodecLoader INSTANCE = JsonCodecLoader.loadCodecsFromSPI(); -} diff --git a/src/main/java/io/vertx/core/json/JsonCodecMapper.java b/src/main/java/io/vertx/core/json/JsonCodecMapper.java new file mode 100644 index 000000000..996c97b65 --- /dev/null +++ b/src/main/java/io/vertx/core/json/JsonCodecMapper.java @@ -0,0 +1,56 @@ +package io.vertx.core.json; + +import io.vertx.core.buffer.Buffer; +import io.vertx.core.spi.json.JsonCodec; + +import java.util.HashMap; +import java.util.Map; +import java.util.ServiceLoader; + +class JsonCodecMapper { + + private static final Map codecMap; + + static { + Map map = new HashMap<>(); + ServiceLoader codecServiceLoader = ServiceLoader.load(JsonCodec.class); + for (JsonCodec j : codecServiceLoader) { + map.put(j.getTargetClass(), j); + } + codecMap = map; + } + + private static JsonCodec codec(Class c) { + return codecMap.get(c); + } + + public static T decode(Object json, Class c) { + if (json == null) { + return null; + } + JsonCodec codec = (JsonCodec) codecMap.get(c); + if (codec == null) { + throw new IllegalStateException("Unable to find codec for class " + c.getName()); + } + return codec.decode(json); + } + + public static T decodeBuffer(Buffer value, Class c) { + return decode(Json.decodeValue(value), c); + } + + public static Object encode(Object value) { + if (value == null) { + return null; + } + JsonCodec codec = (JsonCodec) codecMap.get(value.getClass()); + if (codec == null) { + throw new IllegalStateException("Unable to find codec for class " + value.getClass().getName()); + } + return codec.encode(value); + } + + public static Buffer encodeBuffer(Object value) { + return Json.encodeToBuffer(encode(value)); + } +} diff --git a/src/main/java/io/vertx/core/spi/json/JsonCodec.java b/src/main/java/io/vertx/core/spi/json/JsonCodec.java index 3b55856b9..8e59a13c8 100644 --- a/src/main/java/io/vertx/core/spi/json/JsonCodec.java +++ b/src/main/java/io/vertx/core/spi/json/JsonCodec.java @@ -4,8 +4,9 @@ import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; /** - * This interface represents the Vert.x json representation of TARGET_TYPE.
- * The TARGET_TYPE could be any type, while JSON_TYPE could be one of the following: + * Converts {@code } back and forth to {@code }. + *
+ * {@code } can be any class or interface type, {@code } can be one of the following: *
    *
  • {@link JsonObject}
  • *
  • {@link JsonArray}
  • @@ -14,9 +15,11 @@ import io.vertx.core.json.JsonObject; *
  • {@link Boolean}
  • *
* - * To use it in {@code @ModuleGen} annotation you must provide a {@code public static final [JsonCodecType] INSTANCE} to let codegen retrieve an instance of it + * To use it in {@code @ModuleGen} annotation you must provide + * a {@code public static final [JsonCodecType] INSTANCE}. * - * @param - * @param + * @param the type encoded to Json + * @param the json type */ -public interface JsonCodec extends JsonEncoder, JsonDecoder { } +public interface JsonCodec extends JsonEncoder, JsonDecoder { +} diff --git a/src/main/java/io/vertx/core/spi/json/JsonDecoder.java b/src/main/java/io/vertx/core/spi/json/JsonDecoder.java index 842793263..03f42a002 100644 --- a/src/main/java/io/vertx/core/spi/json/JsonDecoder.java +++ b/src/main/java/io/vertx/core/spi/json/JsonDecoder.java @@ -4,8 +4,9 @@ import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; /** - * Primitive for conversion JSON_TYPE -> TARGET_TYPE
- * The TARGET_TYPE could be any type, while JSON_TYPE could be one of the following: + * Converts {@code } type to {@code }. + *
+ * {@code } can be any class or interface type, {@code } can be one of the following: *
    *
  • {@link JsonObject}
  • *
  • {@link JsonArray}
  • @@ -14,19 +15,24 @@ import io.vertx.core.json.JsonObject; *
  • {@link Boolean}
  • *
* - * @param - * @param + * @param the type decoded from Json + * @param the json type */ -public interface JsonDecoder { +public interface JsonDecoder { + /** - * decode performs the conversion JSON_TYPE -> TARGET_TYPE
- * It expects value not null and must not return a null value + * Decode {@code } to {@code }. + *
+ * The {@code json} will not be {@code null} and the implementation must not return {@code null}. * - * @param value - * @return + * @param json the json value + * @return the decoded value * @throws IllegalArgumentException when it cannot decode the value */ - TARGET_TYPE decode(JSON_TYPE value) throws IllegalArgumentException; + T decode(J json) throws IllegalArgumentException; - Class getTargetClass(); + /** + * @return the class for {@code } + */ + Class getTargetClass(); } diff --git a/src/main/java/io/vertx/core/spi/json/JsonEncoder.java b/src/main/java/io/vertx/core/spi/json/JsonEncoder.java index 201f9ac99..cf2a029fe 100644 --- a/src/main/java/io/vertx/core/spi/json/JsonEncoder.java +++ b/src/main/java/io/vertx/core/spi/json/JsonEncoder.java @@ -4,8 +4,9 @@ import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; /** - * Primitive for conversion TARGET_TYPE -> JSON_TYPE
- * The TARGET_TYPE could be any type, while JSON_TYPE could be one of the following: + * Converts {@code } type to {@code }. + *
+ * {@code } can be any class or interface type, {@code } can be one of the following: *
    *
  • {@link JsonObject}
  • *
  • {@link JsonArray}
  • @@ -14,19 +15,24 @@ import io.vertx.core.json.JsonObject; *
  • {@link Boolean}
  • *
* - * @param - * @param + * @param the type encoded to Json + * @param the json type */ -public interface JsonEncoder { - /** - * encode performs the conversion TARGET_TYPE -> JSON_TYPE
- * It expects value not null and must not return a null value - * - * @param value - * @return - * @throws IllegalArgumentException when it cannot encode the value - */ - JSON_TYPE encode(TARGET_TYPE value) throws IllegalArgumentException; +public interface JsonEncoder { - Class getTargetClass(); + /** + * Encode {@code } to {@code }. + *
+ * The {@code value} will not be {@code null} and the implementation must not return {@code null}. + * + * @param value the value + * @return the encoded json value + * @throws IllegalArgumentException when it cannot decode the value + */ + J encode(T value) throws IllegalArgumentException; + + /** + * @return the class for {@code } + */ + Class getTargetClass(); } diff --git a/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java b/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java index a2972960f..6421400a0 100644 --- a/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java +++ b/src/test/java/io/vertx/core/json/JsonCodecLoaderTest.java @@ -1,7 +1,6 @@ package io.vertx.core.json; import io.vertx.core.json.codecs.*; -import org.junit.Before; import org.junit.Test; import static io.vertx.core.json.Json.encodeToBuffer; @@ -13,78 +12,78 @@ public class JsonCodecLoaderTest { public void booleanCodecTest() { MyBooleanPojo pojo = new MyBooleanPojo(); pojo.setValue(true); - assertEquals(encodeToBuffer(true), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); - assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(true), MyBooleanPojo.class)); + assertEquals(encodeToBuffer(true), JsonCodecMapper.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecMapper.decodeBuffer(encodeToBuffer(true), MyBooleanPojo.class)); } @Test(expected = ClassCastException.class) public void booleanCodecWrongTypeTest() { - JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer("aaa"), MyBooleanPojo.class); + JsonCodecMapper.decodeBuffer(encodeToBuffer("aaa"), MyBooleanPojo.class); } @Test public void doubleCodecTest() { MyDoublePojo pojo = new MyDoublePojo(); pojo.setValue(1.2d); - assertEquals(encodeToBuffer(1.2d), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); - assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(1.2d), MyDoublePojo.class)); + assertEquals(encodeToBuffer(1.2d), JsonCodecMapper.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecMapper.decodeBuffer(encodeToBuffer(1.2d), MyDoublePojo.class)); } @Test(expected = ClassCastException.class) public void doubleCodecWrongTypeTest() { - JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(""), MyDoublePojo.class); + JsonCodecMapper.decodeBuffer(encodeToBuffer(""), MyDoublePojo.class); } @Test public void floatCodecTest() { MyFloatPojo pojo = new MyFloatPojo(); pojo.setValue(1.2f); - assertEquals(encodeToBuffer(1.2f), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); - assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(1.2f), MyFloatPojo.class)); + assertEquals(encodeToBuffer(1.2f), JsonCodecMapper.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecMapper.decodeBuffer(encodeToBuffer(1.2f), MyFloatPojo.class)); } @Test(expected = ClassCastException.class) public void floatCodecWrongTypeTest() { - JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(""), MyFloatPojo.class); + JsonCodecMapper.decodeBuffer(encodeToBuffer(""), MyFloatPojo.class); } @Test public void intCodecTest() { MyIntegerPojo pojo = new MyIntegerPojo(); pojo.setValue(1); - assertEquals(encodeToBuffer((int)1), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); - assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer((int)1), MyIntegerPojo.class)); + assertEquals(encodeToBuffer((int)1), JsonCodecMapper.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecMapper.decodeBuffer(encodeToBuffer((int)1), MyIntegerPojo.class)); } @Test(expected = ClassCastException.class) public void intCodecWrongTypeTest() { - JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(""), MyIntegerPojo.class); + JsonCodecMapper.decodeBuffer(encodeToBuffer(""), MyIntegerPojo.class); } @Test public void longCodecTest() { MyLongPojo pojo = new MyLongPojo(); pojo.setValue(1L); - assertEquals(encodeToBuffer(1L), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); - assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(1L), MyLongPojo.class)); + assertEquals(encodeToBuffer(1L), JsonCodecMapper.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecMapper.decodeBuffer(encodeToBuffer(1L), MyLongPojo.class)); } @Test(expected = ClassCastException.class) public void longCodecWrongTypeTest() { - JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(""), MyLongPojo.class); + JsonCodecMapper.decodeBuffer(encodeToBuffer(""), MyLongPojo.class); } @Test public void shortCodecTest() { MyShortPojo pojo = new MyShortPojo(); pojo.setValue((short)1); - assertEquals(encodeToBuffer((short)1), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); - assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer((short)1), MyShortPojo.class)); + assertEquals(encodeToBuffer((short)1), JsonCodecMapper.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecMapper.decodeBuffer(encodeToBuffer((short)1), MyShortPojo.class)); } @Test(expected = ClassCastException.class) public void shortCodecWrongTypeTest() { - JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(""), MyShortPojo.class); + JsonCodecMapper.decodeBuffer(encodeToBuffer(""), MyShortPojo.class); } @Test @@ -92,13 +91,13 @@ public class JsonCodecLoaderTest { MyJsonArrayPojo pojo = new MyJsonArrayPojo(); JsonArray array = new JsonArray().add(1).add(2).add(3); pojo.setValue(array); - assertEquals(array.toBuffer(), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); - assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(array.toBuffer(), MyJsonArrayPojo.class)); + assertEquals(array.toBuffer(), JsonCodecMapper.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecMapper.decodeBuffer(array.toBuffer(), MyJsonArrayPojo.class)); } @Test(expected = ClassCastException.class) public void jsonArrayCodecWrongTypeTest() { - JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(2), MyJsonArrayPojo.class); + JsonCodecMapper.decodeBuffer(encodeToBuffer(2), MyJsonArrayPojo.class); } @Test @@ -106,13 +105,13 @@ public class JsonCodecLoaderTest { MyJsonObjectPojo pojo = new MyJsonObjectPojo(); JsonObject obj = new JsonObject().put("a", 1).put("b", "c"); pojo.setValue(obj); - assertEquals(obj.toBuffer(), JsonCodecLoader.INSTANCE.encodeBuffer(pojo)); - assertEquals(pojo, JsonCodecLoader.INSTANCE.decodeBuffer(obj.toBuffer(), MyJsonObjectPojo.class)); + assertEquals(obj.toBuffer(), JsonCodecMapper.encodeBuffer(pojo)); + assertEquals(pojo, JsonCodecMapper.decodeBuffer(obj.toBuffer(), MyJsonObjectPojo.class)); } @Test(expected = ClassCastException.class) public void jsonObjectCodecWrongTypeTest() { - JsonCodecLoader.INSTANCE.decodeBuffer(encodeToBuffer(2), MyJsonObjectPojo.class); + JsonCodecMapper.decodeBuffer(encodeToBuffer(2), MyJsonObjectPojo.class); } } From 812505053dcf42dea82b0c1fb0770fbbcefd282f Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Wed, 5 Jun 2019 17:37:50 +0200 Subject: [PATCH 9/9] Regen code --- .../core/DeploymentOptionsConverter.java | 13 +++++- .../io/vertx/core/VertxOptionsConverter.java | 33 +++++++++----- .../io/vertx/core/cli/ArgumentConverter.java | 13 +++++- .../io/vertx/core/cli/OptionConverter.java | 13 +++++- .../DatagramSocketOptionsConverter.java | 13 +++++- .../dns/AddressResolverOptionsConverter.java | 13 +++++- .../core/dns/DnsClientOptionsConverter.java | 13 +++++- .../eventbus/DeliveryOptionsConverter.java | 22 +++++++++ .../eventbus/EventBusOptionsConverter.java | 45 +++++++++++-------- .../vertx/core/file/CopyOptionsConverter.java | 19 +++++++- .../core/file/FileSystemOptionsConverter.java | 13 +++++- .../vertx/core/file/OpenOptionsConverter.java | 19 +++++++- .../io/vertx/core/http/GoAwayConverter.java | 13 +++++- .../core/http/Http2SettingsConverter.java | 13 +++++- .../core/http/HttpClientOptionsConverter.java | 17 +++++-- .../core/http/HttpServerOptionsConverter.java | 17 +++++-- .../core/http/RequestOptionsConverter.java | 13 +++++- .../core/http/StreamPriorityConverter.java | 22 +++++++++ .../WebSocketConnectOptionsConverter.java | 13 +++++- .../core/metrics/MetricsOptionsConverter.java | 13 +++++- .../core/net/ClientOptionsBaseConverter.java | 15 +++++-- .../net/JdkSSLEngineOptionsConverter.java | 22 +++++++++ .../vertx/core/net/JksOptionsConverter.java | 13 +++++- .../core/net/NetClientOptionsConverter.java | 13 +++++- .../core/net/NetServerOptionsConverter.java | 13 +++++- .../core/net/NetworkOptionsConverter.java | 11 ++++- .../net/OpenSSLEngineOptionsConverter.java | 13 +++++- .../core/net/PemKeyCertOptionsConverter.java | 13 +++++- .../core/net/PemTrustOptionsConverter.java | 13 +++++- .../vertx/core/net/PfxOptionsConverter.java | 13 +++++- .../vertx/core/net/ProxyOptionsConverter.java | 13 +++++- .../core/net/TCPSSLOptionsConverter.java | 43 ++++++++++-------- .../core/tracing/TracingOptionsConverter.java | 13 +++++- 33 files changed, 450 insertions(+), 108 deletions(-) create mode 100644 src/main/generated/io/vertx/core/eventbus/DeliveryOptionsConverter.java create mode 100644 src/main/generated/io/vertx/core/http/StreamPriorityConverter.java create mode 100644 src/main/generated/io/vertx/core/net/JdkSSLEngineOptionsConverter.java diff --git a/src/main/generated/io/vertx/core/DeploymentOptionsConverter.java b/src/main/generated/io/vertx/core/DeploymentOptionsConverter.java index c6638904d..67406ecbc 100644 --- a/src/main/generated/io/vertx/core/DeploymentOptionsConverter.java +++ b/src/main/generated/io/vertx/core/DeploymentOptionsConverter.java @@ -4,12 +4,21 @@ import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonArray; import java.time.Instant; import java.time.format.DateTimeFormatter; +import io.vertx.core.spi.json.JsonCodec; /** - * Converter for {@link io.vertx.core.DeploymentOptions}. + * Converter and Codec for {@link io.vertx.core.DeploymentOptions}. * NOTE: This class has been automatically generated from the {@link io.vertx.core.DeploymentOptions} original class using Vert.x codegen. */ - class DeploymentOptionsConverter { +public class DeploymentOptionsConverter implements JsonCodec { + + public static final DeploymentOptionsConverter INSTANCE = new DeploymentOptionsConverter(); + + @Override public JsonObject encode(DeploymentOptions value) { return (value != null) ? value.toJson() : null; } + + @Override public DeploymentOptions decode(JsonObject value) { return (value != null) ? new DeploymentOptions(value) : null; } + + @Override public Class getTargetClass() { return DeploymentOptions.class; } static void fromJson(Iterable> json, DeploymentOptions obj) { for (java.util.Map.Entry member : json) { diff --git a/src/main/generated/io/vertx/core/VertxOptionsConverter.java b/src/main/generated/io/vertx/core/VertxOptionsConverter.java index 0a8a122ee..dab19b406 100644 --- a/src/main/generated/io/vertx/core/VertxOptionsConverter.java +++ b/src/main/generated/io/vertx/core/VertxOptionsConverter.java @@ -4,19 +4,28 @@ import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonArray; import java.time.Instant; import java.time.format.DateTimeFormatter; +import io.vertx.core.spi.json.JsonCodec; /** - * Converter for {@link io.vertx.core.VertxOptions}. + * Converter and Codec for {@link io.vertx.core.VertxOptions}. * NOTE: This class has been automatically generated from the {@link io.vertx.core.VertxOptions} original class using Vert.x codegen. */ - class VertxOptionsConverter { +public class VertxOptionsConverter implements JsonCodec { + + public static final VertxOptionsConverter INSTANCE = new VertxOptionsConverter(); + + @Override public JsonObject encode(VertxOptions value) { return (value != null) ? value.toJson() : null; } + + @Override public VertxOptions decode(JsonObject value) { return (value != null) ? new VertxOptions(value) : null; } + + @Override public Class getTargetClass() { return VertxOptions.class; } static void fromJson(Iterable> json, VertxOptions obj) { for (java.util.Map.Entry member : json) { switch (member.getKey()) { case "addressResolverOptions": if (member.getValue() instanceof JsonObject) { - obj.setAddressResolverOptions(new io.vertx.core.dns.AddressResolverOptions((JsonObject)member.getValue())); + obj.setAddressResolverOptions(io.vertx.core.dns.AddressResolverOptionsConverter.INSTANCE.decode((JsonObject)member.getValue())); } break; case "blockedThreadCheckInterval": @@ -66,7 +75,7 @@ import java.time.format.DateTimeFormatter; break; case "eventBusOptions": if (member.getValue() instanceof JsonObject) { - obj.setEventBusOptions(new io.vertx.core.eventbus.EventBusOptions((JsonObject)member.getValue())); + obj.setEventBusOptions(io.vertx.core.eventbus.EventBusOptionsConverter.INSTANCE.decode((JsonObject)member.getValue())); } break; case "eventLoopPoolSize": @@ -76,7 +85,7 @@ import java.time.format.DateTimeFormatter; break; case "fileSystemOptions": if (member.getValue() instanceof JsonObject) { - obj.setFileSystemOptions(new io.vertx.core.file.FileSystemOptions((JsonObject)member.getValue())); + obj.setFileSystemOptions(io.vertx.core.file.FileSystemOptionsConverter.INSTANCE.decode((JsonObject)member.getValue())); } break; case "haEnabled": @@ -116,7 +125,7 @@ import java.time.format.DateTimeFormatter; break; case "metricsOptions": if (member.getValue() instanceof JsonObject) { - obj.setMetricsOptions(new io.vertx.core.metrics.MetricsOptions((JsonObject)member.getValue())); + obj.setMetricsOptions(io.vertx.core.metrics.MetricsOptionsConverter.INSTANCE.decode((JsonObject)member.getValue())); } break; case "preferNativeTransport": @@ -131,7 +140,7 @@ import java.time.format.DateTimeFormatter; break; case "tracingOptions": if (member.getValue() instanceof JsonObject) { - obj.setTracingOptions(new io.vertx.core.tracing.TracingOptions((JsonObject)member.getValue())); + obj.setTracingOptions(io.vertx.core.tracing.TracingOptionsConverter.INSTANCE.decode((JsonObject)member.getValue())); } break; case "warningExceptionTime": @@ -159,7 +168,7 @@ import java.time.format.DateTimeFormatter; static void toJson(VertxOptions obj, java.util.Map json) { if (obj.getAddressResolverOptions() != null) { - json.put("addressResolverOptions", obj.getAddressResolverOptions().toJson()); + json.put("addressResolverOptions", io.vertx.core.dns.AddressResolverOptionsConverter.INSTANCE.encode(obj.getAddressResolverOptions())); } json.put("blockedThreadCheckInterval", obj.getBlockedThreadCheckInterval()); if (obj.getBlockedThreadCheckIntervalUnit() != null) { @@ -177,11 +186,11 @@ import java.time.format.DateTimeFormatter; json.put("clusterPublicPort", obj.getClusterPublicPort()); json.put("clustered", obj.isClustered()); if (obj.getEventBusOptions() != null) { - json.put("eventBusOptions", obj.getEventBusOptions().toJson()); + json.put("eventBusOptions", io.vertx.core.eventbus.EventBusOptionsConverter.INSTANCE.encode(obj.getEventBusOptions())); } json.put("eventLoopPoolSize", obj.getEventLoopPoolSize()); if (obj.getFileSystemOptions() != null) { - json.put("fileSystemOptions", obj.getFileSystemOptions().toJson()); + json.put("fileSystemOptions", io.vertx.core.file.FileSystemOptionsConverter.INSTANCE.encode(obj.getFileSystemOptions())); } json.put("haEnabled", obj.isHAEnabled()); if (obj.getHAGroup() != null) { @@ -197,12 +206,12 @@ import java.time.format.DateTimeFormatter; json.put("maxWorkerExecuteTimeUnit", obj.getMaxWorkerExecuteTimeUnit().name()); } if (obj.getMetricsOptions() != null) { - json.put("metricsOptions", obj.getMetricsOptions().toJson()); + json.put("metricsOptions", io.vertx.core.metrics.MetricsOptionsConverter.INSTANCE.encode(obj.getMetricsOptions())); } json.put("preferNativeTransport", obj.getPreferNativeTransport()); json.put("quorumSize", obj.getQuorumSize()); if (obj.getTracingOptions() != null) { - json.put("tracingOptions", obj.getTracingOptions().toJson()); + json.put("tracingOptions", io.vertx.core.tracing.TracingOptionsConverter.INSTANCE.encode(obj.getTracingOptions())); } json.put("warningExceptionTime", obj.getWarningExceptionTime()); if (obj.getWarningExceptionTimeUnit() != null) { diff --git a/src/main/generated/io/vertx/core/cli/ArgumentConverter.java b/src/main/generated/io/vertx/core/cli/ArgumentConverter.java index a230bbcc4..5edf9f1b5 100644 --- a/src/main/generated/io/vertx/core/cli/ArgumentConverter.java +++ b/src/main/generated/io/vertx/core/cli/ArgumentConverter.java @@ -4,12 +4,21 @@ import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonArray; import java.time.Instant; import java.time.format.DateTimeFormatter; +import io.vertx.core.spi.json.JsonCodec; /** - * Converter for {@link io.vertx.core.cli.Argument}. + * Converter and Codec for {@link io.vertx.core.cli.Argument}. * NOTE: This class has been automatically generated from the {@link io.vertx.core.cli.Argument} original class using Vert.x codegen. */ - class ArgumentConverter { +public class ArgumentConverter implements JsonCodec { + + public static final ArgumentConverter INSTANCE = new ArgumentConverter(); + + @Override public JsonObject encode(Argument value) { return (value != null) ? value.toJson() : null; } + + @Override public Argument decode(JsonObject value) { return (value != null) ? new Argument(value) : null; } + + @Override public Class getTargetClass() { return Argument.class; } static void fromJson(Iterable> json, Argument obj) { for (java.util.Map.Entry member : json) { diff --git a/src/main/generated/io/vertx/core/cli/OptionConverter.java b/src/main/generated/io/vertx/core/cli/OptionConverter.java index 223b26a2a..581278469 100644 --- a/src/main/generated/io/vertx/core/cli/OptionConverter.java +++ b/src/main/generated/io/vertx/core/cli/OptionConverter.java @@ -4,12 +4,21 @@ import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonArray; import java.time.Instant; import java.time.format.DateTimeFormatter; +import io.vertx.core.spi.json.JsonCodec; /** - * Converter for {@link io.vertx.core.cli.Option}. + * Converter and Codec for {@link io.vertx.core.cli.Option}. * NOTE: This class has been automatically generated from the {@link io.vertx.core.cli.Option} original class using Vert.x codegen. */ - class OptionConverter { +public class OptionConverter implements JsonCodec { + + public static final OptionConverter INSTANCE = new OptionConverter(); + + @Override public JsonObject encode(Option value) { return (value != null) ? value.toJson() : null; } + + @Override public Option decode(JsonObject value) { return (value != null) ? new Option(value) : null; } + + @Override public Class