2

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.

Max
  • 643
  • 11
  • 27

1 Answers1

3

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.

emt14
  • 4,846
  • 7
  • 37
  • 58
  • Thanks. I am using serialization in order to generate "clean" JSON, not transfer object "state" between apps. My thought was that instead of configuring which fields from "this.currency" to "print out" into JSON, use calculated fields to specify the JSON format directly. Any thought? – Max Feb 27 '12 at 06:45
  • If you are not interested in persisting this field or holding the state why don't you remove this field altogether. You can have a method wich calculates and return this value i.e. String getCurrencyName() – emt14 Feb 27 '12 at 13:27
  • Alternatively use the exclude method from the serializer: new JSONSerializer().exclude("currencyName").serialize("Currency", currency); – emt14 Feb 27 '12 at 14:40
  • Thanks a lot, "emt14". I implemented String getCurrencyName() - worked exactly as you described. – Max Feb 27 '12 at 21:50