Coffee to go

Icon

var thisBlog = getGlobalContext().buildFromTemplate(PERSONAL | PROGRAMMING | RANDOM_THOUGHTS);

Changing field name before serialized json output using flexjson & java

Today I was working on a Web API integration project when I ran into this problem. All data being sent and received from this Web API is in JSON format and for which we are flexjson, which works good for serializing and deserializing POJO to/from json.

By default flexjson uses the POJO property names as the field name in serialized json output, so the output of this POJO:

//Person.java
public class Person {
	String firstName;

    public String getName() { return firstName;}
    public void setName(String firstName) { this.firstName = firstName; }
}

will be:

{"firstName":"Abdul Kareem"}

But what if I can’t change POJOs but still would like the serialized output to be something like this:

{"first_name":"Abdul Kareem"}

This was the problem which I ran into today, overriding the default flexjson behaviour and provide the field names manually. It wasn’t that difficult as I thought, few minutes into mailing list search led me to official flexjson tutorial page on sourceforge. There they have written about using Custom Transformer in great detail using example.

So in short, in order to play with field name’s during serialization flexjson has provided an abstract class AbstractTransformer, writting custom transformer is as easy as extending this class and overriding void transform(Object object) method.

Pasted below is the code of FieldNameTransformer class which I wrote for solving the problem I mentioned earlier. It allows user to specify the field name rather then using default POJO property names in json serialized output.

public class FieldNameTransformer extends AbstractTransformer {
	private String transformedFieldName;

	public FieldNameTransformer(String transformedFieldName) {
		this.transformedFieldName = transformedFieldName;
	}

	/***
	 * Use this method to override the default functionality
	 * of handling field names and their values
	 */
	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;
	}
}

And this is how to use it:

Person person = new Person();
person.setFirstName("Abdul Kareem");

JSONSerializer serializer = new JSONSerializer().exclude("class")
                .transform(new StringTransformer("first_name"), "firstName");
String jsonString = serializer.serialize(person);

And here is the desired output:

{"first_name":"Abdul Kareem"}
Share on Twitter

Category: Uncategorized

Tagged: , ,

One Response

  1. Vitamin B says:

    Very interesting info!Perfect just what I was searching for!

Leave a Reply