Repository: johnzon
Updated Branches:
  refs/heads/master dc7306d0e -> 125cbf225


JOHNZON-115 add SerializeValueFilter


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

Branch: refs/heads/master
Commit: 125cbf22557db74ff1f74d59167c5fd88e80298d
Parents: dc7306d
Author: rmannibucau <[email protected]>
Authored: Wed Apr 19 11:13:10 2017 +0200
Committer: rmannibucau <[email protected]>
Committed: Wed Apr 19 11:13:10 2017 +0200

----------------------------------------------------------------------
 .../apache/johnzon/jsonb/JohnzonBuilder.java    |  2 +
 .../apache/johnzon/mapper/MapperBuilder.java    |  9 +++-
 .../org/apache/johnzon/mapper/MapperConfig.java |  9 +++-
 .../johnzon/mapper/MappingGeneratorImpl.java    |  3 ++
 .../johnzon/mapper/SerializeValueFilter.java    | 28 ++++++++++++
 .../apache/johnzon/mapper/MapperConfigTest.java |  2 +-
 .../mapper/SerializeValueFilterTest.java        | 46 ++++++++++++++++++++
 .../java/org/superbiz/ExtendMappingTest.java    |  2 +-
 8 files changed, 97 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/johnzon/blob/125cbf22/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 0b08a55..3ac1cdf 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
@@ -32,6 +32,7 @@ import org.apache.johnzon.mapper.Converter;
 import org.apache.johnzon.mapper.Mapper;
 import org.apache.johnzon.mapper.MapperBuilder;
 import org.apache.johnzon.mapper.ObjectConverter;
+import org.apache.johnzon.mapper.SerializeValueFilter;
 import org.apache.johnzon.mapper.internal.AdapterKey;
 import org.apache.johnzon.mapper.internal.ConverterAdapter;
 
