johnzon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Romain Manni-Bucau <rmannibu...@gmail.com>
Subject Fwd: incubator-johnzon git commit: JOHNZON-37 @JohnzonProperty
Date Sun, 22 Feb 2015 13:51:40 GMT
open point with this: do we merge @JohnzonIgnore and @JohnzonConverter
in @JohnzonProperty

Both makes sense for me so really open question ATM


Romain Manni-Bucau
@rmannibucau
http://www.tomitribe.com
http://rmannibucau.wordpress.com
https://github.com/rmannibucau



---------- Forwarded message ----------
From:  <rmannibucau@apache.org>
Date: 2015-02-22 14:49 GMT+01:00
Subject: incubator-johnzon git commit: JOHNZON-37 @JohnzonProperty
To: commits@johnzon.incubator.apache.org


Repository: incubator-johnzon
Updated Branches:
  refs/heads/master ca56cd03d -> c60739d13


JOHNZON-37 @JohnzonProperty


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

Branch: refs/heads/master
Commit: c60739d134a8eeed39777a00146dcfcff66e5017
Parents: ca56cd0
Author: Romain Manni-Bucau <rmannibucau@apache.org>
Authored: Sun Feb 22 14:49:36 2015 +0100
Committer: Romain Manni-Bucau <rmannibucau@apache.org>
Committed: Sun Feb 22 14:49:36 2015 +0100

----------------------------------------------------------------------
 .../apache/johnzon/mapper/JohnzonProperty.java  | 37 +++++++++++
 .../johnzon/mapper/access/FieldAccessMode.java  | 11 +++-
 .../johnzon/mapper/access/MethodAccessMode.java | 10 ++-
 .../org/apache/johnzon/mapper/MapperTest.java   | 68 +++++++++++++++++---
 4 files changed, 113 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/c60739d1/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/JohnzonProperty.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/JohnzonProperty.java
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/JohnzonProperty.java
new file mode 100644
index 0000000..327f9e3
--- /dev/null
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/JohnzonProperty.java
@@ -0,0 +1,37 @@
+/*
+ * 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 java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Target({ METHOD, FIELD })
+@Retention(RUNTIME)
+public @interface JohnzonProperty {
+    /**
+     * Example: @JohnzonProperty("not_java_name") will consider the
decorated property as called "not_java_name" on JSON side.
+     *
+     * @return the JSON name for this property.
+     */
+    String value();
+}

http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/c60739d1/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/FieldAccessMode.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/FieldAccessMode.java
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/FieldAccessMode.java
index 2df9bb6..1299cfa 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/FieldAccessMode.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/FieldAccessMode.java
@@ -18,6 +18,7 @@
  */
 package org.apache.johnzon.mapper.access;

+import org.apache.johnzon.mapper.JohnzonProperty;
 import org.apache.johnzon.mapper.MapperException;

 import java.lang.annotation.Annotation;
@@ -36,7 +37,8 @@ public class FieldAccessMode implements AccessMode {
             if (isIgnored(key)) {
                 continue;
             }
-            readers.put(key, new FieldReader(f.getValue()));
+
+            readers.put(extractKey(f.getValue(), key), new
FieldReader(f.getValue()));
         }
         return readers;
     }
@@ -49,11 +51,16 @@ public class FieldAccessMode implements AccessMode {
             if (isIgnored(key)) {
                 continue;
             }
-            writers.put(key, new FieldWriter(f.getValue()));
+            writers.put(extractKey(f.getValue(), key), new
FieldWriter(f.getValue()));
         }
         return writers;
     }

+    private String extractKey(final Field f, final String key) {
+        final JohnzonProperty property =
f.getAnnotation(JohnzonProperty.class);
+        return property != null ? property.value() : key;
+    }
+
     protected boolean isIgnored(final String key) {
         return key.contains("$");
     }

http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/c60739d1/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
index cd8f37d..62fee0f 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
@@ -18,6 +18,7 @@
  */
 package org.apache.johnzon.mapper.access;

+import org.apache.johnzon.mapper.JohnzonProperty;
 import org.apache.johnzon.mapper.MapperException;

 import java.beans.IntrospectionException;
