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 2018/03/29 08:49:41 UTC
[1/3] johnzon git commit: JOHNZON-164 add a unit test for JsonB
throwing JsonbException on wrong data
Repository: johnzon
Updated Branches:
refs/heads/master eb93b91a5 -> a9b26270a
JOHNZON-164 add a unit test for JsonB throwing JsonbException on wrong data
Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/df261923
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/df261923
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/df261923
Branch: refs/heads/master
Commit: df2619239419ceae08db066e3853ef963987f0d0
Parents: eb93b91
Author: Mark Struberg <st...@apache.org>
Authored: Wed Mar 28 18:57:08 2018 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Wed Mar 28 18:57:08 2018 +0200
----------------------------------------------------------------------
.../org/apache/johnzon/jsonb/JsonbReadTest.java | 34 ++++++++++++++++++++
1 file changed, 34 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/johnzon/blob/df261923/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbReadTest.java
----------------------------------------------------------------------
diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbReadTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbReadTest.java
index c3d10b0..d8e2d9d 100644
--- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbReadTest.java
+++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbReadTest.java
@@ -20,14 +20,20 @@ package org.apache.johnzon.jsonb;
import org.junit.Test;
+import javax.json.Json;
+import javax.json.bind.JsonbConfig;
+import javax.json.bind.JsonbException;
import javax.json.bind.annotation.JsonbDateFormat;
import javax.json.bind.annotation.JsonbProperty;
+import javax.json.bind.config.BinaryDataStrategy;
import javax.json.bind.spi.JsonbProvider;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
+import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
+import java.util.Base64;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -94,6 +100,22 @@ public class JsonbReadTest {
assertEquals("test", JsonbProvider.provider().create().build().fromJson(new StringReader(json), SimpleProperty.class).value);
}
+ @Test
+ public void testValidBase64() {
+ String json = "{\"blob\":\"" + Base64.getEncoder().encodeToString("test".getBytes(StandardCharsets.UTF_8)) + "\"}";
+ JsonbConfig cfg = new JsonbConfig()
+ .withBinaryDataStrategy(BinaryDataStrategy.BASE_64);
+ SimpleBinaryDto simpleBinaryDto = JsonbProvider.provider().create().withConfig(cfg).build().fromJson(new StringReader(json), SimpleBinaryDto.class);
+ assertEquals("test", new String(simpleBinaryDto.getBlob(), StandardCharsets.UTF_8));
+ }
+
+ @Test(expected = JsonbException.class)
+ public void testInvalidBase64() {
+ String jsonWithIllegalBase64 = "{\"blob\":\"dGVXz@dA==\"}";
+ JsonbConfig cfg = new JsonbConfig()
+ .withBinaryDataStrategy(BinaryDataStrategy.BASE_64);
+ SimpleBinaryDto simpleBinaryDto = JsonbProvider.provider().create().withConfig(cfg).build().fromJson(new StringReader(jsonWithIllegalBase64), SimpleBinaryDto.class);
+ }
public static class Simple {
@@ -147,4 +169,16 @@ public class JsonbReadTest {
this.date = value;
}
}
+
+ public static class SimpleBinaryDto {
+ private byte[] blob;
+
+ public byte[] getBlob() {
+ return blob;
+ }
+
+ public void setBlob(byte[] blob) {
+ this.blob = blob;
+ }
+ }
}
[3/3] johnzon git commit: JOHNZON-164 proper handling of conversion
Exceptions
Posted by st...@apache.org.
JOHNZON-164 proper handling of conversion Exceptions
Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/a9b26270
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/a9b26270
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/a9b26270
Branch: refs/heads/master
Commit: a9b26270a55b6d6a64fed42aa3e73daf932dcbc9
Parents: cec66e8
Author: Mark Struberg <st...@apache.org>
Authored: Thu Mar 29 10:48:02 2018 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Thu Mar 29 10:48:02 2018 +0200
----------------------------------------------------------------------
.../java/org/apache/johnzon/jsonb/JsonbReadTest.java | 1 -
.../org/apache/johnzon/mapper/MappingParserImpl.java | 13 ++++++++++---
2 files changed, 10 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/johnzon/blob/a9b26270/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbReadTest.java
----------------------------------------------------------------------
diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbReadTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbReadTest.java
index d8e2d9d..bd63127 100644
--- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbReadTest.java
+++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbReadTest.java
@@ -20,7 +20,6 @@ package org.apache.johnzon.jsonb;
import org.junit.Test;
-import javax.json.Json;
import javax.json.bind.JsonbConfig;
import javax.json.bind.JsonbException;
import javax.json.bind.annotation.JsonbDateFormat;
http://git-wip-us.apache.org/repos/asf/johnzon/blob/a9b26270/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java
index 8e74e3c..c16ce92 100644
--- a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java
+++ b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java
@@ -696,9 +696,16 @@ public class MappingParserImpl implements MappingParser {
}
}
- return converter == null ? toObject(baseInstance, jsonValue, type, itemConverter, jsonPointer, rootType)
- : jsonValue.getValueType() == JsonValue.ValueType.STRING ? converter.to(JsonString.class.cast(jsonValue).getString())
- : convertTo(converter, jsonValue, jsonPointer);
+ try {
+ return converter == null ? toObject(baseInstance, jsonValue, type, itemConverter, jsonPointer, rootType)
+ : jsonValue.getValueType() == JsonValue.ValueType.STRING ? converter.to(JsonString.class.cast(jsonValue).getString())
+ : convertTo(converter, jsonValue, jsonPointer);
+ } catch (Exception e) {
+ if (e instanceof MapperException) {
+ throw e;
+ }
+ throw new MapperException(e);
+ }
}
[2/3] johnzon git commit: JOHNZON-164 add test for json-p behaviour
Posted by st...@apache.org.
JOHNZON-164 add test for json-p behaviour
Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/cec66e8e
Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/cec66e8e
Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/cec66e8e
Branch: refs/heads/master
Commit: cec66e8e351919564f979c0947fea2207ec6e3ca
Parents: df26192
Author: Mark Struberg <st...@apache.org>
Authored: Thu Mar 29 10:41:11 2018 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Thu Mar 29 10:41:11 2018 +0200
----------------------------------------------------------------------
.../apache/johnzon/core/JsonReaderImplTest.java | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/johnzon/blob/cec66e8e/johnzon-core/src/test/java/org/apache/johnzon/core/JsonReaderImplTest.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonReaderImplTest.java b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonReaderImplTest.java
index 216610e..1fa809d 100644
--- a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonReaderImplTest.java
+++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonReaderImplTest.java
@@ -49,7 +49,6 @@ import org.junit.Test;
public class JsonReaderImplTest {
-
public JsonReaderImplTest() {
if (!Charset.defaultCharset().equals(Charset.forName("UTF-8"))) {
System.err.println("Default charset is " + Charset.defaultCharset() + ", must must be UTF-8");
@@ -184,7 +183,7 @@ public class JsonReaderImplTest {
assertEquals("hallo\u20acö\uffff \u08a5 থ?ߧ$%&´'`*+#\udbff\udfff", object.getString("নa"));
reader.close();
}
-
+
@Test
public void specialKeysWithStringAsByteArrayInputStream() {
final String s = "{\"\\\"a\":\"\u0055\",\"\u0055\":\"test2\"}";
@@ -359,7 +358,7 @@ public class JsonReaderImplTest {
assertEquals(-2, array.getInt(1));
reader.close();
}
-
+
@Test
public void simple2BadBufferSize8() {
final JsonReader reader = Json.createReaderFactory(new HashMap<String, Object>() {
@@ -446,8 +445,8 @@ public class JsonReaderImplTest {
@Test
public void stringescapeVariousBufferSizes() {
- final int[] buffersizes = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
- 26, 27, 28, 32, 64, 128, 1024, 8192 };
+ final int[] buffersizes = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
+ 26, 27, 28, 32, 64, 128, 1024, 8192};
for (final int buffersize : buffersizes) {
final String value = String.valueOf(buffersize);
@@ -569,4 +568,12 @@ public class JsonReaderImplTest {
assertEquals(1234.5, JsonNumber.class.cast(value).doubleValue(), 0.);
}
}
+
+
+ @Test(expected = JsonParsingException.class)
+ public void testInvalidNumber() {
+ String jsonWithIllegalNumber = "{\"val\":12.34-2}";
+ JsonReaderImpl.class.cast(Json.createReader(new StringReader(jsonWithIllegalNumber))).readObject();
+ }
+
}
\ No newline at end of file