composer-php-json
Byte-exact PHP json_encode output for the
two flag combinations Composer relies on — so Rust
tooling can reproduce Composer's content-hash and its composer.json /
composer.lock file formatting byte-for-byte.
use ;
use json;
// Composer's content-hash encoding: compact, escaped slashes, \uXXXX non-ASCII.
let bytes = encode;
assert_eq!;
Modes
Mode::Hash—json_encode($d, 0). The byte stream MD5'd intocomposer.lock'scontent-hash. Compact,/→\/, every non-ASCII code point escaped to lowercase\uXXXX(surrogate pairs above U+FFFF).Mode::Pretty—json_encode($d, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), the flag setComposer\Json\JsonFile::encodeuses when writing files. 4-space indent, raw/, raw UTF-8 (except U+2028 / U+2029, which Composer keeps escaped).
Object key order is preserved as-is — callers that need Composer's content-hash
must ksort the top level themselves before encoding, exactly as
Locker::getContentHash does.
Part of the composer-rs
workspace — shared Composer-domain crates for the cresset-tools family
(bougie,
sconce). Extracted from
bougie-php-json.
License
EUPL-1.2.