You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by st...@apache.org on 2016/12/05 21:39:43 UTC
[1/2] johnzon git commit: JOHNZON-99 JOHNZON-100 improve JSON-B
private attribute support
Repository: johnzon
Updated Branches:
refs/heads/master 389dfe1fc -> ed2a42ef1
JOHNZON-99 JOHNZON-100 improve JSON-B private attribute support
* evaluate the JsonVisibility
* honor @JsonProperty fields
Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/a89915df
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/a89915df
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/a89915df
Branch: refs/heads/master
Commit: a89915df65a31523413c93cc946a79b38ae3db55
Parents: 389dfe1
Author: Mark Struberg <st...@apache.org>
Authored: Mon Dec 5 21:32:36 2016 +0100
Committer: Mark Struberg <st...@apache.org>
Committed: Mon Dec 5 22:26:18 2016 +0100
----------------------------------------------------------------------
.../apache/johnzon/jsonb/JohnzonBuilder.java | 14 ++-
.../johnzon/jsonb/JsonbVisitilityTest.java | 106 +++++++++++++++++++
2 files changed, 119 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/johnzon/blob/a89915df/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonBuilder.java
----------------------------------------------------------------------
diff --git a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonBuilder.java b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonBuilder.java
index 673877a..137cf4c 100644
--- a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonBuilder.java
+++ b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonBuilder.java
@@ -38,6 +38,7 @@ import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import javax.json.bind.adapter.JsonbAdapter;
+import javax.json.bind.annotation.JsonbProperty;
import javax.json.bind.annotation.JsonbVisibility;
import javax.json.bind.config.BinaryDataStrategy;
import javax.json.bind.config.PropertyNamingStrategy;
@@ -156,6 +157,9 @@ public class JohnzonBuilder implements JsonbBuilder {
@Override
public boolean isVisible(final Field field) {
+ if (field.getAnnotation(JsonbProperty.class) != null) {
+ return true;
+ }
final PropertyVisibilityStrategy strategy = strategies.computeIfAbsent(field.getDeclaringClass(), this::visibilityStrategy);
return strategy == this ? Modifier.isPublic(field.getModifiers()) : strategy.isVisible(field);
}
@@ -167,9 +171,17 @@ public class JohnzonBuilder implements JsonbBuilder {
}
private PropertyVisibilityStrategy visibilityStrategy(final Class<?> type) { // can be cached
+ JsonbVisibility visibility = type.getAnnotation(JsonbVisibility.class);
+ if (visibility != null) {
+ try {
+ return visibility.value().newInstance();
+ } catch (final InstantiationException | IllegalAccessException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
Package p = type.getPackage();
while (p != null) {
- final JsonbVisibility visibility = p.getAnnotation(JsonbVisibility.class);
+ visibility = p.getAnnotation(JsonbVisibility.class);
if (visibility != null) {
try {
return visibility.value().newInstance();
http://git-wip-us.apache.org/repos/asf/johnzon/blob/a89915df/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbVisitilityTest.java
----------------------------------------------------------------------
diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbVisitilityTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbVisitilityTest.java
new file mode 100644
index 0000000..9fe45aa
--- /dev/null
+++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbVisitilityTest.java
@@ -0,0 +1,106 @@
+/*
+ * 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 java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.json.bind.Jsonb;
+import javax.json.bind.annotation.JsonbProperty;
+import javax.json.bind.annotation.JsonbVisibility;
+import javax.json.bind.config.PropertyVisibilityStrategy;
+import javax.json.bind.spi.JsonbProvider;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class JsonbVisitilityTest {
+
+ @Test
+ public void testJsonVisibilityAllFields() {
+ MyDataVisibility data = new MyDataVisibility();
+ data.put("x", "a");
+ data.put("y", "b");
+
+ Jsonb jsonb = JsonbProvider.provider().create().build();
+ String json = jsonb.toJson(data);
+ Assert.assertEquals("{\"attribs\":{\"x\":\"a\",\"y\":\"b\"}}", json);
+
+ MyDataVisibility dataBack = jsonb.fromJson(json, MyDataVisibility.class);
+ Assert.assertEquals("a", dataBack.get("x"));
+ Assert.assertEquals("b", dataBack.get("y"));
+ }
+
+ @Test
+ public void testJsonPropertyInternalField() {
+ MyDataJsonField data = new MyDataJsonField();
+ data.put("x", "a");
+ data.put("y", "b");
+
+ Jsonb jsonb = JsonbProvider.provider().create().build();
+ String json = jsonb.toJson(data);
+ Assert.assertEquals("{\"attribs\":{\"x\":\"a\",\"y\":\"b\"}}", json);
+
+ MyDataJsonField dataBack = jsonb.fromJson(json, MyDataJsonField.class);
+ Assert.assertEquals("a", dataBack.get("x"));
+ Assert.assertEquals("b", dataBack.get("y"));
+ }
+
+
+ @JsonbVisibility(VisibleAllFields.class)
+ public static class MyDataVisibility {
+ private Map<String, String> attribs = new HashMap<>();
+
+ public void put(String key, String value) {
+ attribs.put(key, value);
+ }
+
+ public String get(String key) {
+ return attribs.get(key);
+ }
+ }
+
+ public static class MyDataJsonField {
+ @JsonbProperty
+ private Map<String, String> attribs = new HashMap<>();
+
+ public void put(String key, String value) {
+ attribs.put(key, value);
+ }
+
+ public String get(String key) {
+ return attribs.get(key);
+ }
+ }
+
+ /**
+ * All fields are visible. Even private, which by default won't get jsonified.
+ */
+ public static class VisibleAllFields implements PropertyVisibilityStrategy {
+ @Override
+ public boolean isVisible(Field field) {
+ return true;
+ }
+
+ @Override
+ public boolean isVisible(Method method) {
+ return false;
+ }
+ }
+}
[2/2] johnzon git commit: progress to version 1.0.0-SNAPSHOT finally.
Posted by st...@apache.org.
progress to version 1.0.0-SNAPSHOT finally.
Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/ed2a42ef
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/ed2a42ef
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/ed2a42ef
Branch: refs/heads/master
Commit: ed2a42ef101e036071d7b542e97bc5a73980b299
Parents: a89915d
Author: Mark Struberg <st...@apache.org>
Authored: Mon Dec 5 22:39:03 2016 +0100
Committer: Mark Struberg <st...@apache.org>
Committed: Mon Dec 5 22:39:03 2016 +0100
----------------------------------------------------------------------
johnzon-core/pom.xml | 2 +-
johnzon-distribution/pom.xml | 2 +-
johnzon-jaxrs/pom.xml | 2 +-
johnzon-jsonb/pom.xml | 4 ++--
johnzon-mapper/pom.xml | 2 +-
johnzon-maven-plugin/pom.xml | 2 +-
johnzon-websocket/pom.xml | 2 +-
jsonb-api/pom.xml | 4 ++--
pom.xml | 2 +-
9 files changed, 11 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/johnzon/blob/ed2a42ef/johnzon-core/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-core/pom.xml b/johnzon-core/pom.xml
index 60de5ce..02398f7 100644
--- a/johnzon-core/pom.xml
+++ b/johnzon-core/pom.xml
@@ -21,7 +21,7 @@
<parent>
<artifactId>johnzon</artifactId>
<groupId>org.apache.johnzon</groupId>
- <version>0.9.6-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
http://git-wip-us.apache.org/repos/asf/johnzon/blob/ed2a42ef/johnzon-distribution/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-distribution/pom.xml b/johnzon-distribution/pom.xml
index 2333fa5..632ea89 100644
--- a/johnzon-distribution/pom.xml
+++ b/johnzon-distribution/pom.xml
@@ -21,7 +21,7 @@
<parent>
<artifactId>johnzon</artifactId>
<groupId>org.apache.johnzon</groupId>
- <version>0.9.6-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
http://git-wip-us.apache.org/repos/asf/johnzon/blob/ed2a42ef/johnzon-jaxrs/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-jaxrs/pom.xml b/johnzon-jaxrs/pom.xml
index 752ead4..f47efb6 100644
--- a/johnzon-jaxrs/pom.xml
+++ b/johnzon-jaxrs/pom.xml
@@ -21,7 +21,7 @@
<parent>
<artifactId>johnzon</artifactId>
<groupId>org.apache.johnzon</groupId>
- <version>0.9.6-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
http://git-wip-us.apache.org/repos/asf/johnzon/blob/ed2a42ef/johnzon-jsonb/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-jsonb/pom.xml b/johnzon-jsonb/pom.xml
index 74dbfdb..d66c4a4 100644
--- a/johnzon-jsonb/pom.xml
+++ b/johnzon-jsonb/pom.xml
@@ -21,7 +21,7 @@
<parent>
<artifactId>johnzon</artifactId>
<groupId>org.apache.johnzon</groupId>
- <version>0.9.6-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -50,7 +50,7 @@
<dependency>
<groupId>org.apache.johnzon</groupId>
<artifactId>jsonb-api</artifactId>
- <version>0.9.6-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
http://git-wip-us.apache.org/repos/asf/johnzon/blob/ed2a42ef/johnzon-mapper/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-mapper/pom.xml b/johnzon-mapper/pom.xml
index 1c74e39..718ced5 100644
--- a/johnzon-mapper/pom.xml
+++ b/johnzon-mapper/pom.xml
@@ -21,7 +21,7 @@
<parent>
<artifactId>johnzon</artifactId>
<groupId>org.apache.johnzon</groupId>
- <version>0.9.6-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
http://git-wip-us.apache.org/repos/asf/johnzon/blob/ed2a42ef/johnzon-maven-plugin/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-maven-plugin/pom.xml b/johnzon-maven-plugin/pom.xml
index 60e474d..100fcf1 100644
--- a/johnzon-maven-plugin/pom.xml
+++ b/johnzon-maven-plugin/pom.xml
@@ -23,7 +23,7 @@
<parent>
<artifactId>johnzon</artifactId>
<groupId>org.apache.johnzon</groupId>
- <version>0.9.6-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>johnzon-maven-plugin</artifactId>
http://git-wip-us.apache.org/repos/asf/johnzon/blob/ed2a42ef/johnzon-websocket/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-websocket/pom.xml b/johnzon-websocket/pom.xml
index 8d5b4dd..2b1bf8b 100644
--- a/johnzon-websocket/pom.xml
+++ b/johnzon-websocket/pom.xml
@@ -21,7 +21,7 @@
<parent>
<artifactId>johnzon</artifactId>
<groupId>org.apache.johnzon</groupId>
- <version>0.9.6-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
http://git-wip-us.apache.org/repos/asf/johnzon/blob/ed2a42ef/jsonb-api/pom.xml
----------------------------------------------------------------------
diff --git a/jsonb-api/pom.xml b/jsonb-api/pom.xml
index 27244b2..f8ad7a1 100644
--- a/jsonb-api/pom.xml
+++ b/jsonb-api/pom.xml
@@ -21,12 +21,12 @@
<parent>
<artifactId>johnzon</artifactId>
<groupId>org.apache.johnzon</groupId>
- <version>0.9.6-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jsonb-api</artifactId>
- <version>0.9.6-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
<name>Johnzon :: JSON-B API</name>
<properties>
http://git-wip-us.apache.org/repos/asf/johnzon/blob/ed2a42ef/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index e507cc7..4772d79 100644
--- a/pom.xml
+++ b/pom.xml
@@ -33,7 +33,7 @@
<groupId>org.apache.johnzon</groupId>
<artifactId>johnzon</artifactId>
<packaging>pom</packaging>
- <version>0.9.6-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
<name>Apache Johnzon</name>
<description>Apache Johnzon is an implementation of JSR-353 (JavaTM API for JSON Processing).</description>
<inceptionYear>2014</inceptionYear>