(value: &Handle, out: &mut String, ctx: &mut SerCtx, depth: usize)
| 54 | } |
| 55 | |
| 56 | fn 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 |
no test coverage detected