@param int $value

@throws Exception\InvalidJsonEncodeOptionsException

@return JsonEncodeOptions
516910
public static function fromInt(int $value): self
    {
        if (0 > $value) {
            throw Exception\InvalidJsonEncodeOptionsException::fromJsonEncodeOptions($value);
        }

        return new self($value);
    }
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;
    }
47520
protected function jsonDecode($value, $errorMessage = null) {
        $decoded = json_decode($value, true);

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new InvalidArgumentException(
                $errorMessage ?: 'The supplied parameter is not a valid JSON object.'
            );
        }

        return $decoded;
    }
90372
public function serialize($value)
    {
        $this->reset();
        $serializedData = $this->serializeData($value);
        $encoded = json_encode($serializedData, $this->calculateEncodeOptions());
        if ($encoded === false || json_last_error() != JSON_ERROR_NONE) {
            if (json_last_error() != JSON_ERROR_UTF8) {
                throw new JsonSerializerException('Invalid data to encode to JSON. Error: ' . json_last_error());
            }

            $serializedData = $this->encodeNonUtf8ToUtf8($serializedData);
            $encoded = json_encode($serializedData, $this->calculateEncodeOptions());

            if ($encoded === false || json_last_error() != JSON_ERROR_NONE) {
                throw new JsonSerializerException('Invalid data to encode to JSON. Error: ' . json_last_error());
            }
        }
        return $this->processEncodedValue($encoded);
    }
121143
protected function jsonEncode(Field $f, $value)
    {
        // constant supported only starting PHP 7.3
        if (!defined('JSON_THROW_ON_ERROR')) {
            define('JSON_THROW_ON_ERROR', 0);
        }

        $res = json_encode($value, JSON_THROW_ON_ERROR, 512);
        if (JSON_THROW_ON_ERROR == 0 && json_last_error() !== JSON_ERROR_NONE) {
            throw new Exception([
                'There was error while encoding JSON',
                'code'  => json_last_error(),
                'error' => json_last_error_msg(),
            ]);
        }

        return $res;
    }
71920
private function decodeJson($value)
    {
        $decodedValue = json_decode($value, true);
        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new \InvalidArgumentException('JSON parsing error: ' . json_last_error_msg());
        }

        return $decodedValue;
    }
968948
private static function jsonEncode($value, int $flags = null, int $maxDepth = 512): string
    {
        $flags = $flags ?? (JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PRESERVE_ZERO_FRACTION);

        if (!\is_array($value) && !$value instanceof \JsonSerializable) {
            throw new InvalidArgumentException(sprintf('Option "json" must be array or JsonSerializable, %s given.', \is_object($value) ? \get_class($value) : \gettype($value)));
        }

        try {
            $value = json_encode($value, $flags | (\PHP_VERSION_ID >= 70300 ? JSON_THROW_ON_ERROR : 0), $maxDepth);
        } catch (\JsonException $e) {
            throw new InvalidArgumentException(sprintf('Invalid value for "json" option: %s.', $e->getMessage()));
        }

        if (\PHP_VERSION_ID < 70300 && JSON_ERROR_NONE !== json_last_error() && (false === $value || !($flags & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
            throw new InvalidArgumentException(sprintf('Invalid value for "json" option: %s.', json_last_error_msg()));
        }

        return $value;
    }
894728
protected function getParams($type, $value)
    {
        if (false === strpos($value, '-')) {
            throw new InvalidConfigurationException(sprintf('The "%s" option must be configured with "{prefix}-{size}"', $type));
        }

        list($prefix, $size) = explode('-', $value);

        if (!\in_array($prefix, $this->validPrefix)) {
            throw new InvalidConfigurationException(sprintf('The "%s" prefix option does not exist. Known options are: "'.implode('", "', $this->validPrefix).'"', $type));
        }

        if (!(int) $size) {
            throw new InvalidConfigurationException(sprintf('The "%s" size option must be an integer', $type));
        }

        return [$prefix, $size];
    }
120865
protected function serializeValue($value)
    {
        if (is_string($value)) {
            return $value;
        }
        if (is_object($value) || is_array($value)) {
            return json_encode($value);
        }
    }
614621
public static function encode($value, Context $context)
    {
        if (is_bool($value)) {
            return new BooleanValue($context->getIsolate(), $value);
        } elseif (is_string($value)) {
            return new StringValue($context->getIsolate(), $value);
        } elseif (is_float($value)) {
            return new NumberValue($context->getIsolate(), $value);
        } elseif (is_int($value)) {
            return new IntegerValue($context->getIsolate(), $value);
        } elseif ($value === null) {
            return new NullValue($context->getIsolate());
        } elseif ($value instanceof \DateTime) {
            return new DateObject($context, $value->getTimestamp() * 1000);
        } elseif ($value instanceof ObjectInterface) {
            return self::encode($value->getProperties(), $context);
        } elseif ($value instanceof \Closure || is_callable($value)) {
            return new FunctionObject($context, function(FunctionCallbackInfo $info) use ($value){
                $args   = $info->arguments();
                $params = [];
                foreach ($args as $arg) {
                    $params[] = Decoder::decode($arg, $info->getContext());
                }

                $return = call_user_func_array($value, $params);

                $info->getReturnValue()->set(self::encode($return, $info->getContext()));
            });
        } elseif ($value instanceof \ArrayObject) {
            return self::encode($value->getArrayCopy(), $context);
        } elseif ($value instanceof \Traversable) {
            return self::encode(iterator_to_array($value), $context);
        } elseif ($value instanceof \JsonSerializable) {
            return self::encode($value->jsonSerialize(), $context);
        } elseif ($value instanceof \stdClass || (is_array($value) && self::isAssoc($value))) {
            $object = new ObjectValue($context);
            foreach ($value as $key => $val) {
                $object->set(
                    $context,
                    self::encode($key, $context),
                    self::encode($val, $context)
                );
            }
            return $object;
        } elseif (is_array($value)) {
            $array = new ArrayObject($context);
            $index = 0;
            foreach ($value as $val) {
                $array->set(
                    $context,
                    self::encode($index, $context),
                    self::encode($val, $context)
                );
                $index++;
            }
            return $array;
        } elseif (is_object($value) && method_exists($value, '__toString')) {
            return new StringValue($context->getIsolate(), $value->__toString());
        } else {
            return new NullValue($context->getIsolate());
        }
    }
562448
public function transform($value)
    {
        if ($value instanceof JsonSerializable || $value instanceof StdClass) {
            return json_encode($value);
        }

        // If a toJson() method exists, the object can cast itself automatically.
        if (method_exists($value, 'toJson')) {
            return $value;
        }

        // Otherwise, if the object doesn't even have a __toString() method, we can't proceed.
        if (! method_exists($value, '__toString')) {
            throw new \Exception('Cannot transform this object to JavaScript.');
        }

        return "'{$value}'";
    }
691505
protected function getJsonBindingParameter($value)
    {
        if ($value instanceof Expression) {
            return $value->getValue();
        }

        switch ($type = gettype($value)) {
            case 'boolean':
                return $value ? 'true' : 'false';
            case 'NULL':
            case 'integer':
            case 'double':
            case 'string':
                return '?';
            case 'object':
            case 'array':
                return '?';
        }

        throw new InvalidArgumentException("JSON value is of illegal type: {$type}");
    }
655389
protected function transformObject($value)
    {
        if (! is_object($value)) {
            return;
        }
        if ($value instanceof JsonSerializable || $value instanceof StdClass) {
            return json_encode($value);
        }
        // If a toJson() method exists, we'll assume that
        // the object can cast itself automatically.
        if (method_exists($value, 'toJson')) {
            return $value;
        }
        // Otherwise, if the object doesn't even have a
        // __toString() method, we can't proceed.
        if (! method_exists($value, '__toString')) {
            throw new Exception('Cannot transform this object to JavaScript.');
        }

        return "'{$value}'";
    }
663893
public function encode($value, int $options = null, int $depth = null): string
    {
        list($json, $error) = $this->softEncode($value, $options, $depth);
        if ($error !== null) {
            throw $error;
        }
        return $json;
    }
1093272
protected function phpJsonEncodeNameValue( $name, $value )
    {
        return (sprintf("%s:%s", $this->phpJsonEncode(strval($name)), $this->phpJsonEncode($value)));
    }
295695
public function marshal($value)
    {
        if ($this->options()->allowed && !in_array($value, $this->options()->allowed)) {
            throw new \InvalidArgumentException(
                sprintf(
                    'Allowed values for this field are %s.',
                    implode(', ', $this->options()->allowed)
                )
            );
        }
        return new TypedValue((string) $value);
    }
921681
public static function jsonDecode($value)
    {
        if (defined('JSON_PARSER_NOTSTRICT')) { // pecl-json-c ext
            $decoded = json_decode($value, false, 512, JSON_PARSER_NOTSTRICT);
        } else { // json-ext
            $decoded = json_decode($value);
        }

        return $decoded;
    }