camelcase-keys 0.1.0

Recursively convert the keys of a JSON object to camelCase. A faithful port of the camelcase-keys npm package.
Documentation
  • Coverage
  • 100%
    10 out of 10 items documented2 out of 10 items with examples
  • Size
  • Source code size: 37.19 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 444.31 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 6s Average build duration of successful builds.
  • all releases: 6s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/camelcase-keys
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

camelcase-keys

All Contributors

crates.io docs.rs CI license

Convert the keys of a JSON object to camelCase.

{ "foo_bar": 1 }{ "fooBar": 1 }. A faithful Rust port of the widely-used camelcase-keys npm package, operating on [serde_json::Value] and built on the camelcase crate.

  • deep, pascal_case, preserve_consecutive_uppercase, exclude, and stop_paths options
  • Numeric-looking keys ("0", "42", "3.14", "1e3", …) are preserved
  • Differential-tested against the reference camelcase-keys implementation (120k cases)

Install

[dependencies]
camelcase-keys = "0.1"
serde_json = "1"

Usage

use serde_json::json;
use camelcase_keys::{camelcase_keys, camelcase_keys_with, Options};

assert_eq!(
    camelcase_keys(&json!({ "foo_bar": 1, "baz-qux": 2 })),
    json!({ "fooBar": 1, "bazQux": 2 })
);

// Recurse into nested objects and arrays:
assert_eq!(
    camelcase_keys_with(&json!({ "foo_bar": { "nested_key": 1 } }), &Options::new().deep(true)),
    json!({ "fooBar": { "nestedKey": 1 } })
);

// Exclude keys, stop at paths, or produce PascalCase:
let options = Options::new().deep(true).exclude(["api_key"]).stop_paths(["a_b"]);
let _ = camelcase_keys_with(&json!({ "api_key": "x", "a_b": { "c_d": 1 } }), &options);

Notes

  • The exclude and stop_paths options compare against the original keys. stop_paths uses .-joined original keys.
  • Built-in caching aside, this port is deterministic: unlike the npm package — whose internal cache key omits preserveConsecutiveUppercase, so mixing that option for the same key in a long-lived process can return stale results — this crate always reflects the options you pass.

Contributors ✨

This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.

Thanks goes to these wonderful people:

License

Licensed under either of MIT or Apache-2.0 at your option.