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 2019/07/29 14:17:40 UTC
[johnzon] branch master updated (27c2bc0 -> f2d04b6)
This is an automated email from the ASF dual-hosted git repository.
struberg pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git.
from 27c2bc0 JOHNZON-225 support of Map, Collection and arrays in initial data of object and array builders
new 83fc79e JOHNZON-202 improve/fix Adapter JavaDoc
new f2d04b6 JOHNZON-226 performance: avoid Array.getLength
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../java/org/apache/johnzon/mapper/Adapter.java | 40 +++++++++++++++++-----
.../johnzon/mapper/MappingGeneratorImpl.java | 35 ++++++++++++++++++-
2 files changed, 65 insertions(+), 10 deletions(-)
[johnzon] 02/02: JOHNZON-226 performance: avoid Array.getLength
Posted by st...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
struberg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
commit f2d04b6155861e23eb779fa7be0f948276c5c7c3
Author: Mark Struberg <st...@apache.org>
AuthorDate: Mon Jul 29 15:22:01 2019 +0200
JOHNZON-226 performance: avoid Array.getLength
Array.getLength performs rather poor as it doesn't get inlined
by the JIT due to heavy reflection.
---
.../johnzon/mapper/MappingGeneratorImpl.java | 35 +++++++++++++++++++++-
1 file changed, 34 insertions(+), 1 deletion(-)
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 f741aff..1b4d4e8 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
@@ -470,7 +470,7 @@ public class MappingGeneratorImpl implements MappingGenerator {
* @param key either the attribute key or {@code null} if the array should be rendered without key
*/
private void writeArray(Class<?> type, Adapter itemConverter, String key, Object arrayValue, Collection<String> ignoredProperties, JsonPointerTracker jsonPointer) {
- final int length = Array.getLength(arrayValue);
+ final int length = getArrayLength(arrayValue);
if (length == 0 && config.isSkipEmptyArray()) {
return;
}
@@ -582,6 +582,39 @@ public class MappingGeneratorImpl implements MappingGenerator {
generator.writeEnd();
}
+ private int getArrayLength(Object array) {
+ // Note: all types of multidimensional arrays are instanceof Object[]
+ if (array instanceof Object[]) {
+ return ((Object[]) array).length;
+ }
+ if (array instanceof boolean[]) {
+ return ((boolean[])array).length;
+ }
+ if (array instanceof byte[]) {
+ return ((byte[])array).length;
+ }
+ if (array instanceof char[]) {
+ return ((char[]) array).length;
+ }
+ if (array instanceof short[]) {
+ return ((short[]) array).length;
+ }
+ if (array instanceof int[]) {
+ return ((int[]) array).length;
+ }
+ if (array instanceof long[]) {
+ return ((long[]) array).length;
+ }
+ if (array instanceof float[]) {
+ return ((float[]) array).length;
+ }
+ if (array instanceof double[]) {
+ return ((double[]) array).length;
+ }
+
+ throw new IllegalArgumentException("This is not an array! " + array);
+ }
+
private void writeItem(final Object o, final Collection<String> ignoredProperties, JsonPointerTracker jsonPointer) {
if (o == null) {
generator.writeNull();
[johnzon] 01/02: JOHNZON-202 improve/fix Adapter JavaDoc
Posted by st...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
struberg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
commit 83fc79ee67946683479fb450e8047f29cd8165a3
Author: Mark Struberg <st...@apache.org>
AuthorDate: Mon Jul 29 11:13:36 2019 +0200
JOHNZON-202 improve/fix Adapter JavaDoc
txs to Alex falb (elexx) for the report!
---
.../java/org/apache/johnzon/mapper/Adapter.java | 40 +++++++++++++++++-----
1 file changed, 31 insertions(+), 9 deletions(-)
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Adapter.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Adapter.java
index 3abcd5f..fd63549 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Adapter.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Adapter.java
@@ -22,20 +22,42 @@ package org.apache.johnzon.mapper;
* An Adapter is similar to a {@link Converter}.
* The main difference is that a Converter always converts from/to a String.
* An adapter might e.g. convert to a Date or any other Object which will
- * then be json-ified.
+ * then be json-ified.<p>
*
- * @param <A>
- * @param <B>
+ * A small example which has a special Java type to internally represent dates.
+ * Let's call it {@code DateHolder}.
+ * Our {@code Mapper} should treat it as a {@code java.util.Date}.
+ * For doing so we create a {@code DateHolderAdapter} like the following example shows:
+ * <pre>
+ * public static class DateHolderAdapter implements Adapter<DateHolder, Date> {
+ * @Override
+ * public DateHolder to(Date date) {
+ * DateHolder dh = new DateHolder(date.getTime());
+ * return dh;
+ * }
+ *
+ * @Override
+ * public Date from(DateHolder dateHolder) {
+ * return new Date(dateHolder.getDate());
+ * }
+ * }
+ * </pre>
+ *
+ * Consider a POJO has a DateHolder.
+ * When serialising the {@code Mapper} will first use the {@code DateHolderAdapter#from(DateHolder)} and from there to JSON.
+ * When reading JSON the {@code to(Date}} method will be used.
+ *
+ * @param <POJO_TYPE> The Java type in the POJO (Plain Old Java Object)
+ * @param <JSON_TYPE> The Java Type which will be used to transform to JSON.
*/
-public interface Adapter<A, B> extends MapperConverter {
+public interface Adapter<POJO_TYPE, JSON_TYPE> extends MapperConverter {
/**
- * Transfer B to JSON as A.
- * A will be inserted into the JSON output
+ * Transfer JSONTYPE_TYPE from JSON to POJO as POJO_TYPE.
*/
- A to(B b);
+ POJO_TYPE to(JSON_TYPE b);
/**
- * Take the object A from JSON an convert it to B to store in the POJO
+ * Take the POJO_TYPE object A from a POJO an convert it to JSON_TYPE which will be inserted into the JSON output.
*/
- B from(A a);
+ JSON_TYPE from(POJO_TYPE a);
}