Converters and Transients Grails JSON

Using Grail 1.3.7 I found that the JSON converter ignores the transient properties of Domain objects. Question: Is there an elegant way to get around this obstacle.

Bonus question: why are arguments for the exclusion of calculated fields (transition details) sent to the answer ????

+4
source share
5 answers

one way would be to manually create your json response, for example.

["prop1" : obj.prop1, "prop2" : obj.prop2, ...] as JSON 
+4
source

Transient is done just for this: Variables can be marked as transient to indicate that they are not part of the constant state of the object

And JSON is the serialized (= persistent) state of the object

So, if you need it to be serialized - you need to create a new class, only for json serialization, which will have all the fields necessary for serialization.

+5
source

what works for me is one line

 def jsonobj=domobj.properties as JSON 
+4
source

If you need small-scale control over fields that are included / excluded in JSON, I better use JSONBuilder better than a converter. Here is an example of how to do this.

0
source

You can use the "marshallers" plugin and define the transient property as virtual:

  static marshalling = { virtual { yourPropery { value, json -> json.value(value.yourPropery) } } } 
0
source

All Articles