Browse files

Made JSONSerializer injection-enabled.

  • Loading branch information...
1 parent e5cda81 commit ab0ea65d4ba1bc13df8c6a32b7055d2e028ef497 @yanchenko committed Oct 13, 2012
View
20 base/src/org/droidparts/persist/json/JSONSerializer.java
@@ -45,6 +45,7 @@
import java.util.Collection;
import java.util.Date;
+import org.droidparts.inject.Injector;
import org.droidparts.model.Model;
import org.droidparts.reflect.ann.FieldSpec;
import org.droidparts.reflect.ann.json.KeyAnn;
@@ -54,6 +55,7 @@
import org.json.JSONException;
import org.json.JSONObject;
+import android.content.Context;
import android.util.Log;
import android.util.Pair;
@@ -63,13 +65,16 @@
public static final String __ = "->" + (char) 29;
public static <ModelType extends Model> JSONSerializer<ModelType> getInstance(
- Class<ModelType> cls) {
- return new JSONSerializer<ModelType>(cls);
+ Context ctx, Class<ModelType> cls) {
+ return new JSONSerializer<ModelType>(ctx, cls);
}
+ private final Context ctx;
private final Class<? extends Model> cls;
- public JSONSerializer(Class<ModelType> cls) {
+ public JSONSerializer(Context ctx, Class<ModelType> cls) {
+ Injector.get().inject(ctx, this);
+ this.ctx = ctx.getApplicationContext();
this.cls = cls;
}
@@ -139,7 +144,7 @@ protected void putToJSONObject(JSONObject obj, String key,
} else if (isByteArray(valType)) {
obj.put(key, val);
} else if (isModel(valType)) {
- JSONObject obj2 = getInstance(dirtyCast(valType)).serialize(
+ JSONObject obj2 = getInstance(ctx, dirtyCast(valType)).serialize(
(Model) val);
obj.put(key, obj2);
} else if (isArray(valType) || isCollection(valType)) {
@@ -155,7 +160,8 @@ protected void putToJSONObject(JSONObject obj, String key,
if (list.size() > 0) {
Class<?> itemCls = list.get(0).getClass();
if (isModel(itemCls)) {
- JSONSerializer serializer = getInstance(dirtyCast(itemCls));
+ JSONSerializer serializer = getInstance(ctx,
+ dirtyCast(itemCls));
jArr = serializer.serialize(list);
} else {
for (Object o : list) {
@@ -190,7 +196,7 @@ protected Object readFromJSON(Field field, Class<?> multiFieldArgType,
if (isByteArray(fieldType)) {
return jsonVal;
} else if (isModel(fieldType)) {
- return getInstance(dirtyCast(fieldType)).deserialize(
+ return getInstance(ctx, dirtyCast(fieldType)).deserialize(
(JSONObject) jsonVal);
} else if (isArray(fieldType) || isCollection(fieldType)) {
JSONArray jArr = (jsonVal instanceof JSONArray) ? (JSONArray) jsonVal
@@ -205,7 +211,7 @@ protected Object readFromJSON(Field field, Class<?> multiFieldArgType,
}
JSONSerializer serializer = null;
if (isModel(multiFieldArgType)) {
- serializer = getInstance(dirtyCast(multiFieldArgType));
+ serializer = getInstance(ctx, dirtyCast(multiFieldArgType));
}
for (int i = 0; i < jArr.length(); i++) {
Object obj = jArr.get(i);
View
6 sample/DroidPartsGram/src/org/droidparts/gram/persist/ImageSerializer.java
@@ -22,10 +22,12 @@
import org.json.JSONException;
import org.json.JSONObject;
+import android.content.Context;
+
public class ImageSerializer extends JSONSerializer<Image> {
- public ImageSerializer() {
- super(Image.class);
+ public ImageSerializer(Context ctx) {
+ super(ctx, Image.class);
}
@Override
View
2 sample/DroidPartsGram/src/org/droidparts/gram/service/ImageIntentService.java
@@ -69,7 +69,7 @@ protected Bundle execute(String action, Bundle data) throws Exception {
if (ACTION_REFRESH.equals(action)) {
JSONObject obj = restClient.getJSONObject(refreshUri.toString());
JSONArray arr = obj.getJSONArray("data");
- ArrayList<Image> list = new ImageSerializer().deserialize(arr);
+ ArrayList<Image> list = new ImageSerializer(this).deserialize(arr);
imageEntityManager.delete().execute();
imageEntityManager.create(list);
return data;
View
2 sample/droidparts-sample/src/org/droidparts/sample/DependencyProvider.java
@@ -16,7 +16,7 @@
public DependencyProvider(Context ctx) {
super(ctx);
dbOpenHelper = new DBOpenHelper(ctx);
- entrySerializer = new EntrySerializer();
+ entrySerializer = new EntrySerializer(ctx);
}
@Override
View
6 sample/droidparts-sample/src/org/droidparts/sample/json/EntrySerializer.java
@@ -3,10 +3,12 @@
import org.droidparts.persist.json.JSONSerializer;
import org.droidparts.sample.model.Entry;
+import android.content.Context;
+
public class EntrySerializer extends JSONSerializer<Entry> {
- public EntrySerializer() {
- super(Entry.class);
+ public EntrySerializer(Context ctx) {
+ super(ctx, Entry.class);
}
}
View
6 test/src/org/droidparts/test/persist/json/AlbumSerializer.java
@@ -3,10 +3,12 @@
import org.droidparts.persist.json.JSONSerializer;
import org.droidparts.test.model.Album;
+import android.content.Context;
+
public class AlbumSerializer extends JSONSerializer<Album> {
- public AlbumSerializer() {
- super(Album.class);
+ public AlbumSerializer(Context ctx) {
+ super(ctx, Album.class);
}
}
View
6 test/src/org/droidparts/test/testcase/JSONTestCase.java
@@ -21,7 +21,7 @@
public void testPrimitives() throws Exception {
JSONSerializer<Primitives> serializer = new JSONSerializer<Primitives>(
- Primitives.class);
+ getContext(), Primitives.class);
Primitives primitives = serializer.deserialize(getPrimitives());
assertNotNull(primitives.strArr);
//
@@ -41,7 +41,7 @@ public void testPrimitives() throws Exception {
}
public void testAlbums() throws Exception {
- AlbumSerializer serializer = new AlbumSerializer();
+ AlbumSerializer serializer = new AlbumSerializer(getContext());
ArrayList<Album> albums = serializer.deserialize(getAlbums());
assertEquals(2, albums.size());
assertEquals("Diamond", albums.get(0).name);
@@ -51,7 +51,7 @@ public void testAlbums() throws Exception {
public void testNestedKeys() throws Exception {
assertEquals("obj->key", join(new String[] { "obj", "key" }, __, null));
JSONSerializer<Nested> serializer = new JSONSerializer<Nested>(
- Nested.class);
+ getContext(), Nested.class);
Nested model = serializer.deserialize(getNested());
assertEquals("str", model.str);
JSONObject obj = serializer.serialize(model);

0 comments on commit ab0ea65

Please sign in to comment.