Download the PHP package jesseschalken/pure-json without Composer
On this page you can find all versions of the php package jesseschalken/pure-json. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jesseschalken/pure-json
More information about jesseschalken/pure-json
Files in jesseschalken/pure-json
Package pure-json
Short Description json_encode/json_decode wrapper with error checking and one-to-one mapping between PHP and JSON values
License LGPL-2.1
Informations about the package pure-json
PureJSON
json_encode()
/json_decode()
wrapper for PHP 5.3+ with one-to-one mapping between JSON and PHP values.
-
JSON::encode()
will only accept values which can be converted into their exact original byJSON::decode()
, so thatJSON::decode(JSON::encode($x)) === $x
. The accepted values are:int
string
float
(but notINF
,-INF
orNAN
)bool
null
array
(whose contents are also valid)
-
JSON::encode()
/JSON::decode()
will assume PHP strings are UTF-8 by default. To encode/decode binary or ISO-8859-1 strings, useJSON::encode(..., true)
andJSON::decode(..., true)
. -
To pretty print JSON, use
JSON::encode(..., ..., true)
. JSON::encode()
/JSON::decode()
will checkjson_last_error()
for you and throw aPureJSON\JSONException
with an appropriate code and message.
Serialization
The methods JSON::serialize()
and JSON::deserialize()
differ from JSON::encode()
and JSON::decode()
by mapping the JSON {...}
syntax to and from PHP objects instead of to and from PHP associative arrays. Whereas JSON::encode()
rejects objects and accepts associative arrays, JSON::serialize()
rejects associative arrays and accepts objects.
In order for JSON::deserialize()
to reproduce an instance of the original class given to JSON::serialize()
:
- Objects provided to
JSON::serialize()
must implement thePureJSON\Serializable
interface. - The method
jsonProps()
is used for properties andjsonType()
is used to fill a special@type
property to identify the type of the object. JSON::deserialize()
requires an explicit list of classes implementingPureJSON\Serializable
to possibly instantiate using the methodjsonCreate($props)
.
(By storing a type tag instead of the PHP class name in JSON, the PHP class can be renamed while maintaining compatibility with existing serialized data, and if the JSON given to JSON::deserialize()
is produced by an attacker, they cannot instantiate classes outside of the explicit list.)
Exmaple
With JSON::encode()
/JSON::decode()
:
With JSON::serialize()
/JSON::deserialize()
: