You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by rm...@apache.org on 2019/08/07 16:42:32 UTC
[johnzon] 02/02: JOHNZON-227 ensure converter output is mapped
properly even when changing original type - this can need another commit to
introspect converter types and fallback on dynamic mode instead of
activating it upfront (for perfs)
This is an automated email from the ASF dual-hosted git repository.
rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
commit 54c30b376874751f38d6d08a352f39ace60d1f81
Author: Romain Manni-Bucau <rm...@apache.org>
AuthorDate: Wed Aug 7 18:42:20 2019 +0200
JOHNZON-227 ensure converter output is mapped properly even when changing original type - this can need another commit to introspect converter types and fallback on dynamic mode instead of activating it upfront (for perfs)
---
.../johnzon/mapper/MappingGeneratorImpl.java | 10 +--
.../java/org/apache/johnzon/mapper/Mappings.java | 20 ++++--
.../johnzon/mapper/converter/StringerTest.java | 75 ++++++++++++++++++++++
3 files changed, 94 insertions(+), 11 deletions(-)
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
index 04bd2d8..9febaa8 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
@@ -127,7 +127,7 @@ public class MappingGeneratorImpl implements MappingGenerator {
return;
}
- if(writePrimitives(object)) {
+ if (writePrimitives(object)) {
return;
}
@@ -383,9 +383,9 @@ public class MappingGeneratorImpl implements MappingGenerator {
if (config.getSerializeValueFilter().shouldIgnore(key, value)) {
return;
}
- if (array || (dynamic && type.isArray())) {
+ if ((!dynamic && array) || (dynamic && type.isArray())) {
writeArray(type, itemConverter, key, value, ignoredProperties, jsonPointer);
- } else if (collection || (dynamic && Collection.class.isAssignableFrom(type))) {
+ } else if ((!dynamic && collection) || (dynamic && Collection.class.isAssignableFrom(type))) {
generator.writeStartArray(key);
int i = 0;
for (final Object o : Collection.class.cast(value)) {
@@ -412,11 +412,11 @@ public class MappingGeneratorImpl implements MappingGenerator {
i++;
}
generator.writeEnd();
- } else if (map || (dynamic && Map.class.isAssignableFrom(type))) {
+ } else if ((!dynamic && map) || (dynamic && Map.class.isAssignableFrom(type))) {
generator.writeStartObject(key);
writeMapBody((Map<?, ?>) value, itemConverter);
generator.writeEnd();
- } else if (primitive || (dynamic && Mappings.isPrimitive(type))) {
+ } else if ((!dynamic && primitive) || (dynamic && Mappings.isPrimitive(type))) {
if (objectConverter != null) {
final DynamicMappingGenerator dynamicMappingGenerator = new DynamicMappingGenerator(this,
() -> this.generator.writeStartObject(key), this.generator::writeEnd, key);
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mappings.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mappings.java
index 061f1a5..16b42fe 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mappings.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mappings.java
@@ -139,11 +139,6 @@ public class Mappings {
final int version, final String[] ignoreNested) {
this.reader = reader;
this.version = version;
- this.dynamic = dynamic;
- this.array = array;
- this.collection = collection;
- this.primitive = primitive;
- this.ignoreNested = ignoreNested == null || ignoreNested.length == 0 ? null : new HashSet<>(asList(ignoreNested));
Adapter theConverter = null;
Adapter theItemConverter = null;
@@ -173,8 +168,21 @@ public class Mappings {
this.converter = theConverter;
this.itemConverter = theItemConverter;
this.objectConverter = theObjectConverter;
+ this.ignoreNested = ignoreNested == null || ignoreNested.length == 0 ? null : new HashSet<>(asList(ignoreNested));
- this.map = map && this.converter == null;
+ if (converter == null) {
+ this.dynamic = dynamic;
+ this.array = array;
+ this.collection = collection;
+ this.primitive = primitive;
+ this.map = map;
+ } else { // todo: likely find from/to types from the adapter and adjust these meta accordingly
+ this.dynamic = true;
+ this.array = array;
+ this.collection = collection;
+ this.primitive = primitive;
+ this.map = false;
+ }
}
@Override
diff --git a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/converter/StringerTest.java b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/converter/StringerTest.java
new file mode 100644
index 0000000..a727cba
--- /dev/null
+++ b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/converter/StringerTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.mapper.converter;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import java.nio.charset.StandardCharsets;
+
+import org.apache.johnzon.mapper.Adapter;
+import org.apache.johnzon.mapper.JohnzonConverter;
+import org.apache.johnzon.mapper.MapperBuilder;
+import org.junit.Test;
+
+public class StringerTest {
+ private static final String STRING = "hello johnzon";
+ private static final byte[] BYTES = STRING.getBytes(StandardCharsets.UTF_8);
+
+ @Test
+ public void testSerialize() {
+ final Holder datatype = new Holder(BYTES);
+ final String json = new MapperBuilder().build().writeObjectAsString(datatype);
+ assertEquals("{\"data\":\"" + STRING + "\"}", json);
+ }
+
+ @Test
+ public void testDeserialize() {
+ final String json = "{\"data\":\"" + STRING + "\"}";
+ final Holder datatype = new MapperBuilder().build().readObject(json, Holder.class);
+ assertArrayEquals(BYTES, datatype.data);
+ }
+
+ public static class Stringer implements Adapter<byte[], String> {
+ @Override
+ public byte[] to(final String b) {
+ return b.getBytes(StandardCharsets.UTF_8);
+ }
+
+ @Override
+ public String from(final byte[] a) {
+ return new String(a, StandardCharsets.UTF_8);
+ }
+ }
+
+
+ public static class Holder {
+ @JohnzonConverter(Stringer.class)
+ public byte[] data;
+
+ public Holder() {
+ // no-op
+ }
+
+ public Holder(byte[] data) {
+ this.data = data;
+ }
+ }
+
+}