Download the PHP package ustek/hujson without Composer
On this page you can find all versions of the php package ustek/hujson. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package hujson
ustek/hujson
A PHP port of tailscale/hujson: a parser and
packer for the JWCC format — JSON With Commas and Comments (also called "human
JSON"). JWCC extends RFC 8259 JSON with line comments (// ...), block comments
(/* ... */), and trailing commas.
It parses into a lossless syntax tree: comments, whitespace, and byte offsets are all preserved, so you can read, transform, and write a document back byte-for-byte unchanged unless you deliberately modify it.
JWCC is well suited to configuration files that benefit from comments and a forgiving syntax while staying close to standard JSON. This is a general-purpose JWCC library with no runtime dependencies beyond PHP core.
Requirements
- PHP >= 8.1
- No non-core extensions (uses the bundled
jsonand PCRE-with-Unicode support only)
Installation
Quick start
The HuJSON facade covers the common whole-document operations. Each throws
Ustek\HuJSON\HuJSONException on a parse error, with a Go-compatible message such as
hujson: line 1, column 6: invalid character ',' after top-level value.
A common pattern is to feed standardize() (or minimize()) output into
json_decode():
The syntax tree
HuJSON::parse() returns a Value — an exact syntactic representation of the input.
Value exposes:
| Member | Description |
|---|---|
?string $beforeExtra, ?string $afterExtra |
Surrounding comments/whitespace (null = none) |
int $startOffset, int $endOffset |
Byte offsets of the value |
?ValueTrimmed $value |
The trimmed value: a Literal, ObjectValue, or ArrayValue |
pack(): string / (string) $v |
Serialize to HuJSON |
isStandard(): bool |
True if standard JSON (no comments, no trailing commas) |
minimize(), standardize(), format() |
In-place transforms |
find(string $pointer): ?Value |
Look up a node by JSON Pointer (RFC 6901) |
patch(string $patch): void |
Apply a JSON Patch (RFC 6902), preserving comments |
all(): Generator, range(callable): bool |
Depth-first traversal |
updateOffsets(): void |
Recompute startOffset/endOffset |
Deep-copy a value with clone before mutating if you need to keep the original:
Navigating and editing
Literals
Literal is immutable (public readonly string $bytes) with constructors and typed
accessors:
Command-line tool
The package ships a hujsonfmt binary (a port of the upstream Go command):
| Flag | Effect |
|---|---|
| (none) | Format and print to stdout |
-m |
Minify to standard JSON |
-s |
Standardize to standard JSON (preserving offsets) |
-d |
Print a unified diff instead of the result |
-l |
List files whose formatting differs |
-w |
Rewrite the file(s) in place (with a temp backup) |
With no path (or -) it reads stdin. A directory argument is walked recursively,
processing files ending in .hujson.
Go → PHP API map
Go (tailscale/hujson) |
PHP (Ustek\HuJSON) |
|---|---|
hujson.Parse |
HuJSON::parse |
hujson.Standardize / Minimize / Format |
HuJSON::standardize / minimize / format |
Value.Pack / Value.String |
Value::pack / (string) $v |
Value.Clone |
clone $value |
Value.IsStandard / Find / Patch / UpdateOffsets |
same, camelCase |
Value.All / Value.Range |
Value::all / Value::range |
hujson.Bool/String/Int/Uint/Float |
Literal::fromBool/fromString/fromInt/fromUint/fromFloat |
Literal.Bool/String/Int/Uint/Float/IsValid/Kind |
asBool/asString/asInt/asUint/asFloat/isValid/kind |
Object / Array / composite |
ObjectValue / ArrayValue / Composite |
Behaviour notes
- Errors are exceptions. Where Go returns
(originalBytes, error)on failure, this library throwsHuJSONException. Error messages match the Go wording. Object/Arrayare renamed toObjectValue/ArrayValue(PHP reserves those names).- Line comments require a terminating newline.
// fooat end of input is an error (parsing comment: unexpected EOF), exactly as in the JWCC grammar; end the source with\n. - Limitations (documented, not bugs):
Literal::fromUint/asUintare bounded by PHP's signed 64-bitint, so values abovePHP_INT_MAXare not representable; float formatting relies on PHP's defaultserialize_precision = -1for shortest round-trip output.
Development
The golden test tables in tests/fixtures/golden.json are extracted verbatim from the
upstream Go test files, so the suite runs fully offline. An optional
tests/DifferentialTest.php cross-checks a broad corpus against a Go reference oracle
for byte-identical output; it self-skips unless that oracle is present locally, so it
never runs in a normal checkout.
License
BSD-3-Clause. This is a port of tailscale/hujson; the original copyright (Tailscale Inc & AUTHORS) is retained in LICENSE.