remorhaz / php-json-data
JSON Data Accessors
Installs: 575 342
Dependents: 4
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 3
Open Issues: 2
pkg:composer/remorhaz/php-json-data
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- ext-intl: *
- ext-json: *
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8
- phpunit/phpunit: ^10.1 || ^11
README
This library provides infrastructure for JSON documents processing.
Requirements
- PHP 8.1 or newer.
- Internationalization functions (ext-intl) - to compare Unicode strings.
- JSON extension (ext-json) - to encode and decode JSON documents.
Node Values
ValueInterface can be used to represent JSON document or it's part. There is a set of descendant interfaces that provide access to typed values:
ScalarValueInterfaceArrayValueInterfaceObjectValueInterface
NodeValueInterface includes information about value's path in the document, so it can be used to represent values that really exist in some JSON document. Calculated values that have no paths should implement basic ValueInterface.
Node Value Factories
This type of objects is intended to create node values from some source. This library includes two implementations of node value factories:
DecodedJson\NodeValueFactorycreates node value from raw PHP values (produced byjson_decode()function or created manually).EncodedJson\NodeValueFactorycreates node value directly from encoded JSON string.
Example
<?php use Remorhaz\JSON\Data\Value\DecodedJson; use Remorhaz\JSON\Data\Value\EncodedJson; // Both values represent same JSON document: $value1 = EncodedJson\NodeValueFactory::create()->createValue('{"a":"b"}'); $value2 = DecodedJson\NodeValueFactory::create()->createValue((object) ['a' => 'b']);
Event Streams
JSON document can be represented as a stream of events. These events implement descendants of EventInterface. Such events can be emitted by any custom JSON parser, but this library also implements standard ValueWalker object that converts any NodeValueInterface to event stream.
Value walker is also able to use mutations to alter the events. There're no standard mutations, you must implement MutationInterface by yourself.
You can also use EventDecoder object to convert event stream back to NodeValueInterface.
Value exporters
Library includes a set of ValueExporterInterface implementations that allow to export ValueInterface to another representation:
ValueEncoderconverts value to JSON-encoded string.ValueDecoderconverts value to raw PHP values.
Comparators
Library includes a set of ComparatorInterface implementations that provide a simple way to compare/sort JSON documents:
EqualValueComparatorchecks for JSON documents equality. Note that objects with same properties in different order are considered equal.GreaterValueComparatorcompares JSON numbers and strings.ContainsValueComparatorchecks JSON documents either for equality or for containment. Containing object is allowed to have additional properties on any level of recursion.
License
This library is licensed under the MIT License. Please see LICENSE for more information.