0

Currently our json responses are not formatted at all, we are using the JsonResponse class to return a status code with content:

  public function getMarkets() {
    $response = $this->marketService->getMarkets();

    return new JsonResponse($response, 200);
  }

Is there an easy way to make sure all json responses are formatted as camel case or do we have to make sure the properties are camelcased before passing them to the JsonResponse object?

7
  • What is getMarkets returning? Are you using the Serializer service to format it’s response? See: symfony.com/doc/current/serializer.html
    – Kevin
    Commented Jun 20, 2019 at 2:31
  • getMarkets is just returning an array of objects and we aren't formatting it in the service at all, I was wondering if there was a way to format all outgoing responses without doing it manually for every call Commented Jun 20, 2019 at 12:50
  • Without looking at the code I think JsonResponse ensures the returned HTTP response is proper, but won’t auto convert your data structure the way you want it.
    – Kevin
    Commented Jun 20, 2019 at 13:06
  • what would you say is the best way to handle that then? We were thinking about extending the JsonResponse class and then having it returned a camel case response Commented Jun 20, 2019 at 13:14
  • Drupal convention for properties is to use snake case so if you have a contradictory opinion forcing your hand at API design, then you'll need to redo things (I had to do this in Services in Drupal 7 because someone couldn't handle, opinion-wise, "field_" on properties). I guess, if you're not relying on serialization module, then making your own JsonResponse would be fine. I don't think this is worth it and it would increase the cognitive load for anyone learning the site code base.
    – mradcliffe
    Commented Jun 20, 2019 at 13:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.