Created
March 6, 2012 14:50
-
-
Save akshaydashrath/1986647 to your computer and use it in GitHub Desktop.
Android CursorTransformer for flexjson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.database.Cursor; | |
import flexjson.JSONContext; | |
import flexjson.Path; | |
import flexjson.TypeContext; | |
import flexjson.transformer.AbstractTransformer; | |
import flexjson.transformer.TransformerWrapper; | |
public class CursorTransformer extends AbstractTransformer { | |
public void transform(Object object) { | |
JSONContext context = getContext(); | |
Path path = context.getPath(); | |
Cursor value = (Cursor) object; | |
TypeContext typeArrayContext = getContext().writeOpenArray(); | |
while (value.moveToNext()) { | |
if (!typeArrayContext.isFirst()){ | |
getContext().writeComma(); | |
} else { | |
typeArrayContext.setFirst(false); | |
} | |
TypeContext typeContext = getContext().writeOpenObject(); | |
for (String key : value.getColumnNames()) { | |
path.enqueue(key != null ? key : null); | |
if ((context.isIncluded(key != null ? key.toString() | |
: null, value.getString(value | |
.getColumnIndex(key))))) { | |
TransformerWrapper transformer = (TransformerWrapper) context | |
.getTransformer(value.getString(value | |
.getColumnIndex(key))); | |
if (!transformer.isInline()) { | |
if (!typeContext.isFirst()) | |
getContext().writeComma(); | |
typeContext.setFirst(false); | |
if (key != null) { | |
getContext().writeName(key.toString()); | |
} else { | |
getContext().writeName(null); | |
} | |
} | |
if (key != null) { | |
typeContext.setPropertyName(key.toString()); | |
} else { | |
typeContext.setPropertyName(null); | |
} | |
transformer.transform(value.getString(value | |
.getColumnIndex(key))); | |
} | |
path.pop(); | |
} | |
getContext().writeCloseObject(); | |
} | |
getContext().writeCloseArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's pretty cool. I'll have to create a link back here from the flexjson project.