Search code examples
jsonserializationplayframeworkflexjson

How to serialize transient fields in the model via FlexJson?


I am using Play Framework to expose REST API, which returns some JSON objects. To simplify the API usage, I would like to return a "calculated" field in the response.

Unfortunately, in my tests, while FlexJson does not ignore the transient model fields completely, but always sets them to 'null'.

More details:

In the model class, I define:

@Transient
public String currencyName;

The only constructor of the class set the value to "dollar" (for debugging purposes):

this.currencyName = "dollar";

When serializing the class using FlexJson, when the 'currencyName' field is not specified in the include/ exclude - the result always looks like:

"currencyName":null

Any idea what got wrong, and how to get the field value serialized into JSON?

Thanks in advance.


Solution

  • By definition if your field is transient it will not be serialized. Perhaps this field should not be transient in your application if the state matters.