https://github.com/ergebnis/json-pointer
📃 Provides a composer package with an abstraction of a JSON pointer.
https://github.com/ergebnis/json-pointer
json pointer
Last synced: 9 months ago
JSON representation
📃 Provides a composer package with an abstraction of a JSON pointer.
- Host: GitHub
- URL: https://github.com/ergebnis/json-pointer
- Owner: ergebnis
- License: mit
- Created: 2022-01-28T14:31:35.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-03-24T16:19:29.000Z (9 months ago)
- Last Synced: 2025-04-12T02:14:03.586Z (9 months ago)
- Topics: json, pointer
- Language: PHP
- Size: 1.87 MB
- Stars: 17
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Codeowners: .github/CODEOWNERS
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# json-pointer
[](https://github.com/ergebnis/json-pointer/actions)
[](https://github.com/ergebnis/json-pointer/actions)
[](https://github.com/ergebnis/json-pointer/actions)
[](https://github.com/ergebnis/json-pointer/actions)
[](https://codecov.io/gh/ergebnis/json-pointer)
[](https://packagist.org/packages/ergebnis/json-pointer)
[](https://packagist.org/packages/ergebnis/json-pointer)
[](https://packagist.org/packages/ergebnis/json-pointer)
This project provides a [`composer`](https://getcomposer.org) package with an abstraction of a [JSON pointer](https://datatracker.ietf.org/doc/html/rfc6901).
## Installation
Run
```sh
composer require ergebnis/json-pointer
```
## Usage
### `ReferenceToken`
You can create a `ReferenceToken` from a `string` value:
```php
toJsonString(); // 'foo~19000~😆'
$referenceToken->toString(); // 'foo/9000/😆'
$referenceToken->toUriFragmentIdentifierString(); // 'foo~19000~1%F0%9F%98%86'
```
You can create a `ReferenceToken` from a [JSON `string` value](https://datatracker.ietf.org/doc/html/rfc6901#section-5):
```php
toJsonString(); // 'foo~19000~😆'
$referenceToken->toString(); // 'foo/9000/😆'
$referenceToken->toUriFragmentIdentifierString(); // 'foo~19000~1%F0%9F%98%86'
```
You can create a `ReferenceToken` from a [URI fragment identifier `string` value](https://datatracker.ietf.org/doc/html/rfc6901#section-6):
```php
toJsonString(); // 'foo~19000~😆'
$referenceToken->toString(); // 'foo/9000/😆'
$referenceToken->toUriFragmentIdentifierString(); // 'foo~19000~1%F0%9F%98%86'
```
You can create a `ReferenceToken` from an `int` value:
```php
toJsonString(); // '9001'
$referenceToken->toString(); // '9001'
$referenceToken->toUriFragmentIdentifierString(); // '9001'
```
You can compare `ReferenceToken`s:
```php
equals($two); // true
$one->equals($three); // false
```
### `JsonPointer`
You can create a `JsonPointer` referencing a document:
```php
toJsonString(); // ''
$jsonPointer->toUriFragmentIdentifierString(); // '#'
```
You can create a `JsonPointer` from a [JSON `string` representation](https://datatracker.ietf.org/doc/html/rfc6901#section-5) value:
```php
toJsonString(); // '/foo/bar/😆'
$jsonPointer->toUriFragmentIdentifierString(); // '#/foo/bar/%F0%9F%98%86'
```
You can create a `JsonPointer` from a [URI fragment identifier `string` representation](https://datatracker.ietf.org/doc/html/rfc6901#section-6) value:
```php
toJsonString(); // '/foo/bar/😆'
$jsonPointer->toUriFragmentIdentifierString(); // '#/foo/bar/%F0%9F%98%86'
```
You can create a `JsonPointer` from `ReferenceToken`s:
```php
toJsonString(); // '/foo/bar'
$jsonPointer->toUriFragmentIdentifierString(); // '#/foo/bar'
```
You can compare `JsonPointer`s:
```php
equals($two); // false
$one->equals($three); // true
```
You can append a `ReferenceToken` to a `JsonPointer`:
```php
append($referenceToken);
$newJsonPointer->toJsonString(); // '/foo/bar/baz'
$newJsonPointer->toUriFragmentIdentifierString(); // '#foo/bar/baz'
```
### `Specification`
You can create a `Specification` that is always satisfied by a `JsonPointer`:
```php
isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo')); // true
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // true
```
You can create a `Specification` that is satisfied when a closure returns `true` for a `JsonPointer`:
```php
toJsonString() === '/foo/bar';
});
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo')); // false
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // true
```
You can create a `Specification` that is satisfied when a `JsonPointer` equals another `JsonPointer`:
```php
isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo')); // false
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // true
```
You can create a `Specification` that is never satisfied by a `JsonPointer`:
```php
isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo')); // false
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // false
```
You can create a `Specification` that is satisfied when another `Specification` is not satisfied by a `JsonPointer`:
```php
isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo')); // true
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // false
```
You can compose `Specification`s to find out if a `JsonPointer` satisfies any of them:
```php
toJsonString() === '/foo/bar';
}),
Pointer\Specification::equals(Pointer\JsonPointer::fromJsonString('/foo/baz')),
Pointer\Specification::never(),
);
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo')); // false
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // true
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/baz')); // true
```
## Changelog
The maintainers of this project record notable changes to this project in a [changelog](CHANGELOG.md).
## Contributing
The maintainers of this project suggest following the [contribution guide](.github/CONTRIBUTING.md).
## Code of Conduct
The maintainers of this project ask contributors to follow the [code of conduct](https://github.com/ergebnis/.github/blob/main/CODE_OF_CONDUCT.md).
## General Support Policy
The maintainers of this project provide limited support.
You can support the maintenance of this project by [sponsoring @localheinz](https://github.com/sponsors/localheinz) or [requesting an invoice for services related to this project](mailto:am@localheinz.com?subject=ergebnis/json-pointer:%20Requesting%20invoice%20for%20services).
## PHP Version Support Policy
This project supports PHP versions with [active and security support](https://www.php.net/supported-versions.php).
The maintainers of this project add support for a PHP version following its initial release and drop support for a PHP version when it has reached the end of security support.
## Security Policy
This project has a [security policy](.github/SECURITY.md).
## License
This project uses the [MIT license](LICENSE.md).
## Social
Follow [@localheinz](https://twitter.com/intent/follow?screen_name=localheinz) and [@ergebnis](https://twitter.com/intent/follow?screen_name=ergebnis) on Twitter.