0

I have an existing Symfony 3 app. I installed FOSRestBundle, FOSUserBundle, NelmioCorsBundle. For example, I have 2 entity: User and Tariff.

The tariff has id, name, price, description, image, user (OneToMany to User) fields. The User has id, email, created, updated, some one-to-many fields, tariff (ManyToOne to Tariff).

I try to create UserRestController::getUserTariff:

    /**
 * @Rest\Get(path="/users/{id}/tariff", name="get_user_tariff")
 *
 * @param $id
 * @return Response
 */
public function getUserTariff($id)
{
    /** @var User $user */
    $user = $this->getDoctrine()->getRepository(User::class)->find($id);
    $tariff = $this->getDoctrine()->getRepository(Tariff::class)->findBy(['id' => $user->getTariff()->getId()]);

    $encoders = [new JsonEncoder()];
    $normalizers = [new ObjectNormalizer()];
    $serializer = new Serializer($normalizers, $encoders);

    $response = new JsonResponse($serializer->serialize($tariff, 'json'));
    $response->headers->set('Content-Type', 'application/json');

    return $response;
}

As a result of a request I have the following JSON:

"[{\u0022id\u0022:3,\u0022name\u0022:\u0022\\u041f\\u0440\\u043e\\u0434\\u0432\\u0438\\u043d\\u0443\\u0442\\u044b\\u0439\u0022,\u0022price\u0022:7900,\u0022description\u0022:null,\u0022leadMonthLimit\u0022:3000,\u0022status\u0022:100,\u0022active\u0022:true,\u0022comingSoon\u0022:false,\u0022image\u0022:\u0022advanced.jpg\u0022,\u0022user\u0022:null,\u0022deleted\u0022:false,\u0022product\u0022:{\u0022name\u0022:\u0022\\u0410\\u0431\\u043e\\u043d\\u0435\\u043d\\u0442\\u0441\\u043a\\u0430\\u044f \\u043f\\u043b\\u0430\\u0442\\u0430 \\u043f\\u043e \\u0442\\u0430\\u0440\\u0438\\u0444\\u0443 \\u00ab\\u041f\\u0440\\u043e\\u0434\\u0432\\u0438\\u043d\\u0443\\u0442\\u044b\\u0439\\u00bb\u0022,\u0022description\u0022:\u0022\\u0414\\u0430\\u043d\\u043d\\u044b\\u0439 \\u0442\\u0430\\u0440\\u0438\\u0444 \\u043f\\u043e\\u0437\\u0432\\u043e\\u043b\\u044f\\u0435\\u0442 \\u043e\\u0431\\u0440\\u0430\\u0431\\u0430\\u0442\\u044b\\u0432\\u0430\\u0442\\u044c \\u0434\\u043e 3000 \\u043a\\u043e\\u043d\\u0442\\u0430\\u043a\\u0442\\u043e\\u0432 \\u0432 \\u043c\\u0435\\u0441\\u044f\\u0446.\u0022,\u0022price\u0022:7900,\u0022specialPrice\u0022:null,\u0022fiscalInfo\u0022:{\u0022taxSystem\u0022:null,\u0022vatSystem\u0022:null},\u0022quantity\u0022:1,\u0022photo\u0022:\u0022advanced.jpg\u0022,\u0022photoList\u0022:[\u0022advanced.jpg\u0022]},\u0022limitsDescription\u0022:\u0022\\u0414\\u0430\\u043d\\u043d\\u044b\\u0439 \\u0442\\u0430\\u0440\\u0438\\u0444 \\u043f\\u043e\\u0437\\u0432\\u043e\\u043b\\u044f\\u0435\\u0442 \\u043e\\u0431\\u0440\\u0430\\u0431\\u0430\\u0442\\u044b\\u0432\\u0430\\u0442\\u044c \\u0434\\u043e 3000 \\u043a\\u043e\\u043d\\u0442\\u0430\\u043a\\u0442\\u043e\\u0432 \\u0432 \\u043c\\u0435\\u0441\\u044f\\u0446.\u0022,\u0022ecommerce\u0022:true,\u0022modules\u0022:[\u0022tariff.modules.ecommerce\u0022],\u0022__initializer__\u0022:null,\u0022__cloner__\u0022:null,\u0022__isInitialized__\u0022:true}]"

(in the db I have Cyrillic symbols). Have can I fix it?

My config.yml:

#FOSRestBundle
fos_rest:
    body_listener: true
    view:
      view_response_listener: force
    mime_types:
      json: ['application/json; charset=UTF-8', 'application/json']
    serializer:
      serialize_null: true
    body_converter:
      enabled: true
    format_listener:
      rules:
        - { path: '^/api',  priorities: ['json'], fallback_format: json, prefer_extension: true }
        - { path: '^/', priorities: [ 'html', '*/*'], fallback_format: html, prefer_extension: true }

1 Answer 1

2

JsonResponse will serialize your array, but it's already serialized by Serializer. You are now serializing your already serialized object.

Use setJson() instead:

$response = new JsonResponse();
$response->setJson($serializer->serialize($tariff, 'json'));

Take a look at \Symfony\Component\HttpFoundation\JsonResponse to what it's doing. Then you'll see that setting Content-Type is already done by JsonResponse::update (called from JsonResponse::setJson).

If you use JsonResponse($data) or JsonResponse::setData(), \json_encode is being called.

Sign up to request clarification or add additional context in comments.

3 Comments

I change my code: $response = new JsonResponse(); $response->setJson($serializer->serialize($tariff, 'json')); But the result is same
I think Stephan has right. Also try to use jmsyst.com/bundles/JMSSerializerBundle , a good solution for serialization
Well, I try to do following: json_encode($serializer->normalize($data), JSON_UNESCAPED_UNICODE) and the data is ok. I think the problem with Serializer. Do you know what's wrong? :(

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.