You are here

class JsonUtility in Feeds extensible parsers 8

Various helpers for handling JSON.

Hierarchy

Expanded class hierarchy of JsonUtility

6 files declare their use of JsonUtility
JmesPathLinesParserTest.php in tests/src/Unit/Feeds/Parser/JmesPathLinesParserTest.php
JmesPathParser.php in src/Feeds/Parser/JmesPathParser.php
JmesPathParserTest.php in tests/src/Unit/Feeds/Parser/JmesPathParserTest.php
JsonParserBase.php in src/Feeds/Parser/JsonParserBase.php
JsonPathLinesParserTest.php in tests/src/Unit/Feeds/Parser/JsonPathLinesParserTest.php

... See full list

1 string reference to 'JsonUtility'
feeds_ex.services.yml in ./feeds_ex.services.yml
feeds_ex.services.yml
1 service uses JsonUtility
feeds_ex.json_utility in ./feeds_ex.services.yml
Drupal\feeds_ex\Utility\JsonUtility

File

src/Utility/JsonUtility.php, line 11

Namespace

Drupal\feeds_ex\Utility
View source
class JsonUtility {
  use StringTranslationTrait;

  /**
   * Translates an error message.
   *
   * @param int $error
   *   The JSON error.
   *
   * @return string
   *   The JSON parsing error message.
   */
  public function translateError($error) {
    switch ($error) {
      case JSON_ERROR_RECURSION:
        return 'One or more recursive references in the value to be encoded';
      case JSON_ERROR_INF_OR_NAN:
        return 'One or more NAN or INF values in the value to be encoded';
      case JSON_ERROR_UNSUPPORTED_TYPE:
        return 'A value of a type that cannot be encoded was given';
      case JSON_ERROR_UTF8:
        return 'Malformed UTF-8 characters, possibly incorrectly encoded';
      case JSON_ERROR_NONE:
        return 'No error has occurred';
      case JSON_ERROR_DEPTH:
        return 'The maximum stack depth has been exceeded';
      case JSON_ERROR_STATE_MISMATCH:
        return 'Invalid or malformed JSON';
      case JSON_ERROR_CTRL_CHAR:
        return 'Control character error, possibly incorrectly encoded';
      case JSON_ERROR_SYNTAX:
        return 'Syntax error';
      default:
        return 'Unknown error';
    }
  }

  /**
   * Decodes a JSON string into an array.
   *
   * @param string $json
   *   A JSON string.
   *
   * @return array
   *   A PHP array.
   *
   * @throws \RuntimeException
   *   Thrown if the encoded JSON does not result in an array.
   */
  public function decodeJsonArray($json) {
    $parsed = Json::decode($json);
    if (!is_array($parsed)) {
      throw new \RuntimeException($this
        ->t('The JSON is invalid.'));
    }
    return $parsed;
  }

  /**
   * Decodes a JSON string into an object or array.
   *
   * @param string $json
   *   A JSON string.
   *
   * @return object|array
   *   A PHP object or array.
   *
   * @throws \RuntimeException
   *   Thrown if the encoded JSON does not result in an array or object.
   */
  public function decodeJsonObject($json) {
    $parsed = json_decode($json, FALSE);
    if (!is_array($parsed) && !is_object($parsed)) {
      throw new \RuntimeException($this
        ->t('The JSON is invalid.'));
    }
    return $parsed;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsonUtility::decodeJsonArray public function Decodes a JSON string into an array.
JsonUtility::decodeJsonObject public function Decodes a JSON string into an object or array.
JsonUtility::translateError public function Translates an error message.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.