# decamelize-keys

[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)

[![crates.io](https://img.shields.io/crates/v/decamelize-keys.svg)](https://crates.io/crates/decamelize-keys)
[![docs.rs](https://docs.rs/decamelize-keys/badge.svg)](https://docs.rs/decamelize-keys)
[![CI](https://github.com/trananhtung/decamelize-keys/actions/workflows/ci.yml/badge.svg)](https://github.com/trananhtung/decamelize-keys/actions/workflows/ci.yml)
[![license](https://img.shields.io/crates/l/decamelize-keys.svg)](#license)

**Convert the keys of a JSON object from `camelCase` to a separated lower-case form.**

`{ "fooBar": 1 }` → `{ "foo_bar": 1 }`. A faithful Rust port of the
[`decamelize-keys`](https://www.npmjs.com/package/decamelize-keys) npm package, operating on
[`serde_json::Value`] and built on the
[`decamelize`](https://crates.io/crates/decamelize) crate. It is the inverse of
[`camelcase-keys`](https://crates.io/crates/camelcase-keys).

- `deep`, custom `separator`, and `exclude` options
- Differential-tested against the reference `decamelize-keys` implementation (60k cases)

## Install

```toml
[dependencies]
decamelize-keys = "0.1"
serde_json = "1"
```

## Usage

```rust
use serde_json::json;
use decamelize_keys::{decamelize_keys, decamelize_keys_with, Options};

assert_eq!(
    decamelize_keys(&json!({ "fooBar": 1, "BazQux": 2 })),
    json!({ "foo_bar": 1, "baz_qux": 2 })
);

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

// Custom separator, or exclude keys:
assert_eq!(
    decamelize_keys_with(&json!({ "fooBar": 1 }), &Options::new().separator("-")),
    json!({ "foo-bar": 1 })
);
let _ = decamelize_keys_with(&json!({ "apiKey": "x" }), &Options::new().exclude(["apiKey"]));
```

The `exclude` option compares against the original keys.

## Contributors ✨

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the [emoji key](https://allcontributors.org/docs/en/emoji-key) for how each contribution is recognized, and open a PR or issue to get involved.

Thanks goes to these wonderful people:

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
  <tbody>
    <tr>
      <td align="center" valign="top" width="14.28%"><a href="https://github.com/trananhtung"><img src="https://avatars.githubusercontent.com/u/30992229?v=4?s=100" width="100px;" alt="Tung Tran"/><br /><sub><b>Tung Tran</b></sub></a><br /><a href="https://github.com/trananhtung/./commits?author=trananhtung" title="Code">💻</a> <a href="#maintenance-trananhtung" title="Maintenance">🚧</a></td>
    </tr>
  </tbody>
</table>

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

## License

Licensed under either of [MIT](LICENSE-MIT) or [Apache-2.0](LICENSE-APACHE) at your option.
