You are here

class JsonCodec in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2

JSON coder/decoder. Converting objects to JSON and back with fixing some common encoding errors.

Hierarchy

  • class \Drupal\n1ed\Flmngr\FileUploaderServer\lib\JsonCodec

Expanded class hierarchy of JsonCodec

2 files declare their use of JsonCodec
FlmngrServer.php in src/Flmngr/FlmngrServer/FlmngrServer.php
UploaderServlet.php in src/Flmngr/FileUploaderServer/servlet/UploaderServlet.php

File

src/Flmngr/FileUploaderServer/lib/JsonCodec.php, line 11

Namespace

Drupal\n1ed\Flmngr\FileUploaderServer\lib
View source
class JsonCodec {
  protected $actions;

  /**
   * Creates a codec.
   */
  public function __construct($actions) {
    $this->actions = $actions;
  }

  /**
   * Parses request JSON.
   */
  public function fromJson($json) {
    $jsonObj = json_decode($json, FALSE);
    if ($jsonObj === NULL) {
      throw new Exception('Unable to parse JSON');
    }
    if (!isset($jsonObj->action)) {
      throw new Exception('"Unable to detect action in JSON"');
    }
    $action = $this->actions
      ->getAction($jsonObj->action);
    if ($action === NULL) {
      throw new Exception("JSON action is incorrect: " . $action);
    }
    return $jsonObj;
  }

  /**
   * Stringifies response to JSON.
   */
  public function toJson($resp) {
    return JsonCodec::staticToJson($resp);
  }

  /**
   * Encodes and escapes JSON.
   */
  public static function staticToJson($resp) {
    $json = json_encode($resp);
    $json = str_replace('\\u0000*\\u0000', '', $json);
    $json = str_replace('\\u0000', '', $json);
    return $json;
  }
  public static function s_toJson($resp) {
    $json = json_encode($resp);
    $json = str_replace('\\u0000*\\u0000', '', $json);
    $json = str_replace('\\u0000', '', $json);
    return $json;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsonCodec::$actions protected property
JsonCodec::fromJson public function Parses request JSON.
JsonCodec::staticToJson public static function Encodes and escapes JSON.
JsonCodec::s_toJson public static function
JsonCodec::toJson public function Stringifies response to JSON.
JsonCodec::__construct public function Creates a codec.