MCPcopy Create free account
hub / github.com/dylan-sutton-chavez/edge-python / dumps

Function dumps

std/json/src/lib.rs:81–107  ·  view source on GitHub ↗
(value: Handle, kw: Kwargs)

Source from the content-addressed store, hash-verified

79
80#[plugin_fn]
81fn dumps(value: Handle, kw: Kwargs) -> Result<String> {
82 let mut opts = main::serializer::Options {
83 indent: kw.get::<i64>("indent")?,
84 sort_keys: kw.get::<bool>("sort_keys")?.unwrap_or(false),
85 ensure_ascii: kw.get::<bool>("ensure_ascii")?.unwrap_or(true),
86 check_circular: kw.get::<bool>("check_circular")?.unwrap_or(true),
87 allow_nan: kw.get::<bool>("allow_nan")?.unwrap_or(true),
88 skipkeys: kw.get::<bool>("skipkeys")?.unwrap_or(false),
89 cls: kw.get_handle("cls")?,
90 default: kw.get_handle("default")?,
91 item_sep: ",".to_string(),
92 key_sep: ":".to_string(),
93 };
94 // `separators` arrives as a 2-tuple `(item, key)`; index manually since `Kwargs::get` can't decode tuples.
95 if let Some(seps) = kw.get_handle("separators")? {
96 let zero = encode(Value::Int(0))?;
97 let one = encode(Value::Int(1))?;
98 let item = seps.get_item(&zero)?;
99 let key = seps.get_item(&one)?;
100 opts.item_sep = decode_str(&item, "separators[0]")?;
101 opts.key_sep = decode_str(&key, "separators[1]")?;
102 } else if opts.indent.is_some() {
103 // CPython: when `indent` is given and `separators` is not, default key separator becomes `": "`.
104 opts.key_sep = ": ".to_string();
105 }
106 main::serializer::serialize(&value, opts)
107}
108
109fn decode_str(h: &Handle, what: &str) -> Result<String> {
110 match decode(h.raw())? {

Callers

nothing calls this directly

Calls 5

decode_strFunction · 0.85
serializeFunction · 0.85
get_handleMethod · 0.80
encodeFunction · 0.50
get_itemMethod · 0.45

Tested by

no test coverage detected