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:24:31 UTC

[1/3] johnzon git commit: JOHNZON-99 JOHNZON-100 improve JSON-B private attribute support

Repository: johnzon
Updated Branches:
  refs/heads/JSONP-1.1 0e4bd4888 -> 6aea7acc3


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/271a98e5
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/271a98e5
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/271a98e5

Branch: refs/heads/JSONP-1.1
Commit: 271a98e5eea169c753bae877058491759e43c829
Parents: eb932c3
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 21:35:12 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/271a98e5/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/271a98e5/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;
+        }
+    }
+}


[3/3] johnzon git commit: propagate JSON-P 1.1 work to version 1.1.0

Posted by st...@apache.org.
propagate JSON-P 1.1 work to version 1.1.0


Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/6aea7acc
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/6aea7acc
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/6aea7acc

Branch: refs/heads/JSONP-1.1
Commit: 6aea7acc3fd912a9d59439ad66c3cb2853fd591a
Parents: 271a98e
Author: Mark Struberg <st...@apache.org>
Authored: Mon Dec 5 22:23:55 2016 +0100
Committer: Mark Struberg <st...@apache.org>
Committed: Mon Dec 5 22:23:55 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/6aea7acc/johnzon-core/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-core/pom.xml b/johnzon-core/pom.xml
index 60de5ce..081840a 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.1.0-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 

http://git-wip-us.apache.org/repos/asf/johnzon/blob/6aea7acc/johnzon-distribution/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-distribution/pom.xml b/johnzon-distribution/pom.xml
index 420f8c9..0ee6b85 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.1.0-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 

http://git-wip-us.apache.org/repos/asf/johnzon/blob/6aea7acc/johnzon-jaxrs/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-jaxrs/pom.xml b/johnzon-jaxrs/pom.xml
index 752ead4..5a9dfa1 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.1.0-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 

http://git-wip-us.apache.org/repos/asf/johnzon/blob/6aea7acc/johnzon-jsonb/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-jsonb/pom.xml b/johnzon-jsonb/pom.xml
index 74dbfdb..1c225a9 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.1.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.1.0-SNAPSHOT</version>
       <scope>provided</scope>
     </dependency>
     <dependency>

http://git-wip-us.apache.org/repos/asf/johnzon/blob/6aea7acc/johnzon-mapper/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-mapper/pom.xml b/johnzon-mapper/pom.xml
index 1c74e39..b020fc3 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.1.0-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 

http://git-wip-us.apache.org/repos/asf/johnzon/blob/6aea7acc/johnzon-maven-plugin/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-maven-plugin/pom.xml b/johnzon-maven-plugin/pom.xml
index 4e7f743..60bd77b 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.1.0-SNAPSHOT</version>
   </parent>
 
   <artifactId>johnzon-maven-plugin</artifactId>

http://git-wip-us.apache.org/repos/asf/johnzon/blob/6aea7acc/johnzon-websocket/pom.xml
----------------------------------------------------------------------
diff --git a/johnzon-websocket/pom.xml b/johnzon-websocket/pom.xml
index 3420410..67a3917 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.1.0-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 

http://git-wip-us.apache.org/repos/asf/johnzon/blob/6aea7acc/jsonb-api/pom.xml
----------------------------------------------------------------------
diff --git a/jsonb-api/pom.xml b/jsonb-api/pom.xml
index 27244b2..6dcf095 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.1.0-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 
   <artifactId>jsonb-api</artifactId>
-  <version>0.9.6-SNAPSHOT</version>
+  <version>1.1.0-SNAPSHOT</version>
   <name>Johnzon :: JSON-B API</name>
 
   <properties>

http://git-wip-us.apache.org/repos/asf/johnzon/blob/6aea7acc/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 964d31a..b9fa74a 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.1.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>


[2/3] johnzon git commit: JOHNZON-95 first bits of MergeBatch handling

Posted by st...@apache.org.
JOHNZON-95 first bits of MergeBatch handling


Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/eb932c35
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/eb932c35
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/eb932c35

Branch: refs/heads/JSONP-1.1
Commit: eb932c35e91c0a6762716570f1b452796a837d06
Parents: 0e4bd48
Author: Mark Struberg <st...@apache.org>
Authored: Wed Nov 23 22:51:37 2016 +0100
Committer: Mark Struberg <st...@apache.org>
Committed: Mon Dec 5 21:35:12 2016 +0100

----------------------------------------------------------------------
 .../johnzon/core/JsonMergePatchBuilder.java     | 74 ++++++++++++++++++++
 .../johnzon/core/JsonMergeBatchBuilderTest.java | 36 ++++++++++
 2 files changed, 110 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/johnzon/blob/eb932c35/johnzon-core/src/main/java/org/apache/johnzon/core/JsonMergePatchBuilder.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonMergePatchBuilder.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonMergePatchBuilder.java
new file mode 100644
index 0000000..831e82e
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonMergePatchBuilder.java
@@ -0,0 +1,74 @@
+/*
+ * 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.core;
+
+import javax.json.JsonValue;
+
+/**
+ * Creates and applies a Json Merge Patch as defined in
+ * https://tools.ietf.org/html/rfc7396
+ */
+public class JsonMergePatchBuilder {
+
+    /**
+     * Create a merged patch by comparing the source to the target.
+     * Applying this JsonPatch to the source will give you the target.
+     * A mergePatch is a JsonValue as defined in http://tools.ietf.org/html/rfc7396
+     *
+     * If you have a JSON like
+     * <pre>
+     * {
+     *   "a": "b",
+     *   "c": {
+     *     "d": "e",
+     *     "f": "g"
+     *   }
+     * }
+     * </pre>
+     *
+     * Then you can change the value of "a" and removing "f" by sending:
+     * <pre>
+     * {
+     *   "a":"z",
+     *   "c": {
+     *     "f": null
+     *   }
+     * }
+     * </pre>
+     *
+     * @see #mergePatch(JsonValue, JsonValue)
+     *
+     * @since 1.1
+     */
+    public JsonValue createMergePatch(JsonValue source , JsonValue target) {
+        return null;
+    }
+
+    /**
+     * Merge the given patch to the existing source
+     * A mergePatch is a JsonValue as defined in http://tools.ietf.org/html/rfc7396
+     *
+     * @return the result of applying the patch to the source
+     *
+     * @see #createMergePatch(JsonValue, JsonValue)
+     * @since 1.1
+     */
+    public JsonValue mergePatch(JsonValue source, JsonValue patch) {
+        return null;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/johnzon/blob/eb932c35/johnzon-core/src/test/java/org/apache/johnzon/core/JsonMergeBatchBuilderTest.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonMergeBatchBuilderTest.java b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonMergeBatchBuilderTest.java
new file mode 100644
index 0000000..141ed9d
--- /dev/null
+++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonMergeBatchBuilderTest.java
@@ -0,0 +1,36 @@
+/*
+ * 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.core;
+
+import javax.json.spi.JsonProvider;
+
+public class JsonMergeBatchBuilderTest {
+
+    private static final String[][] TEST_CASES = new String[][]{
+            {
+                "{\"a\":\"b\"}",
+                "{\"a\":\"c\"}",
+                "{\"a\":\"c\"}"
+            }
+    };
+
+    public void testSimpleMergePatch() {
+        JsonProvider provider = JsonProvider.provider();
+
+
+    }
+}