Skip to content

Commit

Permalink
Initial commit of datatype binder functionality for net.sf.json-lib a…
Browse files Browse the repository at this point in the history
…nd Jackson.
  • Loading branch information
swquinn committed Nov 4, 2013
1 parent 7c84b18 commit bffcb12
Show file tree
Hide file tree
Showing 13 changed files with 697 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.settings
/.classpath
/.project
/target
90 changes: 90 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<project xmlns="http:https://maven.apache.org/POM/4.0.0"
xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.fasterxml</groupId>
<artifactId>oss-parent</artifactId>
<version>11</version>
</parent>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-json-lib</artifactId>
<name>Jackson-datatype-json-lib</name>
<version>2.2.3</version>
<description>Support for datatypes of "net.sf.json" JSON library (see http:https://json-lib.sourceforge.net),
mainly to make it easier to upgrade code to Jackson, using automated conversions.
</description>
<url>http:https://wiki.fasterxml.com/JacksonModuleProjects</url>
<packaging>jar</packaging>
<scm>
<connection>scm:git:[email protected]:swquinn/jackson-datatype-json-lib.git</connection>
<developerConnection>scm:git:[email protected]:swquinn/jackson-datatype-json-lib.git</developerConnection>
<url>https://github.com/swquinn/jackson-datatype-json-lib</url>
<tag>jackson-datatype-json-lib-2.2.3</tag>
</scm>
<properties>
<version.jackson>2.2.3</version.jackson>
<!-- Generate PackageVersion.java into this directory. -->
<packageVersion.dir>com/fasterxml/jackson/datatype/jsonlib</packageVersion.dir>
<packageVersion.package>${project.groupId}.jsonlib</packageVersion.package>
<osgi.import>com.fasterxml.jackson.core
,com.fasterxml.jackson.core.util
,com.fasterxml.jackson.databind
,com.fasterxml.jackson.databind.deser
,com.fasterxml.jackson.databind.deser.std
,com.fasterxml.jackson.databind.jsontype
,com.fasterxml.jackson.databind.module
,com.fasterxml.jackson.databind.node
,com.fasterxml.jackson.databind.ser
,com.fasterxml.jackson.databind.ser.std
,net.sf.json
</osgi.import>
<osgi.export>${project.groupId}.jsonlib.*;version=${project.version}</osgi.export>
</properties>

<dependencies>
<!-- Extends Jackson mapper, 2.x -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${version.jackson}</version>
</dependency>
<!-- And support net.sf.json-lib/java types -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>

<!-- and for testing, JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<executions>
<execution>
<id>process-packageVersion</id>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
4 changes: 4 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Project: jackson-datatype-json-lib
Version: 2.2.3 (03-Nov-2013)

Official 2.0 compatible version against 2.2.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.fasterxml.jackson.datatype.jsonlib;

import java.io.IOException;

import net.sf.json.JSONArray;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

/**
* A datatype deserializer for the JSON-Lib {@link JSONArray} object.
* @author Sean.Quinn
* @since 1.0
*/
public class JSONArrayDeserializer extends StdDeserializer<JSONArray>
{
private static final long serialVersionUID = 1L;

public final static JSONArrayDeserializer instance = new JSONArrayDeserializer();

public JSONArrayDeserializer()
{
super(JSONArray.class);
}

@Override
public JSONArray deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JSONArray array = new JSONArray();
JsonToken t;
while ((t = jp.nextToken()) != JsonToken.END_ARRAY) {
switch (t) {
case START_ARRAY:
array.add(deserialize(jp, ctxt));
continue;
case START_OBJECT:
array.add(JSONObjectDeserializer.instance.deserialize(jp, ctxt));
continue;
case VALUE_STRING:
array.add(jp.getText());
continue;
case VALUE_NULL:
array.add(null);
continue;
case VALUE_TRUE:
array.add(Boolean.TRUE);
continue;
case VALUE_FALSE:
array.add(Boolean.FALSE);
continue;
case VALUE_NUMBER_INT:
array.add(jp.getNumberValue());
continue;
case VALUE_NUMBER_FLOAT:
array.add(jp.getNumberValue());
continue;
case VALUE_EMBEDDED_OBJECT:
array.add(jp.getEmbeddedObject());
continue;
default:
throw ctxt.mappingException("Unrecognized or unsupported JsonToken type: "+t);
}
}
return array;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.fasterxml.jackson.datatype.jsonlib;

import java.io.IOException;
import java.lang.reflect.Type;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;

public class JSONArraySerializer extends JSONBaseSerializer<JSONArray>
{
public final static JSONArraySerializer instance = new JSONArraySerializer();

public JSONArraySerializer()
{
super(JSONArray.class);
}

@Override
public void serialize(JSONArray value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException
{
jgen.writeStartArray();
serializeContents(value, jgen, provider);
jgen.writeEndArray();
}

@Override
public void serializeWithType(JSONArray value, JsonGenerator jgen, SerializerProvider provider,
TypeSerializer typeSer)
throws IOException, JsonGenerationException
{
typeSer.writeTypePrefixForArray(value, jgen);
serializeContents(value, jgen, provider);
typeSer.writeTypeSuffixForArray(value, jgen);
}

@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint)
throws JsonMappingException
{
return createSchemaNode("array", true);
}

protected void serializeContents(JSONArray value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException
{
for (int i = 0, len = value.size(); i < len; ++i) {
Object ob = value.opt(i);
if (ob == null) {
jgen.writeNull();
continue;
}
Class<?> cls = ob.getClass();
if (cls == JSONObject.class) {
JSONObjectSerializer.instance.serialize((JSONObject) ob, jgen, provider);
} else if (cls == JSONArray.class) {
serialize((JSONArray) ob, jgen, provider);
} else if (cls == String.class) {
jgen.writeString((String) ob);
} else if (cls == Integer.class) {
jgen.writeNumber(((Integer) ob).intValue());
} else if (cls == Long.class) {
jgen.writeNumber(((Long) ob).longValue());
} else if (cls == Boolean.class) {
jgen.writeBoolean(((Boolean) ob).booleanValue());
} else if (cls == Double.class) {
jgen.writeNumber(((Double) ob).doubleValue());
} else if (JSONObject.class.isAssignableFrom(cls)) { // sub-class
JSONObjectSerializer.instance.serialize((JSONObject) ob, jgen, provider);
} else if (JSONArray.class.isAssignableFrom(cls)) { // sub-class
serialize((JSONArray) ob, jgen, provider);
} else if (JSONArray.class.isAssignableFrom(cls)) { // sub-class
JSONArraySerializer.instance.serialize((JSONArray) ob, jgen, provider);
} else {
provider.defaultSerializeValue(ob, jgen);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.fasterxml.jackson.datatype.jsonlib;

import com.fasterxml.jackson.databind.ser.std.StdSerializer;

abstract class JSONBaseSerializer<T> extends StdSerializer<T>
{
protected JSONBaseSerializer(Class<T> cls) { super(cls); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.fasterxml.jackson.datatype.jsonlib;

import java.io.IOException;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

public class JSONObjectDeserializer extends StdDeserializer<JSONObject>
{
private static final long serialVersionUID = 1L;

public final static JSONObjectDeserializer instance = new JSONObjectDeserializer();

public JSONObjectDeserializer()
{
super(JSONObject.class);
}

@Override
public JSONObject deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
JSONObject ob = new JSONObject();
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.START_OBJECT) {
t = jp.nextToken();
}
for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) {
String fieldName = jp.getCurrentName();
t = jp.nextToken();
try {
switch (t) {
case START_ARRAY:
ob.put(fieldName, JSONArrayDeserializer.instance.deserialize(jp, ctxt));
continue;
case START_OBJECT:
ob.put(fieldName, deserialize(jp, ctxt));
continue;
case VALUE_STRING:
ob.put(fieldName, jp.getText());
continue;
case VALUE_NULL:
ob.put(fieldName, null);
continue;
case VALUE_TRUE:
ob.put(fieldName, Boolean.TRUE);
continue;
case VALUE_FALSE:
ob.put(fieldName, Boolean.FALSE);
continue;
case VALUE_NUMBER_INT:
ob.put(fieldName, jp.getNumberValue());
continue;
case VALUE_NUMBER_FLOAT:
ob.put(fieldName, jp.getNumberValue());
continue;
case VALUE_EMBEDDED_OBJECT:
ob.put(fieldName, jp.getEmbeddedObject());
continue;
default:
}
} catch (JSONException e) {
throw ctxt.mappingException("Failed to construct JSONObject: "+e.getMessage());
}
throw ctxt.mappingException("Urecognized or unsupported JsonToken type: "+t);
}
return ob;
}
}
Loading

0 comments on commit bffcb12

Please sign in to comment.