0

I have complex object and I want to render it, but I have several problems in view.

First of all, I have UUID field in my class but in View I get not the String but mostSigBits and leastSigBits. The second one, I have my enums fields like two fields with enum and value

For example,

public class ExampleObject   {
  @JsonProperty("id")
  private UUID id;

  @JsonProperty("name")
  private String name;

  @JsonProperty("address")
  private String address;

  @JsonProperty("port")
  private String port;

  @JsonProperty("users")
  @Valid
  private List<UserRef> users = null;

  @JsonProperty("indexingParameters")
  private IndexingParameters indexingParameters;

  @JsonProperty("otherParameters")
  private OtherParameters otherParameters;

  @JsonProperty("status")
  private Status status;
}

When I get response from controller I get answer with this one

{
   "id": {
        "leastSignificantBits": -5406850341911646206,
        "mostSignificantBits": 8884977146336383467
    },
   "status": {
        "enumType": "api.model.Status",
        "name": "GENERAL"
    }
....
}

The problem is I have a lot of different but with the same problem objects in my code. If there is only 1 object, I`d easy prepare some _exampleObject.gson template and render every answer from controller to it, but I have many objects.

I think there are some variants to render correct my JSON, isn`t there?


Another rendering variants where data is ExampleObject.class or something like that

1)code:

  Map map = [content: data.content, sorting: data.sorting, paging: data.paging] as Map
    render AppResponse.success([success: true, data: map]).asJSON()
  
render data as JSON

on front:

Incorrect UUID and DateTime convert each field in Object, But I need Timeshtamp

   "id": {"leastSignificantBits": -5005002633583312101,
          "mostSignificantBits": 4056748206401340307},
   "key": "b48d35dd-0551-4265-a1b1-65105e713811",

2)code:

Map map =  [data: new ObjectMapper().writeValueAsString(data)] as Map
render map

on front:

Here we can see square brackets at the start  which is wrong for JSON

['data':'{"content":[{"id":"384c7700-09c1-4393-ba8a-a89f555f431b","name":"somename"... 

3)code:

Object result = new HashMap<String, Object>()

result.success = true
result["data1"] = new ObjectMapper().writeValueAsString(data)

render result as JSON

on front:

Here we can see quotes escaping

"data": "{\"content\":[{\"id\":\"384c7700-09c1-4393-ba8a-a89f555f431b\",\"name\":\"somename\",\"key\":\"b48d35dd-0551-4265-a1b1-65105e713811\",\"status\":\"COMPLETED\.......
Dred
  • 1,076
  • 8
  • 24
  • Have you tried mapping the response without gson? You can fill a Map to store the data as a JSON and return it. – luisenricke Oct 19 '21 at 06:16
  • I did, but i got not correct JSON. If i send to fron only Map like `[name: myname, id: myId] as Map`, so it will be correct, but if I try `render [result: myObject] as Map` where "myObject" is example of MyObject.class i get wrong rendering – Dred Oct 20 '21 at 10:31
  • Try to save the data into variable before send the respond and parse it with JSON object, somthing like that `def test = [test: object.test] as JSON; respond test`. Check if the respond runs. Check this question about that https://stackoverflow.com/questions/22601188/grails-controller-rendering-method-render-vs-respond] – luisenricke Oct 20 '21 at 21:01
  • I added 3 examples of my tries. Problem is in that, I got that object from rest request from another endpoin, convert LazyMap to my ExampleObject do some logic and have to send my object to front. ExampleObject is not domain object – Dred Oct 21 '21 at 00:24

1 Answers1

0

I did it like this

@CompileStatic
class MyProxyController {


@Autowired
Myservice service

static ObjectMapper objectMapper = new ObjectMapper()
          .registerModule(new JodaModule())
          .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
          .configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false)
          .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)


def getExampleObject {
  ExampleObject exampleObject = service.getExampleObject()

  render([contentType: "application/json"], objectMapper.writeValueAsString(new CustomObject(data, true)))

}

  @CompileStatic
  class CustomObject {
    Boolean success
    Object data

    CustomObject(Object data, Boolean success) {
      this.data = data
      this.success = success
    }
  }

}

And get json as I wanted like

{
    "success": true,
    "data": {
        "content": [
            { ....
Dred
  • 1,076
  • 8
  • 24