JsonCodec
Interfaces to serialize and deserialize PHP objects to/from JSON
|
Helper class to serialize/unserialize things to/from JSON. More...
Public Member Functions | |
__construct (?ContainerInterface $serviceContainer=null) | |
toJsonString ( $value, $classHint=null) | |
Recursively converts a given object to a JSON-encoded string. | |
newFromJsonString ( $json, $classHint=null) | |
Recursively converts a JSON-encoded string to an object value or scalar. | |
addCodecFor (string $className, JsonClassCodec $codec) | |
Allow the use of a customized encoding for the given class; the given className need not be a JsonCodecable and if it does correspond to a JsonCodecable it will override the class codec specified by the JsonCodecable. | |
toJsonArray ( $value, $classHint=null) | |
Recursively converts a given object to an associative array which can be json-encoded. | |
newFromJsonArray ( $json, $classHint=null) | |
Recursively converts an associative array (or scalar) to an object value (or scalar). | |
Protected Member Functions | |
codecFor (string $className) | |
Maintain a cache giving the codec for a given class name. | |
isArrayMarked (array $value) | |
Determine if the given value is "marked"; that is, either represents a object type encoded using a JsonClassCodec or else is an array which contains values (or contains arrays containing values, etc) which are object types. | |
markArray (array &$value, string $className, ?string $classHint) | |
Record a mark in the array, reversibly. | |
unmarkArray (array &$value, ?string $classHint) | |
Remove a mark from an encoded array, and return an encoded class name if present. | |
Helper class to serialize/unserialize things to/from JSON.
Wikimedia\JsonCodec\JsonCodec::__construct | ( | ?ContainerInterface | $serviceContainer = null | ) |
?ContainerInterface | $serviceContainer |
string | $id |
@inheritDoc
Wikimedia\JsonCodec\JsonCodec::addCodecFor | ( | string | $className, |
JsonClassCodec | $codec ) |
Allow the use of a customized encoding for the given class; the given className need not be a JsonCodecable and if it does correspond to a JsonCodecable it will override the class codec specified by the JsonCodecable.
class-string | $className | |
JsonClassCodec | $codec | A codec to use for $className |
|
protected |
Maintain a cache giving the codec for a given class name.
Reusing this JsonCodec object will also reuse this cache, which could improve performance somewhat.
class-string | $className |
|
protected |
Determine if the given value is "marked"; that is, either represents a object type encoded using a JsonClassCodec or else is an array which contains values (or contains arrays containing values, etc) which are object types.
The values of unmarked arrays are not decoded, in order to speed up the decoding process. Arrays may also be marked even if they do not represent object types (or an array recursively containing them) if they contain keys that need to be escaped ("false marks"); as such this method is called both on the raw results of JsonClassCodec (to check for "false marks") as well as on encoded arrays (to find "true marks").
Arrays do not have to be marked if the decoder has a class hint.
array | $value | An array result from JsonClassCodec::toJsonArray() , or an array result from ::markArray() |
|
protected |
Record a mark in the array, reversibly.
The mark should record the class name, if it is different from the class hint. The result does not need to trigger ::isArrayMarked
if there is an accurate class hint present, but otherwise the result should register as marked. The provided value may be a "complex" array (one that recursively contains encoded object) or an array with a "false mark"; in both cases the provided $className will be array
.
array | &$value | An array result from JsonClassCodec::toJsonArray() or a "complex" array |
'array'|class-string | $className The name of the class encoded by the codec, or else array if $value is a "complex" array or a "false mark" | |
'array'|class-string|null | $classHint The class name provided as a hint to the encoder, and which will be in turn provided as a hint to the decoder, or null if no hint was provided. The class hint will be array when the array is a homogeneous list of objects. |
Wikimedia\JsonCodec\JsonCodec::newFromJsonArray | ( | $json, | |
$classHint = null ) |
Recursively converts an associative array (or scalar) to an object value (or scalar).
While converting this value JsonCodec delegates to the appropriate JsonClassCodecs of any classes which implement JsonCodecable.
For objects encoded using implicit class information, a "class hint" can be provided to guide deserialization; this is unnecessary for objects serialized with explicit classes.
mixed | null | $json | |
class-string | Hint | null | $classHint | An optional hint to the type of the encoded object. In the absence of explicit type information in the JSON, this will be used as the type of the created object. |
Implements Wikimedia\JsonCodec\JsonCodecInterface.
Wikimedia\JsonCodec\JsonCodec::newFromJsonString | ( | $json, | |
$classHint = null ) |
Recursively converts a JSON-encoded string to an object value or scalar.
While deserializing the $json JsonCodec delegates to the appropriate JsonClassCodecs of any classes which implement JsonCodecable.
For objects encoded using implicit class information, a "class hint" can be provided to guide deserialization; this is unnecessary for objects serialized with explicit classes.
string | $json | A JSON-encoded string |
class-string | Hint | null | $classHint | An optional hint to the type of the encoded object. In the absence of explicit type information in the JSON, this will be used as the type of the created object. |
Wikimedia\JsonCodec\JsonCodec::toJsonArray | ( | $value, | |
$classHint = null ) |
Recursively converts a given object to an associative array which can be json-encoded.
(When embedding an object into another context it is sometimes useful to have the array representation rather than the string JSON form of the array; this can also be useful if you want to pretty-print the result, etc.) While converting $value the JsonCodec delegates to the appropriate JsonClassCodecs of any classes which implement JsonCodecable.
If a $classHint is provided and matches the type of the value, then type information will not be included in the generated JSON; otherwise an appropriate class name will be added to the JSON to guide deserialization.
mixed | null | $value | |
class-string | Hint | null | $classHint | An optional hint to the type of the encoded object. If this is provided and matches the type of $value, then explicit type information will be omitted from the generated JSON, which saves some space. |
Implements Wikimedia\JsonCodec\JsonCodecInterface.
Wikimedia\JsonCodec\JsonCodec::toJsonString | ( | $value, | |
$classHint = null ) |
Recursively converts a given object to a JSON-encoded string.
While serializing the $value JsonCodec delegates to the appropriate JsonClassCodecs of any classes which implement JsonCodecable.
If a $classHint is provided and matches the type of the value, then type information will not be included in the generated JSON; otherwise an appropriate class name will be added to the JSON to guide deserialization.
mixed | null | $value | |
class-string | Hint | null | $classHint | An optional hint to the type of the encoded object. If this is provided and matches the type of $value, then explicit type information will be omitted from the generated JSON, which saves some space. |
|
protected |
Remove a mark from an encoded array, and return an encoded class name if present.
The provided array may not trigger ::isArrayMarked
is there was a class hint provided.
If the provided array had a "false mark" or recursively contained objects, the returned class name should be 'array'.
array | &$value | An encoded array |
'array'|class-string|null | $classHint The class name provided as a hint to the decoder, which was previously provided as a hint to the encoder, or null if no hint was provided. |