I found this bit of code from here: Change property name with Flexjson
It works for String data types but seemingly no others. Any ideas?
public class FieldNameTransformer extends AbstractTransformer {
private String transformedFieldName;
public FieldNameTransformer(String transformedFieldName) {
this.transformedFieldName = transformedFieldName;
}
public void transform(Object object) {
boolean setContext = false;
TypeContext typeContext = getContext().peekTypeContext();
//Write comma before starting to write field name if this
//isn't first property that is being transformed
if (!typeContext.isFirst())
getContext().writeComma();
typeContext.setFirst(false);
getContext().writeName(getTransformedFieldName());
getContext().writeQuoted((String) object);
if (setContext) {
getContext().writeCloseObject();
}
}
/***
* TRUE tells the JSONContext that this class will be handling
* the writing of our property name by itself.
*/
@Override
public Boolean isInline() {
return Boolean.TRUE;
}
public String getTransformedFieldName() {
return this.transformedFieldName;
}
}
This works fine for text fields but gets very unhappy if I try to apply it to a field that is a date. For example:
String JSON = new JSONSerializer()
.include("type", "createDate")
.exclude("*")
.transform(new DateTransformer("MM/dd/yyyy"), Date.class)
.transform(new FieldNameTransformer("new_json_property_name"),"createDate")
.serialize(stuff);
Fails with JSONException occured : Error trying to deepSerialize
It works fine when I comment out the transform new Field... line and it works fine when I try to change the type field, which is a String.
It also fails when trying to modify and Int and I would expect everything else that's not a String.
If someone is familiar with the FlexJSON API and can point me in the right direction here I'd very much appreciate it.