Skip to main content

Crate map_obj

Crate map_obj 

Source
Expand description

§map-obj — map over the keys and values of a JSON object

Transform the keys and/or values of a serde_json::Value object with a closure, optionally recursing into nested objects and arrays. A faithful Rust port of the widely-used map-obj npm package — the building block behind key transforms like camelcase-keys.

use serde_json::json;
use map_obj::{map_obj, MapEntry};

// Append `_` to every key:
let result = map_obj(&json!({ "a": 1, "b": 2 }), false, |key, value| {
    MapEntry::keep(format!("{key}_"), value.clone())
});
assert_eq!(result, json!({ "a_": 1, "b_": 2 }));

Recurse into nested objects/arrays by passing deep = true:

use serde_json::json;
use map_obj::{map_obj, MapEntry};

let result = map_obj(&json!({ "a": { "b": 1 } }), true, |key, value| {
    MapEntry::keep(key.to_uppercase(), value.clone())
});
assert_eq!(result, json!({ "A": { "B": 1 } }));

Enums§

MapEntry
The result of mapping a single (key, value) pair.

Functions§

map_obj
Map over the keys and values of object.