Added example Jackson Replacement code

This commit is contained in:
Stuart Boston
2013-02-25 08:41:24 +00:00
parent 160b478c11
commit 40567c3059
5 changed files with 111 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
package com.darylbeattie.movies.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value=ElementType.METHOD)
@Retention(value=RetentionPolicy.RUNTIME)
public @interface JsonAnySetter {
}
+12
View File
@@ -0,0 +1,12 @@
package com.darylbeattie.movies.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonProperty {
String value() default "";
}
+12
View File
@@ -0,0 +1,12 @@
package com.darylbeattie.movies.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonRootName {
String value() default "";
}
+69
View File
@@ -0,0 +1,69 @@
package com.darylbeattie.movies.util;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class ObjectMapper {
/**
* This takes a JSON string and creates (and populates) an object of the given class
* with the data from that JSON string. It mimics the method signature of the jackson
* JSON API, so that we don't have to import the jackson library into this application.
*
* @param jsonString The JSON string to parse.
* @param objClass The class of object we want to create.
* @return The instantiation of that class, populated with data from the JSON object.
* @throws IOException If there was any kind of issue.
*/
public <T> T readValue(String jsonString, Class<T> objClass) throws IOException {
try {
return readValue(new JSONObject(jsonString), objClass);
}
catch (IOException ioe) {
throw ioe;
}
catch (Exception e) {
e.printStackTrace();
throw new IOException(e);
}
}
@SuppressWarnings("unchecked")
public <T, R> T readValue(JSONObject json, Class<T> objClass) throws IOException {
try {
//TODO Iterate through json object values and call the JsonAnySetter method on the unknown ones.
T obj = objClass.newInstance();
for (Field f : objClass.getFields()) {
Annotation a = f.getAnnotation(JsonProperty.class);
if (List.class.equals(f.getType()) && (json.optJSONArray(((JsonProperty) a).value()) != null)) { // It's a list.
JSONArray jsonArray = json.optJSONArray(((JsonProperty) a).value());
ParameterizedType listType = (ParameterizedType) f.getGenericType();
Class<?> subObj = (Class<?>) listType.getActualTypeArguments()[0];
List<R> subObjList = ((Class<List<R>>) f.getType()).newInstance();
for (int i = 0; i < jsonArray.length(); i++) {
subObjList.add((R) readValue(jsonArray.getJSONObject(i), subObj));
}
f.set(obj, subObjList);
}
else if (a != null) {
f.set(obj, json.opt(((JsonProperty) a).value()));
}
}
return obj;
}
catch (IOException ioe) {
throw ioe;
}
catch (Exception e) {
e.printStackTrace();
throw new IOException(e);
}
}
}
+6
View File
@@ -0,0 +1,6 @@
Jackson Library Replacement
===========================
These files are provided by Darren Beattie as an example of how to replace the Jackson libraries with native libraries inside Android.
They are provided without warrantee and if you modify them or find them useful, please let me know.