├── .gitattributes ├── .gitignore ├── README.md ├── pom.xml ├── release-notes └── VERSION-2.x └── src ├── main ├── java │ └── com │ │ └── fasterxml │ │ └── jackson │ │ └── datatype │ │ └── jsonorg │ │ ├── JSONArrayDeserializer.java │ │ ├── JSONArraySerializer.java │ │ ├── JSONBaseSerializer.java │ │ ├── JSONObjectDeserializer.java │ │ ├── JSONObjectSerializer.java │ │ ├── JsonOrgModule.java │ │ ├── PackageVersion.java.in │ │ └── package-info.java └── resources │ └── META-INF │ ├── LICENSE │ └── services │ └── com.fasterxml.jackson.databind.Module ├── moditect └── module-info.java └── test └── java └── com └── fasterxml └── jackson └── datatype └── jsonorg ├── ConvertTest.java ├── ModuleTestBase.java ├── SimpleReadTest.java ├── SimpleWriteTest.java ├── TestVersions.java └── TypeInformationTest.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Do not merge `pom.xml` from older version, as it will typically conflict 2 | 3 | pom.xml merge=ours 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # use glob syntax. 2 | syntax: glob 3 | *.class 4 | *~ 5 | *.bak 6 | *.off 7 | *.old 8 | .DS_Store 9 | 10 | # building 11 | target 12 | 13 | # Eclipse 14 | .classpath 15 | .project 16 | .settings 17 | 18 | # IDEA 19 | *.iml 20 | *.ipr 21 | *.iws 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **NOTE**: This module has become part of [Jackson Misc Datatypes](../../../jackson-datatypes-misc) 2 | repository as of Jackson 2.11.0 3 | 4 | Versions prior to 2.11.x will still be built from maintenance branches in this repo, however. 5 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.fasterxml.jackson 5 | jackson-base 6 | 3.0.0-SNAPSHOT 7 | 8 | com.fasterxml.jackson.datatype 9 | jackson-datatype-json-org 10 | bundle 11 | Jackson-datatype-json.org 12 | 3.0.0-SNAPSHOT 13 | Support for datatypes of "org.json" JSON library (see http://json.org/java), 14 | mainly to make it easier to upgrade code to Jackson, using automated conversions. 15 | 16 | http://github.com/FasterXML/jackson-datatype-json-org 17 | 18 | scm:git:git@github.com:FasterXML/jackson-datatype-json-org.git 19 | scm:git:git@github.com:FasterXML/jackson-datatype-json-org.git 20 | http://github.com/FasterXML/jackson-datatype-json-org 21 | HEAD 22 | 23 | 24 | 25 | com/fasterxml/jackson/datatype/jsonorg 26 | ${project.groupId}.jsonorg 27 | 28 | 29 | 30 | 31 | 32 | com.fasterxml.jackson.core 33 | jackson-core 34 | 35 | 36 | com.fasterxml.jackson.core 37 | jackson-databind 38 | 39 | 40 | 41 | org.json 42 | json 43 | 20180813 44 | 45 | 46 | 47 | 49 | 50 | 51 | sonatype-nexus-snapshots 52 | Sonatype Nexus Snapshots 53 | https://oss.sonatype.org/content/repositories/snapshots 54 | false 55 | true 56 | 57 | 58 | 59 | 60 | 61 | 62 | com.google.code.maven-replacer-plugin 63 | replacer 64 | 65 | 68 | 69 | org.moditect 70 | moditect-maven-plugin 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /release-notes/VERSION-2.x: -------------------------------------------------------------------------------- 1 | Project: jackson-datatype-json-org 2 | 3 | ------------------------------------------------------------------------ 4 | === Releases === 5 | ------------------------------------------------------------------------ 6 | 7 | 2.11.0 (not yet released) 8 | 9 | - 10 | 11 | 2.10.5 (21-Jul-2020) 12 | 2.10.4 (03-May-2020) 13 | 2.10.3 (03-Mar-2020) 14 | 2.10.2 (05-Jan-2020) 15 | 2.10.1 (09-Nov-2019) 16 | 17 | No changes since 2.10.0 18 | 19 | 2.10.0 (26-Sep-2019) 20 | 21 | - Add JDK9+ module-info using Moditect plugin 22 | - Update `org.json` dependency to version `20180813` 23 | 24 | 2.9.10 (21-Sep-2019) 25 | 26 | No changes since 2.9.9 27 | 28 | 2.9.9 (16-May-2019) 29 | 30 | #15: Misleading exception when trying to deserialize JSON String as `org.json.JSONArray` value 31 | 32 | 2.9.8 (15-Dec-2018) 33 | 2.9.7 (19-Sep-2018) 34 | 2.9.6 (12-Jun-2018) 35 | 2.9.5 (26-Mar-2018) 36 | 37 | No changes since 2.9.4 38 | 39 | 2.9.4 (24-Jan-2018) 40 | 41 | #12: Change `json-20090211_1` dependency to work on Java 9 42 | 43 | 2.9.3 (09-Dec-2017) 44 | 2.9.2 (14-Oct-2017) 45 | 2.9.1 (07-Jul-2017) 46 | 2.9.0 (30-Jul-2017) 47 | 48 | No changes since 2.8 49 | 50 | 2.8.6 (12-Jan-2017) 51 | 2.8.5 (14-Nov-2016) 52 | 2.8.4 (14-Oct-2016) 53 | 2.8.3 (17-Sep-2016) 54 | 2.8.2 (30-Aug-2016) 55 | 2.8.1 (20-Jul-2016) 56 | 57 | No changes since 2.8.0. 58 | 59 | 2.8.0 (04-Jul-2016) 60 | 61 | No changes since 2.7 62 | 63 | 2.7.8 (26-Sep-2016) 64 | 2.7.7 (27-Aug-2016) 65 | 2.7.6 (23-Jul-2016) 66 | 2.7.5 (11-Jun-2016) 67 | 2.7.4 (29-Apr-2016) 68 | 2.7.3 (15-Mar-2016) 69 | 2.7.2 (27-Feb-2016) 70 | 2.7.1 (02-Feb-2016) 71 | 2.7.0 (10-Jan-2016) 72 | 73 | No changes since 2.6 74 | 75 | 2.6.6 (05-Apr-2016) 76 | 2.6.5 (19-Jan-2016) 77 | 2.6.4 (07-Dec-2015) 78 | 2.6.3 (12-Oct-2015) 79 | 2.6.2 (15-Sep-2015) 80 | 2.6.1 (09-Aug-2015) 81 | 82 | No changes since 2.6.0 83 | 84 | 2.6.0 (19-Jul-2015) 85 | 86 | - Add `isEmpty()` method for JSONObject, JSONArray serializers (partial support) 87 | 88 | 2.5.3 (24-Apr-2015) 89 | 2.5.2 (29-Mar-2015) 90 | 2.5.1 (06-Feb-2015) 91 | 2.5.0 (01-Jan-2015) 92 | 93 | No changes since 2.4. 94 | 95 | 2.4.4 (24-Nov-2014) 96 | 2.4.3 (04-Oct-2014) 97 | 2.4.2 (15-Aug-2014) 98 | 2.4.1 (17-Jun-2014) 99 | 100 | No changes since 2.4.0. 101 | 102 | 2.4.0 (03-Jun-2014) 103 | 104 | #6: Change org.json package dependency to use "org.apache.geronimo.bundles#json" 105 | instead of one from "org.json"), to work better with OSGi. 106 | (requested by laszlohordos@github) 107 | 108 | 2.3.3 (10-Apr-2014) 109 | 2.3.2 (01-Mar-2014) 110 | 111 | No changes since 2.3.1. 112 | 113 | 2.3.1 (28-Dec-2013) 114 | 115 | #1: Add `META-INF/LICENSE` in jar 116 | #2: Fix problem when serializing a `JSONObject` with null values 117 | (reported by mpoindexter@github) 118 | 119 | 2.3.0 (14-Nov-2013) 120 | 2.2.3 (25-Aug-2013) 121 | 2.2.2 (27-May-2013) 122 | 2.2.1 (04-May-2013) 123 | 124 | No functional changes. 125 | 126 | 2.2.0 (23-Apr-2013) 127 | 128 | No functional changes. 129 | 130 | 2.1.2 (08-Dec-2012 131 | 2.1.1 (13-Nov-2012) 132 | 133 | No functional changes. 134 | 135 | 2.1.0 (08-Oct-2012) 136 | 137 | New minor release, no functional changes, just depends on core Jackson 2.1 138 | 139 | 2.0.2 (18-May-2012) 140 | 141 | No changes 142 | 143 | 2.0.0 (25-Mar-2012) 144 | 145 | Official 2.0 compatible version 146 | -------------------------------------------------------------------------------- /src/main/java/com/fasterxml/jackson/datatype/jsonorg/JSONArrayDeserializer.java: -------------------------------------------------------------------------------- 1 | package com.fasterxml.jackson.datatype.jsonorg; 2 | 3 | import java.io.IOException; 4 | 5 | import com.fasterxml.jackson.core.*; 6 | 7 | import com.fasterxml.jackson.databind.*; 8 | import com.fasterxml.jackson.databind.deser.std.StdDeserializer; 9 | import com.fasterxml.jackson.databind.util.ClassUtil; 10 | 11 | import org.json.JSONArray; 12 | import org.json.JSONObject; 13 | 14 | public class JSONArrayDeserializer extends StdDeserializer 15 | { 16 | public final static JSONArrayDeserializer instance = new JSONArrayDeserializer(); 17 | 18 | public JSONArrayDeserializer() 19 | { 20 | super(JSONArray.class); 21 | } 22 | 23 | @Override 24 | public JSONArray deserialize(JsonParser p, DeserializationContext ctxt) 25 | throws IOException 26 | { 27 | // 07-Jan-2019, tatu: As per [datatype-json-org#15], need to verify it's an Array 28 | if (!p.isExpectedStartArrayToken()) { 29 | final JsonToken t = p.currentToken(); 30 | return (JSONArray) ctxt.handleUnexpectedToken(getValueType(ctxt), t, p, 31 | "Unexpected token (%s), expected START_ARRAY for %s value", 32 | t, ClassUtil.nameOf(handledType())); 33 | } 34 | 35 | JSONArray array = new JSONArray(); 36 | JsonToken t; 37 | while ((t = p.nextToken()) != JsonToken.END_ARRAY) { 38 | switch (t) { 39 | case START_ARRAY: 40 | array.put(deserialize(p, ctxt)); 41 | continue; 42 | case START_OBJECT: 43 | array.put(JSONObjectDeserializer.instance.deserialize(p, ctxt)); 44 | continue; 45 | case VALUE_STRING: 46 | array.put(p.getText()); 47 | continue; 48 | case VALUE_NULL: 49 | array.put(JSONObject.NULL); 50 | continue; 51 | case VALUE_TRUE: 52 | array.put(Boolean.TRUE); 53 | continue; 54 | case VALUE_FALSE: 55 | array.put(Boolean.FALSE); 56 | continue; 57 | case VALUE_NUMBER_INT: 58 | array.put(p.getNumberValue()); 59 | continue; 60 | case VALUE_NUMBER_FLOAT: 61 | array.put(p.getNumberValue()); 62 | continue; 63 | case VALUE_EMBEDDED_OBJECT: 64 | array.put(p.getEmbeddedObject()); 65 | continue; 66 | default: 67 | return (JSONArray) ctxt.handleUnexpectedToken(getValueType(ctxt), p); 68 | } 69 | } 70 | return array; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/fasterxml/jackson/datatype/jsonorg/JSONArraySerializer.java: -------------------------------------------------------------------------------- 1 | package com.fasterxml.jackson.datatype.jsonorg; 2 | 3 | import java.io.IOException; 4 | 5 | import org.json.JSONArray; 6 | import org.json.JSONObject; 7 | 8 | import com.fasterxml.jackson.core.*; 9 | import com.fasterxml.jackson.core.type.WritableTypeId; 10 | import com.fasterxml.jackson.databind.*; 11 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer; 12 | 13 | public class JSONArraySerializer extends JSONBaseSerializer 14 | { 15 | public final static JSONArraySerializer instance = new JSONArraySerializer(); 16 | 17 | public JSONArraySerializer() 18 | { 19 | super(JSONArray.class); 20 | } 21 | 22 | @Override // since 2.6 23 | public boolean isEmpty(SerializerProvider provider, JSONArray value) { 24 | return (value == null) || value.length() == 0; 25 | } 26 | 27 | @Override 28 | public void serialize(JSONArray value, JsonGenerator g, SerializerProvider provider) throws IOException 29 | { 30 | g.writeStartArray(); 31 | serializeContents(value, g, provider); 32 | g.writeEndArray(); 33 | } 34 | 35 | @Override 36 | public void serializeWithType(JSONArray value, JsonGenerator g, SerializerProvider ctxt, 37 | TypeSerializer typeSer) throws IOException 38 | { 39 | g.setCurrentValue(value); 40 | WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, ctxt, 41 | typeSer.typeId(value, JsonToken.START_ARRAY)); 42 | serializeContents(value, g, ctxt); 43 | typeSer.writeTypeSuffix(g, ctxt, typeIdDef); 44 | } 45 | 46 | protected void serializeContents(JSONArray value, JsonGenerator g, SerializerProvider provider) 47 | throws IOException 48 | { 49 | for (int i = 0, len = value.length(); i < len; ++i) { 50 | Object ob = value.opt(i); 51 | if (ob == null || ob == JSONObject.NULL) { 52 | g.writeNull(); 53 | continue; 54 | } 55 | Class cls = ob.getClass(); 56 | if (cls == JSONObject.class) { 57 | JSONObjectSerializer.instance.serialize((JSONObject) ob, g, provider); 58 | } else if (cls == JSONArray.class) { 59 | serialize((JSONArray) ob, g, provider); 60 | } else if (cls == String.class) { 61 | g.writeString((String) ob); 62 | } else if (cls == Integer.class) { 63 | g.writeNumber(((Integer) ob).intValue()); 64 | } else if (cls == Long.class) { 65 | g.writeNumber(((Long) ob).longValue()); 66 | } else if (cls == Boolean.class) { 67 | g.writeBoolean(((Boolean) ob).booleanValue()); 68 | } else if (cls == Double.class) { 69 | g.writeNumber(((Double) ob).doubleValue()); 70 | } else if (JSONObject.class.isAssignableFrom(cls)) { // sub-class 71 | JSONObjectSerializer.instance.serialize((JSONObject) ob, g, provider); 72 | } else if (JSONArray.class.isAssignableFrom(cls)) { // sub-class 73 | serialize((JSONArray) ob, g, provider); 74 | } else { 75 | provider.writeValue(g, ob); 76 | } 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/fasterxml/jackson/datatype/jsonorg/JSONBaseSerializer.java: -------------------------------------------------------------------------------- 1 | package com.fasterxml.jackson.datatype.jsonorg; 2 | 3 | import com.fasterxml.jackson.databind.ser.std.StdSerializer; 4 | 5 | abstract class JSONBaseSerializer extends StdSerializer 6 | { 7 | private static final long serialVersionUID = 1L; 8 | 9 | protected JSONBaseSerializer(Class cls) { super(cls); } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/fasterxml/jackson/datatype/jsonorg/JSONObjectDeserializer.java: -------------------------------------------------------------------------------- 1 | package com.fasterxml.jackson.datatype.jsonorg; 2 | 3 | import java.io.IOException; 4 | 5 | import com.fasterxml.jackson.core.*; 6 | 7 | import com.fasterxml.jackson.databind.*; 8 | import com.fasterxml.jackson.databind.deser.std.StdDeserializer; 9 | 10 | import org.json.JSONException; 11 | import org.json.JSONObject; 12 | 13 | public class JSONObjectDeserializer extends StdDeserializer 14 | { 15 | public final static JSONObjectDeserializer instance = new JSONObjectDeserializer(); 16 | 17 | public JSONObjectDeserializer() 18 | { 19 | super(JSONObject.class); 20 | } 21 | 22 | @Override 23 | public JSONObject deserialize(JsonParser p, DeserializationContext ctxt) 24 | throws IOException 25 | { 26 | JSONObject ob = new JSONObject(); 27 | JsonToken t = p.currentToken(); 28 | if (t == JsonToken.START_OBJECT) { 29 | t = p.nextToken(); 30 | } 31 | for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) { 32 | String fieldName = p.currentName(); 33 | t = p.nextToken(); 34 | try { 35 | switch (t) { 36 | case START_ARRAY: 37 | ob.put(fieldName, JSONArrayDeserializer.instance.deserialize(p, ctxt)); 38 | continue; 39 | case START_OBJECT: 40 | ob.put(fieldName, deserialize(p, ctxt)); 41 | continue; 42 | case VALUE_STRING: 43 | ob.put(fieldName, p.getText()); 44 | continue; 45 | case VALUE_NULL: 46 | ob.put(fieldName, JSONObject.NULL); 47 | continue; 48 | case VALUE_TRUE: 49 | ob.put(fieldName, Boolean.TRUE); 50 | continue; 51 | case VALUE_FALSE: 52 | ob.put(fieldName, Boolean.FALSE); 53 | continue; 54 | case VALUE_NUMBER_INT: 55 | ob.put(fieldName, p.getNumberValue()); 56 | continue; 57 | case VALUE_NUMBER_FLOAT: 58 | ob.put(fieldName, p.getNumberValue()); 59 | continue; 60 | case VALUE_EMBEDDED_OBJECT: 61 | ob.put(fieldName, p.getEmbeddedObject()); 62 | continue; 63 | default: 64 | } 65 | } catch (JSONException e) { 66 | throw ctxt.instantiationException(handledType(), e); 67 | } 68 | return (JSONObject) ctxt.handleUnexpectedToken(getValueType(ctxt), p); 69 | } 70 | return ob; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/fasterxml/jackson/datatype/jsonorg/JSONObjectSerializer.java: -------------------------------------------------------------------------------- 1 | package com.fasterxml.jackson.datatype.jsonorg; 2 | 3 | import java.io.IOException; 4 | import java.util.Iterator; 5 | 6 | import com.fasterxml.jackson.core.*; 7 | import com.fasterxml.jackson.core.type.WritableTypeId; 8 | import com.fasterxml.jackson.databind.*; 9 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer; 10 | 11 | import org.json.*; 12 | 13 | public class JSONObjectSerializer extends JSONBaseSerializer 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 | --------------------------------------------------------------------------------