Problem parsing json objects with oatpp when multiple objects json objects are defined

  Kiến thức lập trình

I’m new to OATPP and to some extent CPP so be gentle and apologies in advance if this is a stupid question/problem but no amount of googling/reading etc seems to give me any pointers on how to solve it.

I need to parse a simple json structure, the structure is as follows:

{
  "service": "service1",
  "backend": "backend1",
  "backendParams": {
                     "param1": "1",
                     "param2": "2"
   }
}

The small oatpp application I’ve written, more as a POC uses the backend parameter to call an external service passing the backendParams as a json “string”, in theory it will be deserialised further down the chain.

I don’t seem to be able to work out how to construct the dto for that type of json structure, everything seems to be key-value pairs from what I’ve seen and it doesn’t matter what I seem to do I just get a parsing error from oatpp, unless I only pass a single key-value pair in backendParams

My DTO definitions are as follows:

class BackendParamsDto : public oatpp::DTO {
  DTO_INIT(BackendParamsDto, DTO)
  DTO_FIELD(String, param1);
  DTO_FIELD(String, param2);
};

class BodyDto : public oatpp::DTO {
  DTO_INIT(BodyDto, DTO)

  DTO_FIELD_INFO(service) {
    info->required = true;
  }

  DTO_FIELD_INFO(backend) {
    info->required = true;
  }

  DTO_FIELD_INFO(backendParams) {
    info->required = true;
  }

  DTO_FIELD(String, service);
  DTO_FIELD(String, backend);
  DTO_FIELD(String, backendParams);
};

I tried defining backendParams as DTO_FIELD(Fields<List<Object<BackendParamsDto>>>, backendParams); (it was a suggestion on stackoverflow when someone asked something similar) but that didn’t work either.

I know its something silly I’m either doing or not doing but going through anything I’ve found on here and other oatpp related discussions I don’t seem to find anything that is helping.

I just need anything/everything that is passed in backendParams to end up in a string, obviously in json format as it will be passed to the backed system. Ideally if I can validate the key-value pairs thats a bonus but I just can’t get beyond this point at the moment.

If anyone has any pointers or if there are any online examples etc that would be great as I’m completely stumped at the moment.

Thanks

New contributor

Brian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT