U
U
UksusoFF2014-02-24 13:46:33
Drupal
UksusoFF, 2014-02-24 13:46:33

How to set up Drupal Services for normal output of Russian text in JSON?

Good afternoon, I have Drupal 7 with Services
configured . Services Views form JSON. During the preview in Views, the Russian text is displayed correctly. But when you try to get JSON through the endpoint, then the Russian text is encoded in:

\u0418\u0432\u0430\u043d \u0422\u0443\u0440\u0438\u0441\u0442

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
UksusoFF, 2014-02-25
@UksusoFF

A small hack made it possible to reduce the JSON size from 379 to 146 kb, i.e. more than 2 times.
If someone tells me how to do it right, I will be glad:

class ServicesJSONFormatter implements ServicesFormatterInterface {
  public function render($data) {
    function php2js($a = false) {
        if (is_null($a) || is_resource($a)) {
            return 'null';
        }
        if ($a === false) {
            return 'false';
        }
        if ($a === true) {
            return 'true';
        }
        if (is_scalar($a)) {
            if (is_float($a)) {
                $a = str_replace(',', '.', strval($a));
            }
            static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '', '\\t', '', '\\b', '\\f', '\"'));
            return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
        }
        $isList = true;
        for ($i = 0, reset($a); $i < count($a); $i++, next($a)) {
            if (key($a) !== $i) {
                $isList = false;
                break;
            }
        }
        $result = array();
        if ($isList) {
            foreach ($a as $v) {
                $result[] = php2js($v);
            }
            return '[ ' . join(', ', $result) . ' ]';
        } else {
            foreach ($a as $k => $v) {
                $result[] = php2js($k) . ': ' . php2js($v);
            }
            return '{ ' . join(', ', $result) . ' }';
        }
    }
    // json_encode doesn't give valid json with data that isn't an array/object.
    if (is_scalar($data)) {
      $data = array($data);
    }
    return str_replace('\\/', '/', php2js($data));
  }
}

sites/all/modules/services/servers/rest_server/includes/ServicesFormatter.inc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question