Replace tabs with spaces

master
Stuart Boston 10 years ago
parent d8e56616e8
commit e95b76c44a

10
.gitattributes vendored

@ -10,13 +10,13 @@
*.dbproj merge=union *.dbproj merge=union
# Standard to msysgit # Standard to msysgit
*.doc diff=astextplain *.doc diff=astextplain
*.DOC diff=astextplain *.DOC diff=astextplain
*.docx diff=astextplain *.docx diff=astextplain
*.DOCX diff=astextplain *.DOCX diff=astextplain
*.dot diff=astextplain *.dot diff=astextplain
*.DOT diff=astextplain *.DOT diff=astextplain
*.pdf diff=astextplain *.pdf diff=astextplain
*.PDF diff=astextplain *.PDF diff=astextplain
*.rtf diff=astextplain *.rtf diff=astextplain
*.RTF diff=astextplain *.RTF diff=astextplain

@ -8,5 +8,6 @@ import java.lang.annotation.Target;
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface JsonProperty { public @interface JsonProperty {
String value() default "";
String value() default "";
} }

@ -8,5 +8,5 @@ import java.lang.annotation.Target;
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface JsonRootName { public @interface JsonRootName {
String value() default ""; String value() default "";
} }

@ -11,59 +11,56 @@ import org.json.JSONObject;
public class ObjectMapper { public class ObjectMapper {
/** /**
* This takes a JSON string and creates (and populates) an object of the given class * This takes a JSON string and creates (and populates) an object of the
* with the data from that JSON string. It mimics the method signature of the jackson * given class with the data from that JSON string. It mimics the method
* JSON API, so that we don't have to import the jackson library into this application. * 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. * @param jsonString The JSON string to parse.
* @return The instantiation of that class, populated with data from the JSON object. * @param objClass The class of object we want to create.
* @throws IOException If there was any kind of issue. * @return The instantiation of that class, populated with data from the
*/ * JSON object.
public <T> T readValue(String jsonString, Class<T> objClass) throws IOException { * @throws IOException If there was any kind of issue.
try { */
return readValue(new JSONObject(jsonString), objClass); public <T> T readValue(String jsonString, Class<T> objClass) throws IOException {
} try {
catch (IOException ioe) { return readValue(new JSONObject(jsonString), objClass);
throw ioe; } catch (IOException ioe) {
} throw ioe;
catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new IOException(e); throw new IOException(e);
} }
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T, R> T readValue(JSONObject json, Class<T> objClass) throws IOException { public <T, R> T readValue(JSONObject json, Class<T> objClass) throws IOException {
try { try {
//TODO Iterate through json object values and call the JsonAnySetter method on the unknown ones. //TODO Iterate through json object values and call the JsonAnySetter method on the unknown ones.
T obj = objClass.newInstance(); T obj = objClass.newInstance();
for (Field f : objClass.getFields()) { for (Field f : objClass.getFields()) {
Annotation a = f.getAnnotation(JsonProperty.class); Annotation a = f.getAnnotation(JsonProperty.class);
if (List.class.equals(f.getType()) && (json.optJSONArray(((JsonProperty) a).value()) != null)) { // It's a list. if (List.class.equals(f.getType()) && (json.optJSONArray(((JsonProperty) a).value()) != null)) { // It's a list.
JSONArray jsonArray = json.optJSONArray(((JsonProperty) a).value()); JSONArray jsonArray = json.optJSONArray(((JsonProperty) a).value());
ParameterizedType listType = (ParameterizedType) f.getGenericType(); ParameterizedType listType = (ParameterizedType) f.getGenericType();
Class<?> subObj = (Class<?>) listType.getActualTypeArguments()[0]; Class<?> subObj = (Class<?>) listType.getActualTypeArguments()[0];
List<R> subObjList = ((Class<List<R>>) f.getType()).newInstance(); List<R> subObjList = ((Class<List<R>>) f.getType()).newInstance();
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
subObjList.add((R) readValue(jsonArray.getJSONObject(i), subObj)); subObjList.add((R) readValue(jsonArray.getJSONObject(i), subObj));
} }
f.set(obj, subObjList); f.set(obj, subObjList);
} } else if (a != null) {
else if (a != null) { f.set(obj, json.opt(((JsonProperty) a).value()));
f.set(obj, json.opt(((JsonProperty) a).value())); }
} }
} return obj;
return obj; } catch (IOException ioe) {
} throw ioe;
catch (IOException ioe) { } catch (Exception e) {
throw ioe; e.printStackTrace();
} throw new IOException(e);
catch (Exception e) { }
e.printStackTrace(); }
throw new IOException(e);
}
}
} }
Loading…
Cancel
Save