@@ -288,6 +289,7 @@ public class JohnzonBuilder implements JsonbBuilder {
 
         builder.setReadAttributeBeforeWrite(
                 
config.getProperty("johnzon.readAttributeBeforeWrite").map(Boolean.class::cast).orElse(false));
+        config.getProperty("johnzon.serialize-value-filter").ifPresent(s -> 
builder.setSerializeValueFilter(SerializeValueFilter.class.cast(s)));
 
         
config.getProperty(JsonbConfig.SERIALIZERS).map(JsonbSerializer[].class::cast).ifPresent(serializers
 -> {
             Stream.of(serializers).forEach(s -> {

http://git-wip-us.apache.org/repos/asf/johnzon/blob/125cbf22/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperBuilder.java
----------------------------------------------------------------------
diff --git 
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperBuilder.java 
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperBuilder.java
index 606e7a9..059bf1e 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperBuilder.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperBuilder.java
@@ -130,6 +130,7 @@ public class MapperBuilder {
     private Map<Class<?>, String[]> ignoredForFields = new HashMap<Class<?>, 
String[]>();
     private boolean primitiveConverters;
     private boolean failOnUnknownProperties;
+    private SerializeValueFilter serializeValueFilter;
 
     public Mapper build() {
         if (readerFactory == null || generatorFactory == null) {
@@ -216,7 +217,8 @@ public class MapperBuilder {
                         version, close,
                         skipNull, skipEmptyArray,
                         treatByteArrayAsBase64, treatByteArrayAsBase64URL, 
readAttributeBeforeWrite,
-                        accessMode, encoding, attributeOrder, 
enforceQuoteString, failOnUnknownProperties),
+                        accessMode, encoding, attributeOrder, 
enforceQuoteString, failOnUnknownProperties,
+                        serializeValueFilter),
                 closeables);
     }
 
@@ -395,4 +397,9 @@ public class MapperBuilder {
         this.primitiveConverters = val;
         return this;
     }
+
+    public MapperBuilder setSerializeValueFilter(final SerializeValueFilter 
serializeValueFilter) {
+        this.serializeValueFilter = serializeValueFilter;
+        return this;
+    }
 }

http://git-wip-us.apache.org/repos/asf/johnzon/blob/125cbf22/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperConfig.java
----------------------------------------------------------------------
diff --git 
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperConfig.java 
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperConfig.java
index 983066d..cbd3437 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperConfig.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperConfig.java
@@ -64,6 +64,7 @@ public /* DON'T MAKE IT HIDDEN */ class MapperConfig 
implements Cloneable {
     private final Comparator<String> attributeOrder;
     private final boolean enforceQuoteString;
     private final boolean failOnUnknown;
+    private final SerializeValueFilter serializeValueFilter;
 
     private final Map<Class<?>, ObjectConverter.Writer<?>> 
objectConverterWriterCache;
     private final Map<Class<?>, ObjectConverter.Reader<?>> 
objectConverterReaderCache;
@@ -79,7 +80,8 @@ public /* DON'T MAKE IT HIDDEN */ class MapperConfig 
implements Cloneable {
                         final boolean readAttributeBeforeWrite,
                         final AccessMode accessMode, final Charset encoding,
                         final Comparator<String> attributeOrder,
-                        final boolean enforceQuoteString, final boolean 
failOnUnknown) {
+                        final boolean enforceQuoteString, final boolean 
failOnUnknown,
+                        final SerializeValueFilter serializeValueFilter) {
     //CHECKSTYLE:ON
         this.objectConverterWriters = objectConverterWriters;
         this.objectConverterReaders = objectConverterReaders;
@@ -96,11 +98,16 @@ public /* DON'T MAKE IT HIDDEN */ class MapperConfig 
implements Cloneable {
         this.attributeOrder = attributeOrder;
         this.enforceQuoteString = enforceQuoteString;
         this.failOnUnknown = failOnUnknown;
+        this.serializeValueFilter = serializeValueFilter == null ? (name, 
value) -> false : serializeValueFilter;
 
         this.objectConverterWriterCache = new HashMap<Class<?>, 
ObjectConverter.Writer<?>>(objectConverterWriters.size());
         this.objectConverterReaderCache = new HashMap<Class<?>, 
ObjectConverter.Reader<?>>(objectConverterReaders.size());
     }
 
+    public SerializeValueFilter getSerializeValueFilter() {
+        return serializeValueFilter;
+    }
+
     public Adapter findAdapter(final Type aClass) {
         final Adapter<?, ?> converter = adapters.get(new AdapterKey(aClass, 
String.class));
         if (converter != null) {

http://git-wip-us.apache.org/repos/asf/johnzon/blob/125cbf22/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java
----------------------------------------------------------------------
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 107307a..92609c9 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
@@ -292,6 +292,9 @@ public class MappingGeneratorImpl implements 
MappingGenerator {
                             final String key, final Object value,
                             final ObjectConverter.Writer objectConverter,
                             final Collection<String> ignoredProperties) throws 
InvocationTargetException, IllegalAccessException {
+        if (config.getSerializeValueFilter().shouldIgnore(key, value)) {
+            return;
+        }
         if (array) {
             final int length = Array.getLength(value);
             if (length == 0 && config.isSkipEmptyArray()) {

http://git-wip-us.apache.org/repos/asf/johnzon/blob/125cbf22/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/SerializeValueFilter.java
----------------------------------------------------------------------
diff --git 
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/SerializeValueFilter.java
 
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/SerializeValueFilter.java
new file mode 100644
index 0000000..e29fd52
--- /dev/null
+++ 
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/SerializeValueFilter.java
@@ -0,0 +1,28 @@
+/*
+ * 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;
+
+public interface SerializeValueFilter {
+    /**
+     * @param name the attribute name if set.
+     * @param value the value which will get serialized.
+     * @return true if the value should be ignored and not serialized.
+     */
+    boolean shouldIgnore(String name, Object value);
+}

http://git-wip-us.apache.org/repos/asf/johnzon/blob/125cbf22/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperConfigTest.java
----------------------------------------------------------------------
diff --git 
a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperConfigTest.java 
b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperConfigTest.java
index ca693f4..949b230 100644
--- 
a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperConfigTest.java
+++ 
b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperConfigTest.java
@@ -166,7 +166,7 @@ public class MapperConfigTest {
                                 new FieldAccessMode(true, true),
                                 Charset.forName("UTF-8"),
                                 null,
-                                false, false);
+                                false, false, null);
     }
 
 

http://git-wip-us.apache.org/repos/asf/johnzon/blob/125cbf22/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/SerializeValueFilterTest.java
----------------------------------------------------------------------
diff --git 
a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/SerializeValueFilterTest.java
 
b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/SerializeValueFilterTest.java
new file mode 100644
index 0000000..aa25f62
--- /dev/null
+++ 
b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/SerializeValueFilterTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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;
+
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.List;
+
+import static java.util.Collections.singleton;
+import static java.util.Collections.singletonList;
+import static org.junit.Assert.assertEquals;
+
+public class SerializeValueFilterTest {
+    @Test
+    public void customIgnore() {
+        // in this test we will serialize lists but not sets or other 
collection impls
+        final Mapper mapper = new 
MapperBuilder().setSerializeValueFilter((name, value) -> 
!List.class.isInstance(value)).build();
+        assertEquals("{\"list\":[\"test\"]}", mapper.writeObjectAsString(new 
Foo(singletonList("test"))));
+        assertEquals("{}", mapper.writeObjectAsString(new 
Foo(singleton("test"))));
+    }
+
+    public static class Foo {
+        public Collection<String> list;
+
+        public Foo(final Collection<String> list) {
+            this.list = list;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/johnzon/blob/125cbf22/johnzon-mapper/src/test/java/org/superbiz/ExtendMappingTest.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/test/java/org/superbiz/ExtendMappingTest.java 
b/johnzon-mapper/src/test/java/org/superbiz/ExtendMappingTest.java
index 712a8e6..1960d4e 100644
--- a/johnzon-mapper/src/test/java/org/superbiz/ExtendMappingTest.java
+++ b/johnzon-mapper/src/test/java/org/superbiz/ExtendMappingTest.java
@@ -71,7 +71,7 @@ public class ExtendMappingTest {
                         public int compare(final String o1, final String o2) {
                             return o1.compareTo(o2);
                         }
-                    }, false, false));
+                    }, false, false, null));
         }
 
         @Override

Reply via email to