ArrayConversionTrait::__toJSON

@see http://php.net/manual/en/function.json-encode.php

@param int $options JSON encode options, default JSON_PRETTY_PRINT
@param int $depth   Maximum depth of JSON encode. Must be greater than zero.

@return string
327023
public function __toJson($options = JSON_PRETTY_PRINT, $depth = 512)
    {
        $depth = $depth == 0 ? 512 : $depth;

        return call_user_func_array('json_encode', [$this->getArrayCopy(), $options, $depth]);
    }
82388
public static function encode($value, $options = null)
    {
        if ($options === null) {
            $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT;
        }

        $json = json_encode($value, $options);

        static::handleJsonError(json_last_error());

        return $json;
    }
485357
public function getDataAsJsonString($option = 0, $depth = 512): string
    {
        $data = $this->get();

        if (\is_object($data) || \is_array($data)) {
            $data = \json_encode($data, $option, $depth);
        } else {
            $data = \json_encode([$data], $option, $depth);
        }

        return \json_encode($data, $option, $depth);
    }
446298
public function json($options = 0, $withDeferred = false)
    {
        $attributes = $this->attributes;
        if ($withDeferred) {
            foreach ($this->deferred as $attribute => $collection) {
                if (empty($collection)) {
                    continue; // Leave orriginal array with __deferred key
                }

                $attributes[$attribute] = [];
                foreach ($collection as $value) {
                    if (! empty($value->deferred)) {
                        $value->attributes = array_merge($value->attributes, $value->deferred);
                    }

                    if (is_a($value, 'Picqer\Financials\Exact\Model')) {
                        array_push($attributes[$attribute], $value->attributes);
                    } else {
                        array_push($attributes[$attribute], $value);
                    }
                }
            }
        }

        return json_encode($attributes, $options);
    }
1202138
public function toArray($assoc = true, $depth = 512, $options = 0)
    {
        return json_decode($this->bodyString(), $assoc, $depth, $options);
    }
103520
public function jsonDecodeFilter($str, $assoc = false, $depth = 512, $options = 0)
    {
        return json_decode(html_entity_decode($str), $assoc, $depth, $options);
    }
1251288
public static function arrayToJSONI18N(array $entities, array $locales=[], $depth=0) {
		foreach($entities as $k=>$entity)
			$entities[$k] = $entity->toArrayI18N($locales, $depth);
		return json_encode($entities);
	}
928227
public static function encode(
        $value,
        $options = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION
    ) {
        $json = \json_encode($value, $options);
        if (JSON_ERROR_NONE !== ($error = \json_last_error())) {
            throw new JsonException(\json_last_error_msg(), $error);
        }

        return $json;
    }
292950
public static function toJson($data, $options = array())
    {
        // Merge options with defaults
        $defaults = array('hide' => array('password'));
        $options = array_merge($defaults, $options);

        // Convert the data to an array, if possible..
        if (!is_array($data)) {
            $data = Arr::convert($data);
        }

        foreach ($data as $k => $v) {
            $data[$k] = Arr::convert($v);
        }

        // Remove the parts we don't want...
        if (isset($options['hide']) and is_array($data)) {
            $data = Arr::removeKeys($data, $options['hide']);
        }

        return json_encode($data);
    }
431901
def _deep_json_to_string(value, depth):
    
    if is_data(value):
        if depth == 0:
            return strings.limit(value2json(value), LOG_STRING_LENGTH)

        return {k: _deep_json_to_string(v, depth - 1) for k, v in value.items()}
    elif is_sequence(value):
        return strings.limit(value2json(value), LOG_STRING_LENGTH)
    elif isinstance(value, number_types):
        return value
    elif is_text(value):
        return strings.limit(value, LOG_STRING_LENGTH)
    elif is_binary(value):
        return strings.limit(bytes2base64(value), LOG_STRING_LENGTH)
    elif isinstance(value, (date, datetime)):
        return datetime2unix(value)
    else:
        return strings.limit(value2json(value), LOG_STRING_LENGTH)
1178880
public static function createFromJson(string $json, bool $assoc = true, int $depth = 512 , int $options = 0) : array
    {
        return json_decode($json, $assoc, $depth, $options);
    }
1112538
public static function encode(
        $value,
        int $options = 0,
        int $depth = 512,
        string $context = ''
    ): string {
        // Clear json_last_error()
        json_encode(null);

        $json = json_encode($value, $options, $depth);

        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new JsonError(
                sprintf(
                    'Unable to encode data to JSON: %s. %s',
                    json_last_error_msg(),
                    $context
                )
            );
        }

        return $json;
    }
990523
public function toJson($options = 0)
    {
        // Models' toJson() calls their toArray() function.  We don't want this to
        // happen twice
        if (!is_a($this, Model::class)) {
            $this->runSerializeTransforms();
        }

        // Continue execution
        return parent::toJson($options);
    }
1275242
public function encode($data, $options = 0) {
        $json = null;

        if ($data instanceof IArrayable) {
            $json = @json_encode($data->toArray(), $options);
        } else if ($data instanceof IJsonable) {
            $json = $data->toJson($options);
        } else if (is_array($data) || is_object($data)) {
            $json =  @json_encode($data, $options);
        } else {
            throw new InvalidDataTypeException(
                s('Can not convert data of type "%s" to json.', get_type($data))
            );
        }

        if ($json === false) {
            throw new JsonEncodeException(json_last_error_msg(), $data);
        }

        return $json;
    }
487141
public static function encode($data, $options = 0): string
    {
        $json = json_encode($data, $options);
        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new JsonEncodeException(json_last_error_msg());
        }

        return $json;
    }
1355744
public function getData($assoc = false, $depth = 512, $options = 0)
	{
		return json_decode($this->data, $assoc, $depth, $options);
	}
1169267
protected function encode($data, $options = 0)
    {
        // Check is already json string.
        if (is_string($data) && strlen($data) >= 1) {
            $firstChar = $data[0];

            if (in_array($firstChar, ['[', '{', '"'])) {
                return $data;
            }
        }

        // Clear json_last_error()
        json_encode(null);

        $json = json_encode($data, $options);

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \UnexpectedValueException(sprintf('JSON encode failure: %s', json_last_error_msg()));
        }

        return $json;
    }