You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by rm...@apache.org on 2019/07/19 07:24:10 UTC
[johnzon] branch master updated: JOHNZON-222 enable to map
JsonArray in jsonb impl
This is an automated email from the ASF dual-hosted git repository.
rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
The following commit(s) were added to refs/heads/master by this push:
new 2f56ba6 JOHNZON-222 enable to map JsonArray in jsonb impl
2f56ba6 is described below
commit 2f56ba6e7dbecff45fbd4c37df84e33b916ec4aa
Author: Romain Manni-Bucau <rm...@gmail.com>
AuthorDate: Fri Jul 19 09:24:01 2019 +0200
JOHNZON-222 enable to map JsonArray in jsonb impl
---
.../org/apache/johnzon/jsonb/JohnzonJsonb.java | 13 ++++++++
.../org/apache/johnzon/jsonb/JohnzonJsonbTest.java | 38 ++++++++++++++++++++++
.../java/org/apache/johnzon/mapper/Mapper.java | 12 +++++++
3 files changed, 63 insertions(+)
diff --git a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonJsonb.java b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonJsonb.java
index adb5d66..a9adada 100644
--- a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonJsonb.java
+++ b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonJsonb.java
@@ -26,6 +26,7 @@ import org.apache.johnzon.mapper.Mapper;
import org.apache.johnzon.mapper.MapperException;
import org.apache.johnzon.mapper.reflection.JohnzonParameterizedType;
+import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonString;
import javax.json.JsonStructure;
@@ -60,6 +61,8 @@ public class JohnzonJsonb implements Jsonb, AutoCloseable, JsonbExtension {
try {
if (isArray(type)) {
return delegate.readTypedArray(new StringReader(str), type.getComponentType(), type);
+ } else if (JsonArray.class == type) {
+ return (T) delegate.readJsonArray(new StringReader(str));
} else if (Collection.class.isAssignableFrom(type)) {
return (T) delegate.readCollection(new StringReader(str), new JohnzonParameterizedType(type, Object.class));
}
@@ -117,6 +120,8 @@ public class JohnzonJsonb implements Jsonb, AutoCloseable, JsonbExtension {
if (isArray(runtimeType)) {
final Class cast = Class.class.cast(runtimeType);
return (T) delegate.readTypedArray(new StringReader(str), cast.getComponentType(), cast);
+ } else if (JsonArray.class == runtimeType) {
+ return (T) delegate.readJsonArray(new StringReader(str));
} else if (isCollection(runtimeType)) {
return (T) delegate.readCollection(new StringReader(str), ParameterizedType.class.cast(runtimeType));
}
@@ -140,6 +145,8 @@ public class JohnzonJsonb implements Jsonb, AutoCloseable, JsonbExtension {
try {
if (isArray(type)) {
return delegate.readTypedArray(reader, type.getComponentType(), type);
+ } else if (JsonArray.class == type) {
+ return (T) delegate.readJsonArray(reader);
} else if (Collection.class.isAssignableFrom(type)) {
return (T) delegate.readCollection(reader, new JohnzonParameterizedType(type, Object.class));
}
@@ -164,6 +171,8 @@ public class JohnzonJsonb implements Jsonb, AutoCloseable, JsonbExtension {
if (isArray(runtimeType)) {
final Class<T> type = Class.class.cast(runtimeType);
return delegate.readTypedArray(reader, type.getComponentType(), type);
+ } else if (JsonArray.class == runtimeType) {
+ return (T) delegate.readJsonArray(reader);
} else if (isCollection(runtimeType)) {
return (T) delegate.readCollection(reader, ParameterizedType.class.cast(runtimeType));
}
@@ -183,6 +192,8 @@ public class JohnzonJsonb implements Jsonb, AutoCloseable, JsonbExtension {
try {
if (isArray(type)) {
return delegate.readTypedArray(stream, type.getComponentType(), type);
+ } else if (JsonArray.class == type) {
+ return (T) delegate.readJsonArray(stream);
} else if (Collection.class.isAssignableFrom(type)) {
return (T) delegate.readCollection(stream, new JohnzonParameterizedType(type, Object.class));
}
@@ -203,6 +214,8 @@ public class JohnzonJsonb implements Jsonb, AutoCloseable, JsonbExtension {
if (isArray(runtimeType)) {
final Class<T> type = Class.class.cast(runtimeType);
return delegate.readTypedArray(stream, type.getComponentType(), type);
+ } else if (JsonArray.class == runtimeType) {
+ return (T) delegate.readJsonArray(stream);
} else if (isCollection(runtimeType)) {
return (T) delegate.readCollection(stream, ParameterizedType.class.cast(runtimeType));
}
diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JohnzonJsonbTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JohnzonJsonbTest.java
new file mode 100644
index 0000000..debf27f
--- /dev/null
+++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JohnzonJsonbTest.java
@@ -0,0 +1,38 @@
+/*
+ * 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 static org.junit.Assert.assertEquals;
+
+import javax.json.JsonArray;
+import javax.json.bind.Jsonb;
+import javax.json.bind.JsonbBuilder;
+
+import org.junit.Test;
+
+public class JohnzonJsonbTest {
+ @Test
+ public void jsonArray() throws Exception {
+ try (final Jsonb jsonb = JsonbBuilder.create()) {
+ final String json = "[{\"foo\":\"bar\"}]";
+ final JsonArray array = jsonb.fromJson(json, JsonArray.class);
+ assertEquals(json, array.toString());
+ }
+ }
+}
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mapper.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mapper.java
index 459fa68..1027fca 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mapper.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mapper.java
@@ -335,12 +335,24 @@ public class Mapper implements Closeable {
}
}
+ public JsonArray readJsonArray(final Reader stream) {
+ try (final JsonReader reader = readerFactory.createReader(stream(stream))) {
+ return reader.readArray();
+ }
+ }
+
public <T> T[] readArray(final InputStream stream, final Class<T> clazz) {
try (final JsonReader reader = charset == null ? readerFactory.createReader(stream(stream)): readerFactory.createReader(stream(stream), charset)) {
return (T[]) mapArray(clazz, reader);
}
}
+ public JsonArray readJsonArray(final InputStream stream) {
+ try (final JsonReader reader = charset == null ? readerFactory.createReader(stream(stream)): readerFactory.createReader(stream(stream), charset)) {
+ return reader.readArray();
+ }
+ }
+
private Object mapArray(final Class<?> clazz, final JsonReader reader) {
return mapObject(Array.newInstance(clazz, 0).getClass(), reader);
}