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 2016/03/17 14:34:14 UTC
incubator-johnzon git commit: JOHNZON-68 transient and visibility
rules are not 100% the same,
noe needs a AND between field and method and the other a OR
Repository: incubator-johnzon
Updated Branches:
refs/heads/master 939171809 -> b301f2a1e
JOHNZON-68 transient and visibility rules are not 100% the same, noe needs a AND between field and method and the other a OR
Project: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/commit/b301f2a1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/tree/b301f2a1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/diff/b301f2a1
Branch: refs/heads/master
Commit: b301f2a1e60db70a74802a0a5526a82144773401
Parents: 9391718
Author: Romain manni-Bucau <rm...@gmail.com>
Authored: Thu Mar 17 14:33:56 2016 +0100
Committer: Romain manni-Bucau <rm...@gmail.com>
Committed: Thu Mar 17 14:33:56 2016 +0100
----------------------------------------------------------------------
.../apache/johnzon/jsonb/JsonbAccessMode.java | 160 ++++++++++---------
.../johnzon/jsonb/JsonbTransientTest.java | 65 ++++++++
2 files changed, 149 insertions(+), 76 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/b301f2a1/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java
----------------------------------------------------------------------
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 083be42..4f8351b 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
@@ -178,75 +178,75 @@ public class JsonbAccessMode implements AccessMode, Closeable {
}
return constructor == null && factory == null ? delegate.findFactory(clazz) : (
- constructor != null ?
- new Factory() {
- @Override
- public Object create(final Object[] params) {
- try {
- return finalConstructor.newInstance(params);
- } catch (final InstantiationException | IllegalAccessException e) {
- throw new IllegalStateException(e);
- } catch (final InvocationTargetException e) {
- throw new IllegalStateException(e.getCause());
- }
- }
+ constructor != null ?
+ new Factory() {
+ @Override
+ public Object create(final Object[] params) {
+ try {
+ return finalConstructor.newInstance(params);
+ } catch (final InstantiationException | IllegalAccessException e) {
+ throw new IllegalStateException(e);
+ } catch (final InvocationTargetException e) {
+ throw new IllegalStateException(e.getCause());
+ }
+ }
- @Override
- public Type[] getParameterTypes() {
- return types;
- }
+ @Override
+ public Type[] getParameterTypes() {
+ return types;
+ }
- @Override
- public String[] getParameterNames() {
- return params;
- }
+ @Override
+ public String[] getParameterNames() {
+ return params;
+ }
- @Override
- public Adapter<?, ?>[] getParameterConverter() {
- return converters;
- }
+ @Override
+ public Adapter<?, ?>[] getParameterConverter() {
+ return converters;
+ }
- @Override
- public Adapter<?, ?>[] getParameterItemConverter() {
- return itemConverters;
- }
- } :
- new Factory() {
- @Override
- public Object create(final Object[] params) {
- try {
- final Object invoke = finalFactory.invoke(null, params);
- if (!clazz.isInstance(invoke)) {
- throw new IllegalArgumentException(invoke + " is not a " + clazz.getName());
+ @Override
+ public Adapter<?, ?>[] getParameterItemConverter() {
+ return itemConverters;
+ }
+ } :
+ new Factory() {
+ @Override
+ public Object create(final Object[] params) {
+ try {
+ final Object invoke = finalFactory.invoke(null, params);
+ if (!clazz.isInstance(invoke)) {
+ throw new IllegalArgumentException(invoke + " is not a " + clazz.getName());
+ }
+ return invoke;
+ } catch (final IllegalAccessException e) {
+ throw new IllegalStateException(e);
+ } catch (final InvocationTargetException e) {
+ throw new IllegalStateException(e.getCause());
+ }
}
- return invoke;
- } catch (final IllegalAccessException e) {
- throw new IllegalStateException(e);
- } catch (final InvocationTargetException e) {
- throw new IllegalStateException(e.getCause());
- }
- }
- @Override
- public Type[] getParameterTypes() {
- return types;
- }
+ @Override
+ public Type[] getParameterTypes() {
+ return types;
+ }
- @Override
- public String[] getParameterNames() {
- return params;
- }
+ @Override
+ public String[] getParameterNames() {
+ return params;
+ }
- @Override
- public Adapter<?, ?>[] getParameterConverter() {
- return converters;
- }
+ @Override
+ public Adapter<?, ?>[] getParameterConverter() {
+ return converters;
+ }
- @Override
- public Adapter<?, ?>[] getParameterItemConverter() {
- return itemConverters;
- }
- });
+ @Override
+ public Adapter<?, ?>[] getParameterItemConverter() {
+ return itemConverters;
+ }
+ });
}
private void validateAnnotations(final Object parameter,
@@ -267,8 +267,8 @@ public class JsonbAccessMode implements AccessMode, Closeable {
if (adapter != null) {
final Class<? extends JsonbAdapter> value = adapter.value();
final ParameterizedType pt = ParameterizedType.class.cast(
- Stream.of(value.getGenericInterfaces())
- .filter(i -> ParameterizedType.class.isInstance(i) && ParameterizedType.class.cast(i).getRawType() == JsonbAdapter.class).findFirst().orElse(null));
+ Stream.of(value.getGenericInterfaces())
+ .filter(i -> ParameterizedType.class.isInstance(i) && ParameterizedType.class.cast(i).getRawType() == JsonbAdapter.class).findFirst().orElse(null));
if (pt == null) {
throw new IllegalArgumentException(value + " doesn't implement JsonbAdapter");
}
@@ -337,8 +337,8 @@ public class JsonbAccessMode implements AccessMode, Closeable {
final Adapter<?, ?> converter;
try {
converter = adapter == null && dateFormat == null && numberFormat == null ?
- defaultConverters.get(new AdapterKey(initialReader.getType(), String.class)) :
- toConverter(initialReader.getType(), adapter, dateFormat, numberFormat);
+ defaultConverters.get(new AdapterKey(initialReader.getType(), String.class)) :
+ toConverter(initialReader.getType(), adapter, dateFormat, numberFormat);
} catch (final InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
@@ -438,8 +438,8 @@ public class JsonbAccessMode implements AccessMode, Closeable {
final Adapter<?, ?> converter;
try {
converter = adapter == null && dateFormat == null && numberFormat == null ?
- defaultConverters.get(new AdapterKey(initialWriter.getType(), String.class)) :
- toConverter(initialWriter.getType(), adapter, dateFormat, numberFormat);
+ defaultConverters.get(new AdapterKey(initialWriter.getType(), String.class)) :
+ toConverter(initialWriter.getType(), adapter, dateFormat, numberFormat);
} catch (final InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
@@ -507,19 +507,27 @@ public class JsonbAccessMode implements AccessMode, Closeable {
}
private boolean isTransient(final DecoratedType dt, final PropertyVisibilityStrategy visibility) {
- return shouldSkip(visibility, dt) &&
- (!FieldAndMethodAccessMode.CompositeDecoratedType.class.isInstance(dt) ||
- !Stream.of(FieldAndMethodAccessMode.CompositeDecoratedType.class.cast(dt).getType1(), FieldAndMethodAccessMode.CompositeDecoratedType.class.cast(dt).getType2())
- .map(t -> shouldSkip(visibility, t))
- .filter(a -> a)
- .findAny()
- .isPresent());
+ if (!FieldAndMethodAccessMode.CompositeDecoratedType.class.isInstance(dt)) {
+ return isTransient(dt) || shouldSkip(visibility, dt);
+ }
+ final FieldAndMethodAccessMode.CompositeDecoratedType cdt = FieldAndMethodAccessMode.CompositeDecoratedType.class.cast(dt);
+ return isTransient(cdt.getType1()) || isTransient(cdt.getType2()) ||
+ (shouldSkip(visibility, cdt.getType1()) && shouldSkip(visibility, cdt.getType2()));
}
private boolean shouldSkip(final PropertyVisibilityStrategy visibility, final DecoratedType t) {
- return t.getAnnotation(JsonbTransient.class) != null ||
- (FieldAccessMode.FieldDecoratedType.class.isInstance(t) && !visibility.isVisible(FieldAccessMode.FieldDecoratedType.class.cast(t).getField())) ||
- (MethodAccessMode.MethodDecoratedType.class.isInstance(t) && !visibility.isVisible(MethodAccessMode.MethodDecoratedType.class.cast(t).getMethod()));
+ return isNotVisible(visibility, t);
+ }
+
+ private boolean isTransient(final DecoratedType t) {
+ return t.getAnnotation(JsonbTransient.class) != null;
+ }
+
+ private boolean isNotVisible(PropertyVisibilityStrategy visibility, DecoratedType t) {
+ return !(FieldAccessMode.FieldDecoratedType.class.isInstance(t) ?
+ visibility.isVisible(FieldAccessMode.FieldDecoratedType.class.cast(t).getField())
+ : (MethodAccessMode.MethodDecoratedType.class.isInstance(t) &&
+ visibility.isVisible(MethodAccessMode.MethodDecoratedType.class.cast(t).getMethod())));
}
private Comparator<String> orderComparator(final Class<?> clazz) {
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/b301f2a1/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbTransientTest.java
----------------------------------------------------------------------
diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbTransientTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbTransientTest.java
new file mode 100644
index 0000000..29edfdd
--- /dev/null
+++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbTransientTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.Jsonb;
+import javax.json.bind.annotation.JsonbProperty;
+import javax.json.bind.annotation.JsonbTransient;
+import javax.json.bind.spi.JsonbProvider;
+
+import static org.junit.Assert.assertEquals;
+
+public class JsonbTransientTest {
+ @Test
+ public void roundtrip() {
+ final Jsonb jsonb = JsonbProvider.provider().create().build();
+ final Book book = jsonb.fromJson("{\"_name\": \"test\", \"id\":123}", Book.class);
+ assertEquals("test", book.getName());
+ assertEquals(0, book.getId());
+
+ book.id = 123;
+ assertEquals("{\"_name\":\"test\"}", jsonb.toJson(book));
+ }
+
+ public static class Book {
+ @JsonbTransient
+ private long id;
+
+ @JsonbProperty("_name")
+ private String name;
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+ }
+}