Repository: johnzon Updated Branches: refs/heads/master 2f0cb1b54 -> 80b937db8
JOHNZON-132 handle custom ParameterizedType in buildObject Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/80b937db Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/80b937db Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/80b937db Branch: refs/heads/master Commit: 80b937db850e185b6e3d572607f6e1b96d868a1c Parents: 2f0cb1b Author: rmannibucau <[email protected]> Authored: Sat Jul 29 09:53:12 2017 +0200 Committer: rmannibucau <[email protected]> Committed: Sat Jul 29 09:53:12 2017 +0200 ---------------------------------------------------------------------- .../jsonb/CustomParameterizedTypeTest.java | 104 +++++++++++++++++++ .../johnzon/mapper/MappingParserImpl.java | 15 +-- 2 files changed, 106 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/johnzon/blob/80b937db/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/CustomParameterizedTypeTest.java ---------------------------------------------------------------------- diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/CustomParameterizedTypeTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/CustomParameterizedTypeTest.java new file mode 100644 index 0000000..7b5e2cc --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/CustomParameterizedTypeTest.java @@ -0,0 +1,104 @@ +/* + * 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; + +import org.junit.Test; + +import javax.json.bind.spi.JsonbProvider; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Map; +import java.util.Objects; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +public class CustomParameterizedTypeTest { + @Test + public void run() { + final String value = "{\"val1\":{\"name\":\"the name\",\"age\":1234}}"; + final Object map = JsonbProvider.provider().create().build().fromJson(value, new ParameterizedType() { + @Override + public Type[] getActualTypeArguments() { + return new Type[]{String.class, Value.class}; + } + + @Override + public Type getRawType() { + return Map.class; + } + + @Override + public Type getOwnerType() { + return null; + } + }); + assertThat(map, instanceOf(Map.class)); + final Map<String, Value> asMap = Map.class.cast(map); + assertTrue(asMap.containsKey("val1")); + assertEquals(1, asMap.size()); + assertEquals(1234, asMap.get("val1").age); + assertEquals("the name", asMap.get("val1").name); + } + + public static class Value { + private String name; + private int age; + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return this.age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public int hashCode() { + return 0; + } + + @Override + public boolean equals(Object obj) { + if (obj == null || obj.getClass() != getClass()) { + return false; + } + Value other = (Value) obj; + return Objects.equals(this.name, other.name) + && Objects.equals(this.age, other.age); + } + + @Override + public String toString() { + return this.name + " " + this.age; + } + + } + +} http://git-wip-us.apache.org/repos/asf/johnzon/blob/80b937db/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java ---------------------------------------------------------------------- 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 a97427f..ba28efb 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 @@ -80,17 +80,6 @@ public class MappingParserImpl implements MappingParser { private static final Adapter<Object, String> FALLBACK_CONVERTER = new ConverterAdapter<Object>(new FallbackConverter()); private static final JohnzonParameterizedType ANY_LIST = new JohnzonParameterizedType(List.class, Object.class); private static final CharacterConverter CHARACTER_CONVERTER = new CharacterConverter(); // this one is particular, share the logic - private static final boolean HAS_READ_VALUE; - static { - boolean hasReadValue; // v1.0 vs v1.1 - try { - JsonReader.class.getDeclaredMethod("readValue"); - hasReadValue = true; - } catch (final Error | NoSuchMethodException e) { - hasReadValue = false; - } - HAS_READ_VALUE = hasReadValue; - } protected final ConcurrentMap<Adapter<?, ?>, AdapterKey> reverseAdaptersRegistry; protected final ConcurrentMap<Class<?>, Method> valueOfs = new ConcurrentHashMap<Class<?>, Method>(); @@ -114,7 +103,7 @@ public class MappingParserImpl implements MappingParser { @Override public <T> T readObject(Type targetType) { try { - return readObject(HAS_READ_VALUE ? jsonReader.readValue() : jsonReader.read(), targetType); + return readObject(jsonReader.readValue(), targetType); } finally { if (config.isClose()) { jsonReader.close(); @@ -197,7 +186,7 @@ public class MappingParserImpl implements MappingParser { type = new JohnzonParameterizedType(Map.class, String.class, Object.class); } - if (applyObjectConverter && !(type instanceof JohnzonParameterizedType)) { + if (applyObjectConverter && !(type instanceof ParameterizedType)) { if (!(type instanceof Class)) { throw new MapperException("ObjectConverters are only supported for Classes not Types");
