You are here

WSDecoderJSON.php in Web Service Data 2.0.x

Same filename and directory in other branches
  1. 8 src/Plugin/WSDecoder/WSDecoderJSON.php

File

src/Plugin/WSDecoder/WSDecoderJSON.php
View source
<?php

namespace Drupal\wsdata\Plugin\WSDecoder;

use Drupal\wsdata\Plugin\WSDecoderBase;
use Drupal\Component\Serialization\Json;

/**
 * JSON Decoder.
 *
 * @WSDecoder(
 *   id = "WSDecoderJSON",
 *   label = @Translation("JSON Decoder", context = "WSDecoder"),
 * )
 */
class WSDecoderJSON extends WSDecoderBase {

  /**
   * {@inheritdoc}
   */
  public function decode($data) {
    if (!isset($data) || empty($data)) {
      return;
    }

    // Remove UTF-8 BOM if present, json_decode() does not like it.
    if (substr($data, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
      $data = substr($data, 3);
    }
    $data = trim($data);
    return Json::decode($data);
  }

  /**
   * {@inheritdoc}
   */
  public function accepts() {
    return [
      'text/json',
    ];
  }

}

Classes

Namesort descending Description
WSDecoderJSON JSON Decoder.