4

Since updating to 8.3 we've noticed that some URL's in our REST responses are being escaped (this may have been happening before but I don't think so as it is now causing issues with the Android parser).

The URL's are escaped like this;

{"_links":
    {"self":
        {"href":"http:\/\/example.com\/api\/resource\/80?_format=hal_json"}

Those w's (//) and v's (/) are killing things and I'd like to remove this escaping.

BTW, this API is password protected and used by a controlled app only. While removing this escaping may introduce issues with public API's and security/stabiliy, we don't need this level of control.

Can I just override the Serialize class and use that?

1 Answer 1

1

Actually, I can't see how this could have been different prior to 8.3 - PHP's json_encode seems to have been escaping / by default forever, and neither Drupal nor Symfony seem to have overridden that in the past.

The service ID and class you would need to override is serializer.encoder.json (Drupal\serialization\Encoder\JsonEncoder), adding a JSON_UNESCAPED_SLASHES to the options bitmask in the constructor. (Confusingly, there's a separate service named serialization.json, which isn't the one that's used here.)

However, note that \/ is a valid escape sequence in JSON (if not strictly required), and a JSON parser must interpret this as a literal forward slash character to be working correctly. There should be no backslashed URLs in the parsed values. Is it possible that your client's parser library has a bug?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.