14 | {
15 | public final static JSONObjectSerializer instance = new JSONObjectSerializer();
16 |
17 | public JSONObjectSerializer()
18 | {
19 | super(JSONObject.class);
20 | }
21 |
22 | @Override // since 2.6
23 | public boolean isEmpty(SerializerProvider provider, JSONObject value) {
24 | return (value == null) || value.length() == 0;
25 | }
26 |
27 | @Override
28 | public void serialize(JSONObject value, JsonGenerator g, SerializerProvider provider)
29 | throws IOException
30 | {
31 | g.writeStartObject(value);
32 | serializeContents(value, g, provider);
33 | g.writeEndObject();
34 | }
35 |
36 | @Override
37 | public void serializeWithType(JSONObject value, JsonGenerator g, SerializerProvider ctxt,
38 | TypeSerializer typeSer) throws IOException
39 | {
40 | g.setCurrentValue(value);
41 | WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt,
42 | typeSer.typeId(value, JsonToken.START_OBJECT));
43 | serializeContents(value, g, ctxt);
44 | typeSer.writeTypeSuffix(g, ctxt, typeIdDef);
45 |
46 | }
47 |
48 | protected void serializeContents(JSONObject value, JsonGenerator g, SerializerProvider provider)
49 | throws IOException
50 | {
51 | Iterator> it = value.keys();
52 | while (it.hasNext()) {
53 | String key = (String) it.next();
54 | Object ob = value.opt(key);
55 | if (ob == null || ob == JSONObject.NULL) {
56 | // 28-Mar-2019, tatu: Should possibly support filter of empty/null/default values?
57 | g.writeNullField(key);
58 | continue;
59 | }
60 | g.writeFieldName(key);
61 | Class> cls = ob.getClass();
62 | if (cls == JSONObject.class) {
63 | serialize((JSONObject) ob, g, provider);
64 | } else if (cls == JSONArray.class) {
65 | JSONArraySerializer.instance.serialize((JSONArray) ob, g, provider);
66 | } else if (cls == String.class) {
67 | g.writeString((String) ob);
68 | } else if (cls == Integer.class) {
69 | g.writeNumber(((Integer) ob).intValue());
70 | } else if (cls == Long.class) {
71 | g.writeNumber(((Long) ob).longValue());
72 | } else if (cls == Boolean.class) {
73 | g.writeBoolean(((Boolean) ob).booleanValue());
74 | } else if (cls == Double.class) {
75 | g.writeNumber(((Double) ob).doubleValue());
76 | } else if (JSONObject.class.isAssignableFrom(cls)) { // sub-class
77 | serialize((JSONObject) ob, g, provider);
78 | } else if (JSONArray.class.isAssignableFrom(cls)) { // sub-class
79 | JSONArraySerializer.instance.serialize((JSONArray) ob, g, provider);
80 | } else {
81 | provider.writeValue(g, ob);
82 | }
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/main/java/com/fasterxml/jackson/datatype/jsonorg/JsonOrgModule.java:
--------------------------------------------------------------------------------
1 | package com.fasterxml.jackson.datatype.jsonorg;
2 |
3 | import com.fasterxml.jackson.databind.module.SimpleModule;
4 |
5 | import org.json.JSONArray;
6 | import org.json.JSONObject;
7 |
8 | public class JsonOrgModule extends SimpleModule
9 | {
10 | private static final long serialVersionUID = 1;
11 |
12 | private final static String NAME = "JsonOrgModule";
13 |
14 | /*
15 | /**********************************************************
16 | /* Life-cycle
17 | /**********************************************************
18 | */
19 |
20 | public JsonOrgModule()
21 | {
22 | super(NAME, PackageVersion.VERSION);
23 | addDeserializer(JSONArray.class, JSONArrayDeserializer.instance);
24 | addDeserializer(JSONObject.class, JSONObjectDeserializer.instance);
25 | addSerializer(JSONArraySerializer.instance);
26 | addSerializer(JSONObjectSerializer.instance);
27 | }
28 | }
--------------------------------------------------------------------------------
/src/main/java/com/fasterxml/jackson/datatype/jsonorg/PackageVersion.java.in:
--------------------------------------------------------------------------------
1 | package @package@;
2 |
3 | import com.fasterxml.jackson.core.Version;
4 | import com.fasterxml.jackson.core.Versioned;
5 | import com.fasterxml.jackson.core.util.VersionUtil;
6 |
7 | /**
8 | * Automatically generated from PackageVersion.java.in during
9 | * packageVersion-generate execution of maven-replacer-plugin in
10 | * pom.xml.
11 | */
12 | public final class PackageVersion implements Versioned {
13 | public final static Version VERSION = VersionUtil.parseVersion(
14 | "@projectversion@", "@projectgroupid@", "@projectartifactid@");
15 |
16 | @Override
17 | public Version version() {
18 | return VERSION;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/fasterxml/jackson/datatype/jsonorg/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Package that contains experimental implementation of
3 | * "Binary-Encoded JSON-Like" data format handlers (parser,
4 | * generator, factory produce both, supporting constants).
5 | *
6 | * See Smile format specification for more details.
7 | *
8 | * @since 1.6
9 | */
10 | package com.fasterxml.jackson.datatype.jsonorg;
11 |
--------------------------------------------------------------------------------
/src/main/resources/META-INF/LICENSE:
--------------------------------------------------------------------------------
1 | This copy of Jackson JSON processor streaming parser/generator is licensed under the
2 | Apache (Software) License, version 2.0 ("the License").
3 | See the License for details about distribution rights, and the
4 | specific rights regarding derivate works.
5 |
6 | You may obtain a copy of the License at:
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
--------------------------------------------------------------------------------
/src/main/resources/META-INF/services/com.fasterxml.jackson.databind.Module:
--------------------------------------------------------------------------------
1 | com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule
2 |
--------------------------------------------------------------------------------
/src/moditect/module-info.java:
--------------------------------------------------------------------------------
1 | // Generated 27-Mar-2019 using Moditect maven plugin
2 | module com.fasterxml.jackson.datatype.jsonorg {
3 | requires com.fasterxml.jackson.core;
4 | requires com.fasterxml.jackson.databind;
5 |
6 | // is this the package name
7 | requires static json;
8 | //^2015
9 | requires static org.json;
10 |
11 | exports com.fasterxml.jackson.datatype.jsonorg;
12 |
13 | provides com.fasterxml.jackson.databind.Module with
14 | com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/test/java/com/fasterxml/jackson/datatype/jsonorg/ConvertTest.java:
--------------------------------------------------------------------------------
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 ldt;
19 | public Map ld;
20 | public Map 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 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 | }
44 |
--------------------------------------------------------------------------------
/src/test/java/com/fasterxml/jackson/datatype/jsonorg/ModuleTestBase.java:
--------------------------------------------------------------------------------
1 | package com.fasterxml.jackson.datatype.jsonorg;
2 |
3 | import java.util.Arrays;
4 |
5 | import com.fasterxml.jackson.databind.DatabindContext;
6 | import com.fasterxml.jackson.databind.JavaType;
7 | import com.fasterxml.jackson.databind.ObjectMapper;
8 | import com.fasterxml.jackson.databind.json.JsonMapper;
9 | import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
10 |
11 | public abstract class ModuleTestBase extends junit.framework.TestCase
12 | {
13 | protected static class NoCheckSubTypeValidator
14 | extends PolymorphicTypeValidator.Base
15 | {
16 | private static final long serialVersionUID = 1L;
17 |
18 | @Override
19 | public Validity validateBaseType(DatabindContext ctxt, JavaType baseType) {
20 | return Validity.ALLOWED;
21 | }
22 | }
23 |
24 | public ObjectMapper newMapper() {
25 | return newMapperBuilder().build();
26 | }
27 |
28 | public JsonMapper.Builder newMapperBuilder() {
29 | return JsonMapper.builder()
30 | .addModule(new JsonOrgModule());
31 | }
32 |
33 | protected void verifyException(Throwable e, String... matches)
34 | {
35 | String msg = e.getMessage();
36 | String lmsg = (msg == null) ? "" : msg.toLowerCase();
37 | for (String match : matches) {
38 | String lmatch = match.toLowerCase();
39 | if (lmsg.indexOf(lmatch) >= 0) {
40 | return;
41 | }
42 | }
43 | throw new Error("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one with message \""+msg+"\"");
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleReadTest.java:
--------------------------------------------------------------------------------
1 | package com.fasterxml.jackson.datatype.jsonorg;
2 |
3 | import com.fasterxml.jackson.databind.*;
4 |
5 | import org.json.*;
6 |
7 | public class SimpleReadTest extends ModuleTestBase
8 | {
9 | public void testReadObject() throws Exception
10 | {
11 | final ObjectMapper mapper = newMapper();
12 |
13 | JSONObject ob = mapper.readValue("{\"a\":{\"b\":3}, \"c\":[9, -4], \"d\":null, \"e\":true}",
14 | JSONObject.class);
15 | assertEquals(4, ob.length());
16 | JSONObject ob2 = ob.getJSONObject("a");
17 | assertEquals(1, ob2.length());
18 | assertEquals(3, ob2.getInt("b"));
19 | JSONArray array = ob.getJSONArray("c");
20 | assertEquals(2, array.length());
21 | assertEquals(9, array.getInt(0));
22 | assertEquals(-4, array.getInt(1));
23 | assertTrue(ob.isNull("d"));
24 | assertTrue(ob.getBoolean("e"));
25 | }
26 |
27 | public void testReadArray() throws Exception
28 | {
29 | final ObjectMapper mapper = newMapper();
30 |
31 | JSONArray array = mapper.readValue("[null, 13, false, 1.25, \"abc\", {\"a\":13}, [ ] ]",
32 | JSONArray.class);
33 | assertEquals(7, array.length());
34 | assertTrue(array.isNull(0));
35 | assertEquals(13, array.getInt(1));
36 | assertFalse(array.getBoolean(2));
37 | assertEquals(Double.valueOf(1.25), array.getDouble(3));
38 | assertEquals("abc", array.getString(4));
39 | JSONObject ob = array.getJSONObject(5);
40 | assertEquals(1, ob.length());
41 | assertEquals(13, ob.getInt("a"));
42 | JSONArray array2 = array.getJSONArray(6);
43 | assertEquals(0, array2.length());
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/com/fasterxml/jackson/datatype/jsonorg/SimpleWriteTest.java:
--------------------------------------------------------------------------------
1 | package com.fasterxml.jackson.datatype.jsonorg;
2 |
3 | import com.fasterxml.jackson.databind.*;
4 |
5 | import org.json.*;
6 |
7 | public class SimpleWriteTest extends ModuleTestBase
8 | {
9 | public void testWriteObject() throws Exception
10 | {
11 | final ObjectMapper mapper = newMapper();
12 |
13 | // Ok: let's create JSONObject from JSON text
14 | String JSON = "{\"a\":{\"b\":3}}";
15 | JSONTokener tok = new JSONTokener(JSON);
16 | JSONObject ob = (JSONObject) tok.nextValue();
17 | assertEquals(JSON, mapper.writeValueAsString(ob));
18 |
19 | // And for [Issue#2], with null(s):
20 | JSON = "{\"a\":null}";
21 | tok = new JSONTokener(JSON);
22 | ob = (JSONObject) tok.nextValue();
23 | assertEquals(JSON, mapper.writeValueAsString(ob));
24 | }
25 |
26 | public void testWriteArray() throws Exception
27 | {
28 | final ObjectMapper mapper = newMapper();
29 |
30 | // Ok: let's create JSONObject from JSON text
31 | String JSON = "[1,true,\"text\",[null,3],{\"a\":[1.25]}]";
32 | JSONTokener tok = new JSONTokener(JSON);
33 | JSONArray ob = (JSONArray) tok.nextValue();
34 | assertEquals(JSON, mapper.writeValueAsString(ob));
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/test/java/com/fasterxml/jackson/datatype/jsonorg/TestVersions.java:
--------------------------------------------------------------------------------
1 | package com.fasterxml.jackson.datatype.jsonorg;
2 |
3 | import java.io.*;
4 |
5 | import com.fasterxml.jackson.core.Version;
6 | import com.fasterxml.jackson.core.Versioned;
7 |
8 | public class TestVersions extends ModuleTestBase
9 | {
10 | public void testMapperVersions() throws IOException
11 | {
12 | JsonOrgModule module = new JsonOrgModule();
13 | assertVersion(module);
14 | }
15 |
16 | /*
17 | /**********************************************************
18 | /* Helper methods
19 | /**********************************************************
20 | */
21 |
22 | private void assertVersion(Versioned vers)
23 | {
24 | final Version v = vers.version();
25 | assertEquals(PackageVersion.VERSION, v);
26 | }
27 | }
28 |
29 |
--------------------------------------------------------------------------------
/src/test/java/com/fasterxml/jackson/datatype/jsonorg/TypeInformationTest.java:
--------------------------------------------------------------------------------
1 | package com.fasterxml.jackson.datatype.jsonorg;
2 |
3 | import com.fasterxml.jackson.databind.*;
4 |
5 | import org.json.*;
6 |
7 | /**
8 | * Tests to verify that we can also use JSONObject and JSONArray
9 | * with polymorphic type information.
10 | */
11 | public class TypeInformationTest extends ModuleTestBase
12 | {
13 | static class ObjectWrapper {
14 | public Object value;
15 |
16 | public ObjectWrapper(Object v) { value = v; }
17 | public ObjectWrapper() { }
18 | }
19 |
20 | private final ObjectMapper POLY_MAPPER = newMapperBuilder()
21 | .activateDefaultTyping(new NoCheckSubTypeValidator())
22 | .build();
23 |
24 | public void testWrappedArray() throws Exception
25 | {
26 | JSONTokener tok = new JSONTokener("[13]");
27 | JSONArray array = (JSONArray) tok.nextValue();
28 |
29 | String json = POLY_MAPPER.writeValueAsString(new ObjectWrapper(array));
30 | assertEquals("{\"value\":[\"org.json.JSONArray\",[13]]}", json);
31 |
32 | ObjectWrapper result = POLY_MAPPER.readValue(json, ObjectWrapper.class);
33 | assertEquals(JSONArray.class, result.value.getClass());
34 | JSONArray resultArray = (JSONArray) result.value;
35 | assertEquals(1, resultArray.length());
36 | assertEquals(13, resultArray.getInt(0));
37 | }
38 |
39 | public void testWrappedObject() throws Exception
40 | {
41 | JSONTokener tok = new JSONTokener("{\"a\":true}");
42 | JSONObject array = (JSONObject) tok.nextValue();
43 |
44 | String json = POLY_MAPPER.writeValueAsString(new ObjectWrapper(array));
45 | assertEquals("{\"value\":[\"org.json.JSONObject\",{\"a\":true}]}", json);
46 |
47 | ObjectWrapper result = POLY_MAPPER.readValue(json, ObjectWrapper.class);
48 | assertEquals(JSONObject.class, result.value.getClass());
49 | JSONObject resultOb = (JSONObject) result.value;
50 | assertEquals(1, resultOb.length());
51 | assertTrue(resultOb.getBoolean("a"));
52 | }
53 | }
54 |
--------------------------------------------------------------------------------