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

Function serialize_into

std/json/src/main/serializer.rs:56–112  ·  view source on GitHub ↗
(value: &Handle, out: &mut String, ctx: &mut SerCtx, depth: usize)

Source from the content-addressed store, hash-verified

54}
55
56fn serialize_into(value: &Handle, out: &mut String, ctx: &mut SerCtx, depth: usize) -> Result<()> {
57 let ty_handle = value.type_of()?;
58 let ty = String::from_handle(ty_handle.raw())?;
59 match ty.as_str() {
60 "NoneType" => { out.push_str("null"); Ok(()) }
61 "bool" => {
62 match decode(value.raw())? {
63 Value::Bool(true) => out.push_str("true"),
64 Value::Bool(false) => out.push_str("false"),
65 _ => return Err(Error::Type("bool decoded as non-bool".into())),
66 }
67 Ok(())
68 }
69 "int" => {
70 match decode(value.raw())? {
71 Value::Int(i) => out.push_str(&format!("{}", i)),
72 _ => return Err(Error::Type("int decoded as non-int".into())),
73 }
74 Ok(())
75 }
76 "float" => {
77 match decode(value.raw())? {
78 Value::Float(f) => {
79 if !f.is_finite() {
80 if !ctx.opts.allow_nan {
81 return Err(Error::Value("Out of range float values are not JSON compliant".into()));
82 }
83 out.push_str(if f.is_nan() { "NaN" } else if f > 0.0 { "Infinity" } else { "-Infinity" });
84 } else {
85 out.push_str(&format_float(f));
86 }
87 }
88 _ => return Err(Error::Type("float decoded as non-float".into())),
89 }
90 Ok(())
91 }
92 "str" => {
93 match decode(value.raw())? {
94 Value::Bytes(b) => {
95 let s = String::from_utf8(b).map_err(|e| Error::Value(format!("invalid utf-8 in str: {}", e)))?;
96 escape_string(&s, out, ctx.opts.ensure_ascii);
97 }
98 _ => return Err(Error::Type("str decoded as non-bytes".into())),
99 }
100 Ok(())
101 }
102 "list" | "tuple" | "set" | "frozenset" => serialize_sequence(value, out, ctx, depth),
103 "dict" => serialize_object(value, out, ctx, depth),
104 other => {
105 if let Some(default) = &ctx.opts.default {
106 let replacement = default.call("__call__", &[value.raw()])?;
107 return serialize_into(&replacement, out, ctx, depth);
108 }
109 Err(Error::Type(format!("'{}' is not JSON-serializable", other)))
110 }
111 }
112}
113

Callers 3

serializeFunction · 0.85
serialize_sequenceFunction · 0.85
serialize_objectFunction · 0.85

Calls 10

escape_stringFunction · 0.85
serialize_sequenceFunction · 0.85
serialize_objectFunction · 0.85
type_ofMethod · 0.80
format_floatFunction · 0.70
decodeFunction · 0.50
ValueEnum · 0.50
rawMethod · 0.45
as_strMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected