diff --git a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java index 87f42c75..4ce308af 100644 --- a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java +++ b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java @@ -872,8 +872,11 @@ public void afterParsed(final Class clazz) { private boolean isReversedAdapter(final Class payloadType, final Class aClass, final Adapter instance) { if (TypeAwareAdapter.class.isInstance(instance)) { - return payloadType.isAssignableFrom(Class.class.cast(TypeAwareAdapter.class.cast(instance).getTo())) - && !payloadType.isAssignableFrom(Class.class.cast(TypeAwareAdapter.class.cast(instance).getFrom())); + final Class to = toClass(TypeAwareAdapter.class.cast(instance).getTo()); + final Class from = toClass(TypeAwareAdapter.class.cast(instance).getFrom()); + return to != null && from != null + && payloadType.isAssignableFrom(to) + && !payloadType.isAssignableFrom(from); } final Type[] genericInterfaces = aClass.getGenericInterfaces(); return Stream.of(genericInterfaces).filter(ParameterizedType.class::isInstance) @@ -889,6 +892,16 @@ private boolean isReversedAdapter(final Class payloadType, final Class aCl }); } + private Class toClass(final Type type) { + if (Class.class.isInstance(type)) { + return Class.class.cast(type); + } + if (ParameterizedType.class.isInstance(type)) { + return toClass(ParameterizedType.class.cast(type).getRawType()); + } + return null; + } + private boolean isNillable(final JsonbProperty property, final JsonbNillable propertyNillable, final JsonbNillable classOrPackageNillable) { if (propertyNillable != null) { return propertyNillable.value(); diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/array/ArrayAdapterOnClassDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/array/ArrayAdapterOnClassDirectTest.java index 2249feba..4d1b4eeb 100644 --- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/array/ArrayAdapterOnClassDirectTest.java +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/array/ArrayAdapterOnClassDirectTest.java @@ -18,8 +18,6 @@ import jakarta.json.bind.Jsonb; import jakarta.json.bind.JsonbBuilder; -import org.junit.Ignore; -import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -42,41 +40,4 @@ public void assertRead(final Jsonb jsonb) { assertEquals("test@domain.com:EmailClass.adaptFromJson", email.toString()); assertEquals("EmailClass.adaptFromJson", calls()); } - - /** - * Fails as the adapter is not found - */ - @Test - @Ignore() - @Override - public void read() throws Exception { - super.read(); - } - - /** - * Fails as the adapter is not found - */ - @Test - @Ignore() - @Override - public void readAfterRead() throws Exception { - super.readAfterRead(); - } - - /** - * Fails as the adapter is not found on the first read - */ - @Test - @Ignore() - @Override - public void writeAfterRead() throws Exception { - super.writeAfterRead(); - } - - @Test - @Ignore() - @Override - public void readAfterWrite() throws Exception { - super.readAfterWrite(); - } } diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/array/ArrayAdapterPrecedenceConfigClassDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/array/ArrayAdapterPrecedenceConfigClassDirectTest.java index e3bfd572..32dd64af 100644 --- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/array/ArrayAdapterPrecedenceConfigClassDirectTest.java +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/array/ArrayAdapterPrecedenceConfigClassDirectTest.java @@ -19,8 +19,6 @@ import jakarta.json.bind.Jsonb; import jakarta.json.bind.JsonbBuilder; import jakarta.json.bind.JsonbConfig; -import org.junit.Ignore; -import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -57,32 +55,4 @@ public void assertWrite(final Jsonb jsonb) { assertEquals("[\"test\",\"domain.com\",\"Config.adaptToJson\"]", json); assertEquals("Config.adaptToJson", calls()); } - - @Test - @Ignore - @Override - public void read() throws Exception { - super.read(); - } - - @Test - @Ignore - @Override - public void readAfterRead() throws Exception { - super.readAfterRead(); - } - - @Test - @Ignore - @Override - public void readAfterWrite() throws Exception { - super.readAfterWrite(); - } - - @Test - @Ignore - @Override - public void writeAfterRead() throws Exception { - super.writeAfterRead(); - } } diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/map/MapAdapterOnClassDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/map/MapAdapterOnClassDirectTest.java index a8905462..8e7cd886 100644 --- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/map/MapAdapterOnClassDirectTest.java +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/map/MapAdapterOnClassDirectTest.java @@ -18,11 +18,9 @@ import jakarta.json.bind.Jsonb; import jakarta.json.bind.JsonbBuilder; -import org.junit.Ignore; import static org.junit.Assert.assertEquals; -@Ignore("java.lang.ClassCastException: Cannot cast sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl to java.lang.Class") public class MapAdapterOnClassDirectTest extends MapAdapterOnClassTest { public Jsonb jsonb() { diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/map/MapAdapterPrecedenceConfigClassDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/map/MapAdapterPrecedenceConfigClassDirectTest.java index 3d59920d..97c1388a 100644 --- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/map/MapAdapterPrecedenceConfigClassDirectTest.java +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/map/MapAdapterPrecedenceConfigClassDirectTest.java @@ -19,8 +19,6 @@ import jakarta.json.bind.Jsonb; import jakarta.json.bind.JsonbBuilder; import jakarta.json.bind.JsonbConfig; -import org.junit.Ignore; -import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -31,7 +29,7 @@ * * * Outcome: - * - Reads fail + * - Config wins on read * - Config wins on write */ public class MapAdapterPrecedenceConfigClassDirectTest extends MapAdapterOnClassTest { @@ -57,56 +55,4 @@ public void assertWrite(final Jsonb jsonb) { assertEquals("{\"user\":\"test\",\"domain\":\"domain.com\",\"call\":\"Config.adaptToJson\"}", json); assertEquals("Config.adaptToJson", calls()); } - - /** - * java.lang.ClassCastException: Cannot cast sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl to java.lang.Class - * - * at java.base/java.lang.Class.cast(Class.java:3889) - * at org.apache.johnzon.jsonb.JsonbAccessMode.isReversedAdapter(JsonbAccessMode.java:875) - */ - @Test - @Ignore - @Override - public void read() throws Exception { - super.read(); - } - - /** - * java.lang.ClassCastException: Cannot cast sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl to java.lang.Class - * - * at java.base/java.lang.Class.cast(Class.java:3889) - * at org.apache.johnzon.jsonb.JsonbAccessMode.isReversedAdapter(JsonbAccessMode.java:875) - */ - @Test - @Ignore - @Override - public void readAfterRead() throws Exception { - super.readAfterRead(); - } - - /** - * java.lang.ClassCastException: Cannot cast sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl to java.lang.Class - * - * at java.base/java.lang.Class.cast(Class.java:3889) - * at org.apache.johnzon.jsonb.JsonbAccessMode.isReversedAdapter(JsonbAccessMode.java:875) - */ - @Test - @Ignore - @Override - public void readAfterWrite() throws Exception { - super.readAfterWrite(); - } - - /** - * java.lang.ClassCastException: Cannot cast sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl to java.lang.Class - * - * at java.base/java.lang.Class.cast(Class.java:3889) - * at org.apache.johnzon.jsonb.JsonbAccessMode.isReversedAdapter(JsonbAccessMode.java:875) - */ - @Test - @Ignore - @Override - public void writeAfterRead() throws Exception { - super.writeAfterRead(); - } } diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/string/StringAdapterOnClassDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/string/StringAdapterOnClassDirectTest.java index 7266a76e..1cf59fc9 100644 --- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/string/StringAdapterOnClassDirectTest.java +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/adapter/string/StringAdapterOnClassDirectTest.java @@ -18,8 +18,6 @@ import jakarta.json.bind.Jsonb; import jakarta.json.bind.JsonbBuilder; -import org.junit.Ignore; -import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -42,34 +40,4 @@ public void assertRead(final Jsonb jsonb) { assertEquals("test@domain.com:EmailClass.adaptFromJson", email.toString()); assertEquals("EmailClass.adaptFromJson", calls()); } - - /** - * Fails as the adapter is not found - */ - @Test - @Ignore() - @Override - public void read() throws Exception { - super.read(); - } - - /** - * Fails as the adapter is not found - */ - @Test - @Ignore() - @Override - public void readAfterRead() throws Exception { - super.readAfterRead(); - } - - /** - * Fails as the adapter is not found on the first read - */ - @Test - @Ignore() - @Override - public void writeAfterRead() throws Exception { - super.writeAfterRead(); - } } diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigDecimalDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigDecimalDirectTest.java new file mode 100644 index 00000000..e2cb46b1 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigDecimalDirectTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import jakarta.json.bind.JsonbBuilder; +import jakarta.json.bind.JsonbConfig; +import java.math.BigDecimal; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 — BigDecimal maps to JSON Number. + * I-JSON (RFC 7493 Section 2.2): values exceeding IEEE 754 double + * precision are serialized as strings. 1.5 fits in double → JSON Number. + * + *

Johnzon defaults to string adapter ON (non-spec). We explicitly + * disable it to test spec-compliant behavior.

+ * + * @see ee.jakarta.tck.json.bind.defaultmapping.bignumbers.BigNumbersMappingTest + */ +public class BigDecimalDirectTest extends BuiltInSymmetryTest { + + @Override + public Jsonb jsonb() { + return JsonbBuilder.create(new JsonbConfig() + .setProperty("johnzon.use-bigdecimal-stringadapter", false)); + } + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("1.5", jsonb.toJson(new BigDecimal("1.5"))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(new BigDecimal("1.5"), jsonb.fromJson("1.5", BigDecimal.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigDecimalSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigDecimalSimpleTest.java new file mode 100644 index 00000000..7b08be6f --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigDecimalSimpleTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import jakarta.json.bind.JsonbBuilder; +import jakarta.json.bind.JsonbConfig; +import java.math.BigDecimal; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 — BigDecimal maps to JSON Number. + * I-JSON (RFC 7493 Section 2.2): values exceeding IEEE 754 double + * precision are serialized as strings. 1.5 fits in double → JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.bignumbers.BigNumbersMappingTest + */ +public class BigDecimalSimpleTest extends BuiltInSymmetryTest { + + @Override + public Jsonb jsonb() { + return JsonbBuilder.create(new JsonbConfig() + .setProperty("johnzon.use-bigdecimal-stringadapter", false)); + } + + public static class Widget { + + private BigDecimal value; + + public BigDecimal getValue() { + return value; + } + + public void setValue(final BigDecimal value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(new BigDecimal("1.5")); + assertEquals("{\"value\":1.5}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":1.5}", Widget.class); + assertEquals(new BigDecimal("1.5"), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigIntegerDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigIntegerDirectTest.java new file mode 100644 index 00000000..58537ce9 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigIntegerDirectTest.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import jakarta.json.bind.JsonbBuilder; +import jakarta.json.bind.JsonbConfig; +import java.math.BigInteger; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 — BigInteger maps to JSON Number. + * I-JSON (RFC 7493 Section 2.2): values exceeding IEEE 754 double + * precision are serialized as strings. 42 fits in double → JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.bignumbers.BigNumbersMappingTest + */ +public class BigIntegerDirectTest extends BuiltInSymmetryTest { + + @Override + public Jsonb jsonb() { + return JsonbBuilder.create(new JsonbConfig() + .setProperty("johnzon.use-biginteger-stringadapter", false)); + } + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("42", jsonb.toJson(BigInteger.valueOf(42))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(BigInteger.valueOf(42), jsonb.fromJson("42", BigInteger.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigIntegerSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigIntegerSimpleTest.java new file mode 100644 index 00000000..386b06bb --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BigIntegerSimpleTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import jakarta.json.bind.JsonbBuilder; +import jakarta.json.bind.JsonbConfig; +import java.math.BigInteger; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 — BigInteger maps to JSON Number. + * I-JSON (RFC 7493 Section 2.2): values exceeding IEEE 754 double + * precision are serialized as strings. 42 fits in double → JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.bignumbers.BigNumbersMappingTest + */ +public class BigIntegerSimpleTest extends BuiltInSymmetryTest { + + @Override + public Jsonb jsonb() { + return JsonbBuilder.create(new JsonbConfig() + .setProperty("johnzon.use-biginteger-stringadapter", false)); + } + + public static class Widget { + + private BigInteger value; + + public BigInteger getValue() { + return value; + } + + public void setValue(final BigInteger value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(BigInteger.valueOf(42)); + assertEquals("{\"value\":42}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":42}", Widget.class); + assertEquals(BigInteger.valueOf(42), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BooleanDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BooleanDirectTest.java new file mode 100644 index 00000000..113c8bf4 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BooleanDirectTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 3 — Boolean maps to JSON true/false. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class BooleanDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("true", jsonb.toJson(true)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(true, jsonb.fromJson("true", Boolean.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BooleanSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BooleanSimpleTest.java new file mode 100644 index 00000000..15ea7790 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BooleanSimpleTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 3 — Boolean maps to JSON true/false. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class BooleanSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Boolean value; + + public Boolean getValue() { + return value; + } + + public void setValue(final Boolean value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(true); + assertEquals("{\"value\":true}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":true}", Widget.class); + assertEquals(true, widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BuiltInSymmetryTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BuiltInSymmetryTest.java new file mode 100644 index 00000000..547af467 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/BuiltInSymmetryTest.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import jakarta.json.bind.JsonbBuilder; +import org.apache.johnzon.jsonb.symmetry.SymmetryTest; + +public abstract class BuiltInSymmetryTest extends SymmetryTest { + + @Override + public Jsonb jsonb() { + return JsonbBuilder.create(); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ByteDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ByteDirectTest.java new file mode 100644 index 00000000..31f6fd94 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ByteDirectTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Byte maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class ByteDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("42", jsonb.toJson((byte) 42)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Byte.valueOf((byte) 42), jsonb.fromJson("42", Byte.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ByteSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ByteSimpleTest.java new file mode 100644 index 00000000..9c53ed1c --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ByteSimpleTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Byte maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class ByteSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Byte value; + + public Byte getValue() { + return value; + } + + public void setValue(final Byte value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue((byte) 42); + assertEquals("{\"value\":42}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":42}", Widget.class); + assertEquals(Byte.valueOf((byte) 42), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/CharacterDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/CharacterDirectTest.java new file mode 100644 index 00000000..42c2b4eb --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/CharacterDirectTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 7 — Character maps to JSON String. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class CharacterDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"a\"", jsonb.toJson('a')); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Character.valueOf('a'), jsonb.fromJson("\"a\"", Character.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/CharacterSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/CharacterSimpleTest.java new file mode 100644 index 00000000..9a993818 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/CharacterSimpleTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 7 — Character maps to JSON String. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class CharacterSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Character value; + + public Character getValue() { + return value; + } + + public void setValue(final Character value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue('a'); + assertEquals("{\"value\":\"a\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"a\"}", Widget.class); + assertEquals(Character.valueOf('a'), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DateDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DateDirectTest.java new file mode 100644 index 00000000..28f5d8cc --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DateDirectTest.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.Instant; +import java.util.Date; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — java.util.Date uses DateTimeFormatter.ISO_DATE_TIME format. + * Expected format includes zone ID: {@code 2024-01-15T10:30:45Z[UTC]}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testDateWithTimeMapping() + */ +public class DateDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"2024-01-15T10:30:45Z[UTC]\"", jsonb.toJson(Date.from(Instant.parse("2024-01-15T10:30:45Z")))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Date.from(Instant.parse("2024-01-15T10:30:45Z")), jsonb.fromJson("\"2024-01-15T10:30:45Z[UTC]\"", Date.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DateSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DateSimpleTest.java new file mode 100644 index 00000000..665e2aae --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DateSimpleTest.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.Instant; +import java.util.Date; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — java.util.Date uses DateTimeFormatter.ISO_DATE_TIME format. + * Expected format includes zone ID: {@code 2024-01-15T10:30:45Z[UTC]}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testDateWithTimeMapping() + */ +public class DateSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Date value; + + public Date getValue() { + return value; + } + + public void setValue(final Date value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(Date.from(Instant.parse("2024-01-15T10:30:45Z"))); + assertEquals("{\"value\":\"2024-01-15T10:30:45Z[UTC]\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"2024-01-15T10:30:45Z[UTC]\"}", Widget.class); + assertEquals(Date.from(Instant.parse("2024-01-15T10:30:45Z")), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DoubleDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DoubleDirectTest.java new file mode 100644 index 00000000..c8372e15 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DoubleDirectTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Double maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class DoubleDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("1.5", jsonb.toJson(1.5)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Double.valueOf(1.5), jsonb.fromJson("1.5", Double.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DoubleSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DoubleSimpleTest.java new file mode 100644 index 00000000..f469a7e3 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DoubleSimpleTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Double maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class DoubleSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Double value; + + public Double getValue() { + return value; + } + + public void setValue(final Double value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(1.5); + assertEquals("{\"value\":1.5}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":1.5}", Widget.class); + assertEquals(Double.valueOf(1.5), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DurationDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DurationDirectTest.java new file mode 100644 index 00000000..9e90b210 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DurationDirectTest.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.Duration; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — Duration uses ISO 8601 duration format: {@code PTnHnMnS}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testDurationMapping() + */ +public class DurationDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"PT1H30M\"", jsonb.toJson(Duration.parse("PT1H30M"))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Duration.parse("PT1H30M"), jsonb.fromJson("\"PT1H30M\"", Duration.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DurationSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DurationSimpleTest.java new file mode 100644 index 00000000..78af0864 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/DurationSimpleTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.Duration; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — Duration uses ISO 8601 duration format: {@code PTnHnMnS}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testDurationMapping() + */ +public class DurationSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Duration value; + + public Duration getValue() { + return value; + } + + public void setValue(final Duration value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(Duration.parse("PT1H30M")); + assertEquals("{\"value\":\"PT1H30M\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"PT1H30M\"}", Widget.class); + assertEquals(Duration.parse("PT1H30M"), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/EnumDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/EnumDirectTest.java new file mode 100644 index 00000000..cfd3c73a --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/EnumDirectTest.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 — Enum maps to JSON String via {@code Enum.name()}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.enums + */ +public class EnumDirectTest extends BuiltInSymmetryTest { + + public enum Color { + RED, GREEN, BLUE + } + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"RED\"", jsonb.toJson(Color.RED)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Color.RED, jsonb.fromJson("\"RED\"", Color.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/EnumSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/EnumSimpleTest.java new file mode 100644 index 00000000..262c6fcd --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/EnumSimpleTest.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 — Enum maps to JSON String via {@code Enum.name()}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.enums + */ +public class EnumSimpleTest extends BuiltInSymmetryTest { + + public enum Color { + RED, GREEN, BLUE + } + + public static class Widget { + + private Color value; + + public Color getValue() { + return value; + } + + public void setValue(final Color value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(Color.RED); + assertEquals("{\"value\":\"RED\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"RED\"}", Widget.class); + assertEquals(Color.RED, widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/FloatDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/FloatDirectTest.java new file mode 100644 index 00000000..e380b4da --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/FloatDirectTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Float maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class FloatDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("1.5", jsonb.toJson(1.5f)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Float.valueOf(1.5f), jsonb.fromJson("1.5", Float.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/FloatSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/FloatSimpleTest.java new file mode 100644 index 00000000..1db9aa92 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/FloatSimpleTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Float maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class FloatSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Float value; + + public Float getValue() { + return value; + } + + public void setValue(final Float value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(1.5f); + assertEquals("{\"value\":1.5}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":1.5}", Widget.class); + assertEquals(Float.valueOf(1.5f), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/InstantDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/InstantDirectTest.java new file mode 100644 index 00000000..e8dd07ff --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/InstantDirectTest.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.Instant; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — Instant uses ISO 8601 format: {@code yyyy-MM-dd'T'HH:mm:ssZ}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testInstantMapping() + */ +public class InstantDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"2024-01-15T10:30:45Z\"", jsonb.toJson(Instant.parse("2024-01-15T10:30:45Z"))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Instant.parse("2024-01-15T10:30:45Z"), jsonb.fromJson("\"2024-01-15T10:30:45Z\"", Instant.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/InstantSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/InstantSimpleTest.java new file mode 100644 index 00000000..604df451 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/InstantSimpleTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.Instant; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — Instant uses ISO 8601 format: {@code yyyy-MM-dd'T'HH:mm:ssZ}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testInstantMapping() + */ +public class InstantSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Instant value; + + public Instant getValue() { + return value; + } + + public void setValue(final Instant value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(Instant.parse("2024-01-15T10:30:45Z")); + assertEquals("{\"value\":\"2024-01-15T10:30:45Z\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"2024-01-15T10:30:45Z\"}", Widget.class); + assertEquals(Instant.parse("2024-01-15T10:30:45Z"), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/IntegerDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/IntegerDirectTest.java new file mode 100644 index 00000000..ba4068d0 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/IntegerDirectTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Integer maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class IntegerDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("42", jsonb.toJson(42)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Integer.valueOf(42), jsonb.fromJson("42", Integer.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/IntegerSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/IntegerSimpleTest.java new file mode 100644 index 00000000..91226887 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/IntegerSimpleTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Integer maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class IntegerSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Integer value; + + public Integer getValue() { + return value; + } + + public void setValue(final Integer value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(42); + assertEquals("{\"value\":42}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":42}", Widget.class); + assertEquals(Integer.valueOf(42), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateDirectTest.java new file mode 100644 index 00000000..0a9a970a --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateDirectTest.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.LocalDate; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — LocalDate uses ISO_LOCAL_DATE format: {@code yyyy-MM-dd}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testLocalDateMapping() + */ +public class LocalDateDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"2024-01-15\"", jsonb.toJson(LocalDate.of(2024, 1, 15))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(LocalDate.of(2024, 1, 15), jsonb.fromJson("\"2024-01-15\"", LocalDate.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateSimpleTest.java new file mode 100644 index 00000000..f42629a3 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateSimpleTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.LocalDate; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — LocalDate uses ISO_LOCAL_DATE format: {@code yyyy-MM-dd}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testLocalDateMapping() + */ +public class LocalDateSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private LocalDate value; + + public LocalDate getValue() { + return value; + } + + public void setValue(final LocalDate value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(LocalDate.of(2024, 1, 15)); + assertEquals("{\"value\":\"2024-01-15\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"2024-01-15\"}", Widget.class); + assertEquals(LocalDate.of(2024, 1, 15), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateTimeDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateTimeDirectTest.java new file mode 100644 index 00000000..da266525 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateTimeDirectTest.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.LocalDateTime; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — LocalDate uses ISO_LOCAL_DATE format: {@code yyyy-MM-dd}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testLocalDateMapping() + */ +public class LocalDateTimeDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"2024-01-15T10:30:45\"", jsonb.toJson(LocalDateTime.of(2024, 1, 15, 10, 30, 45))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(LocalDateTime.of(2024, 1, 15, 10, 30, 45), jsonb.fromJson("\"2024-01-15T10:30:45\"", LocalDateTime.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateTimeSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateTimeSimpleTest.java new file mode 100644 index 00000000..e463e6cc --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalDateTimeSimpleTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.LocalDateTime; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — LocalDate uses ISO_LOCAL_DATE format: {@code yyyy-MM-dd}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testLocalDateMapping() + */ +public class LocalDateTimeSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private LocalDateTime value; + + public LocalDateTime getValue() { + return value; + } + + public void setValue(final LocalDateTime value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(LocalDateTime.of(2024, 1, 15, 10, 30, 45)); + assertEquals("{\"value\":\"2024-01-15T10:30:45\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"2024-01-15T10:30:45\"}", Widget.class); + assertEquals(LocalDateTime.of(2024, 1, 15, 10, 30, 45), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalTimeDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalTimeDirectTest.java new file mode 100644 index 00000000..f93b9dbe --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalTimeDirectTest.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.LocalTime; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — LocalTime uses ISO_LOCAL_TIME format: {@code HH:mm:ss}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testLocalTimeMapping() + */ +public class LocalTimeDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"10:30:45\"", jsonb.toJson(LocalTime.of(10, 30, 45))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(LocalTime.of(10, 30, 45), jsonb.fromJson("\"10:30:45\"", LocalTime.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalTimeSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalTimeSimpleTest.java new file mode 100644 index 00000000..41652779 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LocalTimeSimpleTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.LocalTime; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — LocalTime uses ISO_LOCAL_TIME format: {@code HH:mm:ss}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testLocalTimeMapping() + */ +public class LocalTimeSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private LocalTime value; + + public LocalTime getValue() { + return value; + } + + public void setValue(final LocalTime value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(LocalTime.of(10, 30, 45)); + assertEquals("{\"value\":\"10:30:45\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"10:30:45\"}", Widget.class); + assertEquals(LocalTime.of(10, 30, 45), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LongDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LongDirectTest.java new file mode 100644 index 00000000..93b9e05d --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LongDirectTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Long maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class LongDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("42", jsonb.toJson(42L)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Long.valueOf(42L), jsonb.fromJson("42", Long.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LongSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LongSimpleTest.java new file mode 100644 index 00000000..43b70688 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/LongSimpleTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Long maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class LongSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Long value; + + public Long getValue() { + return value; + } + + public void setValue(final Long value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(42L); + assertEquals("{\"value\":42}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":42}", Widget.class); + assertEquals(Long.valueOf(42L), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetDateTimeDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetDateTimeDirectTest.java new file mode 100644 index 00000000..2500fabc --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetDateTimeDirectTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — OffsetDateTime uses ISO_OFFSET_DATE_TIME format. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testOffsetDateTimeMapping() + */ +public class OffsetDateTimeDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"2024-01-15T10:30:45Z\"", jsonb.toJson(OffsetDateTime.of(2024, 1, 15, 10, 30, 45, 0, ZoneOffset.UTC))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(OffsetDateTime.of(2024, 1, 15, 10, 30, 45, 0, ZoneOffset.UTC), jsonb.fromJson("\"2024-01-15T10:30:45Z\"", OffsetDateTime.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetDateTimeSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetDateTimeSimpleTest.java new file mode 100644 index 00000000..69e63752 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetDateTimeSimpleTest.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — OffsetDateTime uses ISO_OFFSET_DATE_TIME format. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testOffsetDateTimeMapping() + */ +public class OffsetDateTimeSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private OffsetDateTime value; + + public OffsetDateTime getValue() { + return value; + } + + public void setValue(final OffsetDateTime value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(OffsetDateTime.of(2024, 1, 15, 10, 30, 45, 0, ZoneOffset.UTC)); + assertEquals("{\"value\":\"2024-01-15T10:30:45Z\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"2024-01-15T10:30:45Z\"}", Widget.class); + assertEquals(OffsetDateTime.of(2024, 1, 15, 10, 30, 45, 0, ZoneOffset.UTC), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetTimeDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetTimeDirectTest.java new file mode 100644 index 00000000..f3f2524f --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetTimeDirectTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.OffsetTime; +import java.time.ZoneOffset; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — OffsetTime uses ISO_OFFSET_TIME format. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testOffsetTimeMapping() + */ +public class OffsetTimeDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"10:30:45Z\"", jsonb.toJson(OffsetTime.of(10, 30, 45, 0, ZoneOffset.UTC))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(OffsetTime.of(10, 30, 45, 0, ZoneOffset.UTC), jsonb.fromJson("\"10:30:45Z\"", OffsetTime.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetTimeSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetTimeSimpleTest.java new file mode 100644 index 00000000..179c2498 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/OffsetTimeSimpleTest.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.OffsetTime; +import java.time.ZoneOffset; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — OffsetTime uses ISO_OFFSET_TIME format. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testOffsetTimeMapping() + */ +public class OffsetTimeSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private OffsetTime value; + + public OffsetTime getValue() { + return value; + } + + public void setValue(final OffsetTime value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(OffsetTime.of(10, 30, 45, 0, ZoneOffset.UTC)); + assertEquals("{\"value\":\"10:30:45Z\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"10:30:45Z\"}", Widget.class); + assertEquals(OffsetTime.of(10, 30, 45, 0, ZoneOffset.UTC), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/PeriodDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/PeriodDirectTest.java new file mode 100644 index 00000000..7e59cf72 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/PeriodDirectTest.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.Period; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — Period uses ISO 8601 period format: {@code PnYnMnD}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testPeriodMapping() + */ +public class PeriodDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"P1Y2M3D\"", jsonb.toJson(Period.of(1, 2, 3))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Period.of(1, 2, 3), jsonb.fromJson("\"P1Y2M3D\"", Period.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/PeriodSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/PeriodSimpleTest.java new file mode 100644 index 00000000..3fdcc504 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/PeriodSimpleTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.Period; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — Period uses ISO 8601 period format: {@code PnYnMnD}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testPeriodMapping() + */ +public class PeriodSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Period value; + + public Period getValue() { + return value; + } + + public void setValue(final Period value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(Period.of(1, 2, 3)); + assertEquals("{\"value\":\"P1Y2M3D\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"P1Y2M3D\"}", Widget.class); + assertEquals(Period.of(1, 2, 3), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ShortDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ShortDirectTest.java new file mode 100644 index 00000000..a99cf3ef --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ShortDirectTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Short maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class ShortDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("256", jsonb.toJson((short) 256)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(Short.valueOf((short) 256), jsonb.fromJson("256", Short.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ShortSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ShortSimpleTest.java new file mode 100644 index 00000000..3bf1f518 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ShortSimpleTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 6 — Short maps to JSON Number. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class ShortSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private Short value; + + public Short getValue() { + return value; + } + + public void setValue(final Short value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue((short) 256); + assertEquals("{\"value\":256}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":256}", Widget.class); + assertEquals(Short.valueOf((short) 256), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/StringDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/StringDirectTest.java new file mode 100644 index 00000000..52e1759f --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/StringDirectTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 7 — String maps to JSON String. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class StringDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"hello\"", jsonb.toJson("hello")); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals("hello", jsonb.fromJson("\"hello\"", String.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/StringSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/StringSimpleTest.java new file mode 100644 index 00000000..81029ce4 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/StringSimpleTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 / RFC 8259 Section 7 — String maps to JSON String. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.basictypes.BasicJavaTypesMappingTest + */ +public class StringSimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private String value; + + public String getValue() { + return value; + } + + public void setValue(final String value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue("hello"); + assertEquals("{\"value\":\"hello\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"hello\"}", Widget.class); + assertEquals("hello", widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/URIDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/URIDirectTest.java new file mode 100644 index 00000000..1f92f377 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/URIDirectTest.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.net.URI; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 — URI maps to JSON String via {@code URI.toString()}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.specifictypes.SpecificTypesMappingTest + */ +public class URIDirectTest extends BuiltInSymmetryTest { + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals("\"https://example.com\"", jsonb.toJson(URI.create("https://example.com"))); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(URI.create("https://example.com"), jsonb.fromJson("\"https://example.com\"", URI.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/URISimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/URISimpleTest.java new file mode 100644 index 00000000..1a78498e --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/URISimpleTest.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.net.URI; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.4 — URI maps to JSON String via {@code URI.toString()}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.specifictypes.SpecificTypesMappingTest + */ +public class URISimpleTest extends BuiltInSymmetryTest { + + public static class Widget { + + private URI value; + + public URI getValue() { + return value; + } + + public void setValue(final URI value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(URI.create("https://example.com")); + assertEquals("{\"value\":\"https://example.com\"}", jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson("{\"value\":\"https://example.com\"}", Widget.class); + assertEquals(URI.create("https://example.com"), widget.getValue()); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ZonedDateTimeDirectTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ZonedDateTimeDirectTest.java new file mode 100644 index 00000000..4617f973 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ZonedDateTimeDirectTest.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — ZonedDateTime uses ISO_ZONED_DATE_TIME format, + * which includes the zone ID in brackets: {@code 2024-01-15T10:30:45+01:00[Europe/Paris]}. + * + *

Uses Europe/Paris (CET, UTC+1 in January) like the TCK.

+ * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testZonedDateTimeMapping() + */ +public class ZonedDateTimeDirectTest extends BuiltInSymmetryTest { + + private static final ZonedDateTime VALUE = + ZonedDateTime.of(2024, 1, 15, 10, 30, 45, 0, ZoneId.of("Europe/Paris")); + + private static final String JSON = "\"2024-01-15T10:30:45+01:00[Europe/Paris]\""; + + @Override + public void assertWrite(final Jsonb jsonb) { + assertEquals(JSON, jsonb.toJson(VALUE)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + assertEquals(VALUE, jsonb.fromJson(JSON, ZonedDateTime.class)); + } +} diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ZonedDateTimeSimpleTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ZonedDateTimeSimpleTest.java new file mode 100644 index 00000000..0ebe5a7e --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/symmetry/builtin/ZonedDateTimeSimpleTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.johnzon.jsonb.symmetry.builtin; + +import jakarta.json.bind.Jsonb; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import static org.junit.Assert.assertEquals; + +/** + * JSON-B 3.0 Section 3.5 — ZonedDateTime uses ISO_ZONED_DATE_TIME format, + * which includes the zone ID in brackets: {@code 2024-01-15T10:30:45+01:00[Europe/Paris]}. + * + * @see ee.jakarta.tck.json.bind.defaultmapping.dates.DatesMappingTest#testZonedDateTimeMapping() + */ +public class ZonedDateTimeSimpleTest extends BuiltInSymmetryTest { + + private static final ZonedDateTime VALUE = + ZonedDateTime.of(2024, 1, 15, 10, 30, 45, 0, ZoneId.of("Europe/Paris")); + + private static final String FIELD_JSON = + "{\"value\":\"2024-01-15T10:30:45+01:00[Europe/Paris]\"}"; + + public static class Widget { + + private ZonedDateTime value; + + public ZonedDateTime getValue() { + return value; + } + + public void setValue(final ZonedDateTime value) { + this.value = value; + } + } + + @Override + public void assertWrite(final Jsonb jsonb) { + final Widget widget = new Widget(); + widget.setValue(VALUE); + assertEquals(FIELD_JSON, jsonb.toJson(widget)); + } + + @Override + public void assertRead(final Jsonb jsonb) { + final Widget widget = jsonb.fromJson(FIELD_JSON, Widget.class); + assertEquals(VALUE, widget.getValue()); + } +} diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java index f0ce5d76..5969ea01 100644 --- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java +++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java @@ -114,6 +114,35 @@ public T readObject(final JsonValue jsonValue, final Type targetType, final if (JsonStructure.class == targetType || JsonObject.class == targetType || JsonValue.class == targetType) { return (T) jsonValue; } + + // Resolve class-level adapter (e.g. @JsonbTypeAdapter) before type-specific handling. + // The adapter converts from a JSON-friendly type (String, String[], Map, etc.) to the target POJO. + // We first parse the JSON into the adapter's expected type, then apply the adapter. + // Precedence: Config adapter > class annotation adapter (matching write behavior). + if (jsonValue != null && Class.class.isInstance(targetType) && !Mappings.isPrimitive(targetType)) { + final Mappings.ClassMapping existing = mappings.getClassMapping(targetType); + Adapter classAdapter = existing != null && existing.adapter != null + ? existing.adapter : null; + if (classAdapter == null) { + classAdapter = config.findAdapter(targetType); + } + if (classAdapter == null) { + classAdapter = config.getAccessMode().findAdapter(Class.class.cast(targetType)); + } + if (classAdapter != null && TypeAwareAdapter.class.isInstance(classAdapter)) { + final Type adapterToType = TypeAwareAdapter.class.cast(classAdapter).getTo(); + if (!targetType.equals(adapterToType)) { + final Object intermediate; + if (adapterToType instanceof Class && JsonValue.class.isAssignableFrom((Class) adapterToType)) { + intermediate = jsonValue; + } else { + intermediate = readObject(jsonValue, adapterToType, false, skippedConverters); + } + return (T) ((Adapter) classAdapter).to(intermediate); + } + } + } + if (JsonObject.class.isInstance(jsonValue)) { return (T) buildObject( targetType, JsonObject.class.cast(jsonValue), applyObjectConverter,