Skip to content

Instantly share code, notes, and snippets.

@akshaydashrath
Created March 6, 2012 14:50
Show Gist options
  • Save akshaydashrath/1986647 to your computer and use it in GitHub Desktop.
Save akshaydashrath/1986647 to your computer and use it in GitHub Desktop.
Android CursorTransformer for flexjson
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();
}
}
@chubbard
Copy link

chubbard commented Mar 6, 2012

That's pretty cool. I'll have to create a link back here from the flexjson project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment