Oihana PHP

json

Table of Contents

Functions

deepJsonSerialize()  : mixed
Recursively serialize all JsonSerializable values within an array or object.
getJsonType()  : string
Get JSON type of a PHP value.
isValidJsonDecodeFlags()  : bool
Checks whether a given integer value is a valid combination of `json_decode()` option flags.
isValidJsonEncodeFlags()  : bool
Checks whether a given integer value is a valid combination of `json_encode()` option flags.

Functions

deepJsonSerialize()

Recursively serialize all JsonSerializable values within an array or object.

deepJsonSerialize(mixed $value[, int $currentDepth = 0 ]) : mixed

This function traverses arrays and objects at any depth, and whenever it finds a value that implements JsonSerializable, it calls jsonSerialize() on it.

Parameters
$value : mixed

The value to serialize.

$currentDepth : int = 0

Internal recursion counter (default 0).

Tags
example
$data =
[
    'user' => new User(),       // implements JsonSerializable
    'tags' => ['a', 'b'],
];

$result = deepJsonSerialize($data);
// All JsonSerializable objects will be converted to arrays/values recursively
author

Marc Alcaraz (ekameleon)

since
1.0.6
Return values
mixed

The value with all JsonSerializable elements serialized.

getJsonType()

Get JSON type of a PHP value.

getJsonType(mixed $value[, string $default = 'unknown' ]) : string
Parameters
$value : mixed

The value to evaluates.

$default : string = 'unknown'

The default return value if the type is not a valid JSON type.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.7
Return values
string

isValidJsonDecodeFlags()

Checks whether a given integer value is a valid combination of `json_decode()` option flags.

isValidJsonDecodeFlags(int $flags) : bool

The function compares the provided bitmask against the list of officially supported JSON_* constants for json_decode() in the current PHP version.

Note: PHP itself does not validate unknown flag bits in json_decode() — they are simply ignored. This helper ensures stricter validation.

Parameters
$flags : int

One or more JSON_* constants combined with bitwise OR (|).

Tags
example
use function oihana\core\json\isValidJsonDecodeFlags;

var_dump( isValidJsonDecodeFlags(0) );                        // true
var_dump( isValidJsonDecodeFlags(JSON_BIGINT_AS_STRING) );    // true
var_dump( isValidJsonDecodeFlags(JSON_OBJECT_AS_ARRAY) );     // true
var_dump( isValidJsonDecodeFlags(JSON_INVALID_UTF8_IGNORE) ); // true
var_dump( isValidJsonDecodeFlags(1 << 30) );                  // false
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool

Returns true if the given flags are a valid combination of json_decode() options, false otherwise.

isValidJsonEncodeFlags()

Checks whether a given integer value is a valid combination of `json_encode()` option flags.

isValidJsonEncodeFlags(int $flags) : bool

The function compares the provided bitmask against the list of officially supported JSON_* constants in the current PHP version. If any unsupported bits are set, the function will return false.

Note: PHP itself does not validate unknown flag bits in json_encode() — they are simply ignored. This helper ensures stricter validation.

Parameters
$flags : int

One or more JSON_* constants combined with bitwise OR (|).

Tags
example
use function oihana\core\json\isValidJsonEncodeFlags;

var_dump( isValidJsonEncodeFlags( 0 ) );                                       // true (no options)
var_dump( isValidJsonEncodeFlags( JSON_PRETTY_PRINT) );                       // true
var_dump( isValidJsonEncodeFlags( JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ) ; // true
var_dump( isValidJsonEncodeFlags( JSON_PRETTY_PRINT | 123456 ) );             // false
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool

Returns true if the given flags are a valid combination of json_encode() options, false otherwise.


        
On this page

Search results