- How do I install ergebnis/json-pointer in a Laravel project?
- Run composer require ergebnis/json-pointer in your Laravel project. The package has no Laravel-specific dependencies and requires only the json extension, which Laravel already needs. Composer will install it alongside your existing dependencies without conflicts.
- What is a JSON Pointer and why would I use it in Laravel?
- A JSON Pointer is an RFC 6901 string that identifies a specific value inside a JSON document, such as /foo/bar/0. In Laravel you might use it for API payload validation, resolving OpenAPI paths, dynamic config overrides, or as a building block for JSON Patch operations. The package gives you immutable value objects instead of ad-hoc explode() calls.
- How do I create a JSON Pointer from a string in PHP?
- Use the package's value objects, such as ReferenceToken and JsonPointer. For a single token you can call ReferenceToken::fromString() for a plain string, fromJsonString() for an RFC 6901 JSON string, or fromUriFragmentIdentifierString() for a URI fragment. Each factory returns an immutable object with conversion methods like toString(), toJsonString(), and toUriFragmentIdentifierString().
- How does ergebnis/json-pointer handle escaping of special characters like / and ~?
- The package follows RFC 6901 escaping rules: a tilde is encoded as ~0 and a forward slash as ~1. For example, the plain token foo/9000/😆 becomes foo~19000~😆 in its JSON string form. URI fragment output additionally percent-encodes characters such as the emoji.
- Can I convert a JSON Pointer to a URI fragment identifier?
- Yes. Call toUriFragmentIdentifierString() on a ReferenceToken or JsonPointer. It applies the RFC 6901 fragment rules, including escaping and percent-encoding, so the result can be used after a # in a URI. For example, the token foo/9000/😆 becomes foo~19000~1%F0%9F%98%86.
- Does ergebnis/json-pointer include a Laravel validation rule?
- No, the package does not ship a Laravel-specific validation rule or FormRequest integration. You can use its Specification class to declaratively check whether a pointer is valid, then wrap that check in a custom Laravel validation rule. This keeps validation explicit while still benefiting from RFC 6901 compliance.
- What alternatives exist to ergebnis/json-pointer for JSON pointer handling in PHP?
- You can handle JSON pointers manually with explode(), array access, and your own escaping logic, but that is error-prone for RFC 6901 edge cases. Other options include general JSON libraries or property-access packages, but they may not focus on RFC 6901 token and URI fragment conversions. ergebnis/json-pointer is a lightweight, dependency-free abstraction specifically for this purpose.
- Is ergebnis/json-pointer compatible with Laravel 10, 11, and 12?
- Yes. The package has no Laravel-specific dependencies and supports PHP versions used by modern Laravel releases, including PHP 8.x and 8.5. It can be installed in Laravel 10, 11, and 12 projects as long as the PHP version requirement is met. Always check the package's composer.json for the exact PHP constraint before upgrading.
- How should I handle invalid JSON pointers in Laravel?
- The package provides a Specification class for declarative validation of JSON pointers. You can check a pointer against the specification before using it, or catch the package's exception when invalid input is provided. In Laravel, you might wrap that logic in a custom validation rule or a FormRequest to return a clean 422 response.
- Is ergebnis/json-pointer actively maintained and safe for production?
- Yes. The package is actively maintained with CI workflows, code coverage, and support for current PHP versions including PHP 8.5. It is lightweight, has zero external dependencies, and follows backward-compatible upgrade practices. That makes it a low-risk addition for production Laravel applications that need RFC 6901 pointer handling.