composer-php-json 0.2.0

Byte-exact PHP json_encode output for the two flag combinations Composer relies on (content-hash + JsonFile::encode).
Documentation
  • Coverage
  • 80%
    4 out of 5 items documented0 out of 2 items with examples
  • Size
  • Source code size: 22.27 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 403.45 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 6s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • cresset-tools/composer-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • pingiun

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 composer_php_json::{encode, Mode};
use serde_json::json;

// Composer's content-hash encoding: compact, escaped slashes, \uXXXX non-ASCII.
let bytes = encode(&json!({"name": "acme/widget"}), Mode::Hash);
assert_eq!(String::from_utf8(bytes).unwrap(), r#"{"name":"acme\/widget"}"#);

Modes

  • Mode::Hashjson_encode($d, 0). The byte stream MD5'd into composer.lock's content-hash. Compact, /\/, every non-ASCII code point escaped to lowercase \uXXXX (surrogate pairs above U+FFFF).
  • Mode::Prettyjson_encode($d, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), the flag set Composer\Json\JsonFile::encode uses 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.