Skip to content
This repository was archived by the owner on Apr 14, 2023. It is now read-only.

Commit 1be12b0

Browse files
committed
Fix #15 (wrt exception message)
1 parent 16833e5 commit 1be12b0

File tree

9 files changed

+86
-16
lines changed

9 files changed

+86
-16
lines changed

src/main/java/com/fasterxml/jackson/datatype/jsonorg/JSONArrayDeserializer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.fasterxml.jackson.databind.*;
88
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
9+
import com.fasterxml.jackson.databind.util.ClassUtil;
910

1011
import org.json.JSONArray;
1112
import org.json.JSONObject;
@@ -25,6 +26,14 @@ public JSONArrayDeserializer()
2526
public JSONArray deserialize(JsonParser p, DeserializationContext ctxt)
2627
throws IOException
2728
{
29+
// 07-Jan-2019, tatu: As per [datatype-json-org#15], need to verify it's an Array
30+
if (!p.isExpectedStartArrayToken()) {
31+
final JsonToken t = p.currentToken();
32+
return (JSONArray) ctxt.handleUnexpectedToken(handledType(), t, p,
33+
"Unexpected token (%s), expected START_ARRAY for %s value",
34+
t, ClassUtil.nameOf(handledType()));
35+
}
36+
2837
JSONArray array = new JSONArray();
2938
JsonToken t;
3039
while ((t = p.nextToken()) != JsonToken.END_ARRAY) {

src/main/java/com/fasterxml/jackson/datatype/jsonorg/JSONBaseSerializer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44

55
abstract class JSONBaseSerializer<T> extends StdSerializer<T>
66
{
7+
private static final long serialVersionUID = 1L;
8+
79
protected JSONBaseSerializer(Class<T> cls) { super(cls); }
810
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fasterxml.jackson.datatype.jsonorg;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
11+
12+
public class ConvertTest extends ModuleTestBase
13+
{
14+
static class TestDomain {
15+
public Integer id;
16+
public String name;
17+
public Double da;
18+
public Map<String,Object> ldt;
19+
public Map<String,Object> ld;
20+
public Map<String,Object> lt;
21+
public JSONObject jsn;
22+
public JSONArray jsa;
23+
}
24+
25+
private final ObjectMapper MAPPER = newMapper();
26+
27+
public void testIssue15() throws Exception
28+
{
29+
Map<String,Object> map = new HashMap<>();
30+
map.put("name", "zpj");
31+
map.put("id", 111);
32+
map.put("jsa", "[1, 34, 32, \"zpj\", {\"age\": 18, \"name\": \"zpj\", \"child\": {\"name\": \"zzy\", \"gender\": \"nan\"}}, {\"url\": \"test\", \"name\": \"suhu\"}]");
33+
final String json = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(map);
34+
try {
35+
MAPPER.readValue(json, TestDomain.class);
36+
fail("Should not pass");
37+
} catch (MismatchedInputException e) {
38+
verifyException(e, "Unexpected token");
39+
verifyException(e, "org.json.JSONArray");
40+
verifyException(e, "START_ARRAY");
41+
}
42+
}
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fasterxml.jackson.datatype.jsonorg;
2+
3+
import java.util.Arrays;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
public abstract class ModuleTestBase extends junit.framework.TestCase
8+
{
9+
public ObjectMapper newMapper() {
10+
return new ObjectMapper()
11+
.registerModule(new JsonOrgModule());
12+
}
13+
14+
protected void verifyException(Throwable e, String... matches)
15+
{
16+
String msg = e.getMessage();
17+
String lmsg = (msg == null) ? "" : msg.toLowerCase();
18+
for (String match : matches) {
19+
String lmatch = match.toLowerCase();
20+
if (lmsg.indexOf(lmatch) >= 0) {
21+
return;
22+
}
23+
}
24+
throw new Error("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one with message \""+msg+"\"");
25+
}
26+
}

src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleReadTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
88

9-
public class SimpleReadTest extends TestBase
9+
public class SimpleReadTest extends ModuleTestBase
1010
{
1111
public void testReadObject() throws Exception
1212
{

src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleWriteTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
import org.json.*;
66

7-
import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
8-
9-
public class SimpleWriteTest extends TestBase
7+
public class SimpleWriteTest extends ModuleTestBase
108
{
119
public void testWriteObject() throws Exception
1210
{
13-
ObjectMapper mapper = new ObjectMapper();
14-
mapper.registerModule(new JsonOrgModule());
11+
final ObjectMapper mapper = newMapper();
1512

1613
// Ok: let's create JSONObject from JSON text
1714
String JSON = "{\"a\":{\"b\":3}}";
@@ -28,8 +25,7 @@ public void testWriteObject() throws Exception
2825

2926
public void testWriteArray() throws Exception
3027
{
31-
ObjectMapper mapper = new ObjectMapper();
32-
mapper.registerModule(new JsonOrgModule());
28+
final ObjectMapper mapper = newMapper();
3329

3430
// Ok: let's create JSONObject from JSON text
3531
String JSON = "[1,true,\"text\",[null,3],{\"a\":[1.25]}]";

src/test/java/com/fasterxml/jackson/datatype/jsonorg/TestBase.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/test/java/com/fasterxml/jackson/datatype/jsonorg/TestVersions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.fasterxml.jackson.core.Version;
66
import com.fasterxml.jackson.core.Versioned;
77

8-
public class TestVersions extends TestBase
8+
public class TestVersions extends ModuleTestBase
99
{
1010
public void testMapperVersions() throws IOException
1111
{

src/test/java/com/fasterxml/jackson/datatype/jsonorg/TypeInformationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Tests to verify that we can also use JSONObject and JSONArray
1111
* with polymorphic type information.
1212
*/
13-
public class TypeInformationTest extends TestBase
13+
public class TypeInformationTest extends ModuleTestBase
1414
{
1515
static class ObjectWrapper {
1616
public Object value;

0 commit comments

Comments
 (0)