@@ -40,7 +41,7 @@ public class MethodAccessMode implements AccessMode {
                 if (isIgnored(descriptor.getName())) {
                     continue;
                 }
-                readers.put(descriptor.getName(), new
MethodReader(readMethod));
+                readers.put(extractKey(descriptor), new
MethodReader(readMethod));
             }
         }
         return readers;
@@ -56,12 +57,17 @@ public class MethodAccessMode implements AccessMode {
                 if (isIgnored(descriptor.getName())) {
                     continue;
                 }
-                writers.put(descriptor.getName(), new
MethodWriter(writeMethod));
+                writers.put(extractKey(descriptor), new
MethodWriter(writeMethod));
             }
         }
         return writers;
     }

+    private String extractKey(final PropertyDescriptor f) {
+        final JohnzonProperty property = f.getReadMethod() == null ?
null : f.getReadMethod().getAnnotation(JohnzonProperty.class);
+        return property != null ? property.value() : f.getName();
+    }
+
     protected boolean isIgnored(final String name) {
         return name.equals("metaClass") || name.contains("$");
     }

http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/c60739d1/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperTest.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperTest.java
b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperTest.java
index 55cc926..cf86ec8 100644
--- a/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperTest.java
+++ b/johnzon-mapper/src/test/java/org/apache/johnzon/mapper/MapperTest.java
@@ -18,12 +18,9 @@
  */
 package org.apache.johnzon.mapper;

-import static java.util.Arrays.asList;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import org.apache.johnzon.mapper.reflection.JohnzonCollectionType;
+import org.apache.johnzon.mapper.reflection.JohnzonParameterizedType;
+import org.junit.Test;

 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -37,9 +34,12 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;

-import org.apache.johnzon.mapper.reflection.JohnzonCollectionType;
-import org.apache.johnzon.mapper.reflection.JohnzonParameterizedType;
-import org.junit.Test;
+import static java.util.Arrays.asList;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;

 public class MapperTest {
     private static final String BIG_OBJECT_STR = "{" +
"\"name\":\"the string\"," + "\"integer\":56," + "\"longnumber\":118,"
@@ -429,6 +429,30 @@ public class MapperTest {
         assertEquals("{}", value);
     }

+    @Test
+    public void aliases() {
+        {
+            final Aliases aliases = new MapperBuilder().build().readObject(
+                    new
ByteArrayInputStream("{\"super_long_property\":\"ok\"}".getBytes()),
Aliases.class);
+            assertEquals("ok", aliases.superLongProperty);
+        }
+        {
+            final Aliases aliases = new Aliases();
+            aliases.setSuperLongProperty("ok");
+            assertEquals("{\"super_long_property\":\"ok\"}", new
MapperBuilder().build().writeObjectAsString(aliases));
+        }
+        {
+            final AliasesOnField aliases = new
MapperBuilder().setAccessModeName("field").build().readObject(
+                    new
ByteArrayInputStream("{\"super_long_property\":\"ok\"}".getBytes()),
AliasesOnField.class);
+            assertEquals("ok", aliases.superLongProperty);
+        }
+        {
+            final AliasesOnField aliases = new AliasesOnField();
+            aliases.setSuperLongProperty("ok");
+            assertEquals("{\"super_long_property\":\"ok\"}", new
MapperBuilder().setAccessModeName("field").build().writeObjectAsString(aliases));
+        }
+    }
+
     public static class NanHolder {
         private Double nan = Double.NaN;

@@ -751,6 +775,32 @@ public class MapperTest {


     }
+
+    public static class Aliases {
+        private String superLongProperty;
+
+        @JohnzonProperty("super_long_property")
+        public String getSuperLongProperty() {
+            return superLongProperty;
+        }
+
+        public void setSuperLongProperty(final String superLongProperty) {
+            this.superLongProperty = superLongProperty;
+        }
+    }
+
+    public static class AliasesOnField {
+        @JohnzonProperty("super_long_property")
+        private String superLongProperty;
+
+        public String getSuperLongProperty() {
+            return superLongProperty;
+        }
+
+        public void setSuperLongProperty(final String superLongProperty) {
+            this.superLongProperty = superLongProperty;
+        }
+    }

     /*public static class ByteArray {

Mime
View raw message