pub fn from_json<C>(json: &Value, type_name: &str, rti: &ReflectedTypeInfo, ctx: &mut C) -> String
where
    C: TestDeserializeContext,
{
    let type_name = normalize_type_name(type_name);
    if let Some(result) = ctx.reverse_syntax_override(json, &type_name, rti) {
        return result;
    }
    if let Some((names, types)) = rti.struct_dict.get(&type_name[..]) {
        if types.is_empty() {
            "".to_string()
        } else {
            format!("({})", from_json_fields(json, names, types, rti, ctx))
        }
    } else if let Some(enum_dict) = rti.enum_dict.get(&type_name[..]) {
        match json {
            // A unit enum in JSON is `"variant"`. In the spec it is `variant`.
            Value::String(s) => unquote(s),
            // An enum with fields is `{"variant": <fields>}` in JSON. In the
            // spec it is `(variant field1 .. fieldn).
            Value::Object(map) => {
                // Each enum instance only belongs to one variant.
                assert_eq!(
                    map.len(),
                    1,
                    "Multivariant instance {:?} found for enum {}",
                    map,
                    type_name
                );
                for (variant, data) in map.iter() {
                    if let Some((names, types)) = enum_dict.get(&variant[..]) {
                        return format!(
                            "({} {})",
                            variant.to_string(),
                            from_json_fields(data, names, types, rti, ctx)
                        );
                    }
                }
                unreachable!()
            }
            _ => unreachable!("Invalid json {:?} for enum type {}", json, type_name),
        }
    } else {
        match json {
            Value::Array(members) => {
                let result = if type_name.starts_with("Vec<") && type_name.ends_with('>') {
                    // This is a Vec<something>.
                    members
                        .iter()
                        .map(|v| from_json(v, &type_name[4..(type_name.len() - 1)], rti, ctx))
                        .collect::<Vec<_>>()
                } else {
                    // This is a tuple.
                    let mut result = Vec::new();
                    let type_name = &type_name[1..(type_name.len() - 1)];
                    let mut prev_elem_end = 0;
                    let mut members_iter = members.into_iter();
                    while let Some((next_elem_begin, next_elem_end)) =
                        find_next_type_in_tuple(type_name, prev_elem_end)
                    {
                        match members_iter.next() {
                            Some(elem) => result.push(from_json(
                                elem,
                                &type_name[next_elem_begin..next_elem_end],
                                rti,
                                ctx,
                            )),
                            // we have reached the end of the tuple.
                            None => break,
                        }
                        prev_elem_end = next_elem_end;
                    }
                    result
                };
                // The spec for both is `[elem1 .. elemn]`
                format!("[{}]", separated(" ", result))
            }
            Value::Object(map) => {
                unreachable!("Invalid map {:?} found for type {}", map, type_name)
            }
            other => other.to_string(),
        }
    }
}
/// Converts serialized JSON to the syntax that [to_json] handles.
///
/// `json` is assumed to have been produced by serializing an object of type
/// `type_name`.
/// `ctx` is responsible for converting serialized JSON to any syntax
/// extensions or overrides.
Converts serialized JSON to the syntax that [to_json] handles.
`json` is assumed to have been produced by serializing an object of type
`type_name`.
`ctx` is responsible for converting serialized JSON to any syntax
extensions or overrides.
Converts serialized JSON to the syntax that [ to_json ] handles . ` json ` is assumed to have been produced by serializing an object of type ` type_name ` . ` ctx ` is responsible for converting serialized JSON to any syntax extensions or overrides .
pub fn from_json<C>(json: &Value, type_name: &str, rti: &ReflectedTypeInfo, ctx: &mut C) -> String
where
    C: TestDeserializeContext,
{
    let type_name = normalize_type_name(type_name);
    if let Some(result) = ctx.reverse_syntax_override(json, &type_name, rti) {
        return result;
    }
    if let Some((names, types)) = rti.struct_dict.get(&type_name[..]) {
        if types.is_empty() {
            "".to_string()
        } else {
            format!("({})", from_json_fields(json, names, types, rti, ctx))
        }
    } else if let Some(enum_dict) = rti.enum_dict.get(&type_name[..]) {
        match json {
            // A unit enum in JSON is `"variant"`. In the spec it is `variant`.
            Value::String(s) => unquote(s),
            // An enum with fields is `{"variant": <fields>}` in JSON. In the
            // spec it is `(variant field1 .. fieldn).
            Value::Object(map) => {
                // Each enum instance only belongs to one variant.
                assert_eq!(
                    map.len(),
                    1,
                    "Multivariant instance {:?} found for enum {}",
                    map,
                    type_name
                );
                for (variant, data) in map.iter() {
                    if let Some((names, types)) = enum_dict.get(&variant[..]) {
                        return format!(
                            "({} {})",
                            variant.to_string(),
                            from_json_fields(data, names, types, rti, ctx)
                        );
                    }
                }
                unreachable!()
            }
            _ => unreachable!("Invalid json {:?} for enum type {}", json, type_name),
        }
    } else {
        match json {
            Value::Array(members) => {
                let result = if type_name.starts_with("Vec<") && type_name.ends_with('>') {
                    // This is a Vec<something>.
                    members
                        .iter()
                        .map(|v| from_json(v, &type_name[4..(type_name.len() - 1)], rti, ctx))
                        .collect::<Vec<_>>()
                } else {
                    // This is a tuple.
                    let mut result = Vec::new();
                    let type_name = &type_name[1..(type_name.len() - 1)];
                    let mut prev_elem_end = 0;
                    let mut members_iter = members.into_iter();
                    while let Some((next_elem_begin, next_elem_end)) =
                        find_next_type_in_tuple(type_name, prev_elem_end)
                    {
                        match members_iter.next() {
                            Some(elem) => result.push(from_json(
                                elem,
                                &type_name[next_elem_begin..next_elem_end],
                                rti,
                                ctx,
                            )),
                            // we have reached the end of the tuple.
                            None => break,
                        }
                        prev_elem_end = next_elem_end;
                    }
                    result
                };
                // The spec for both is `[elem1 .. elemn]`
                format!("[{}]", separated(" ", result))
            }
            Value::Object(map) => {
                unreachable!("Invalid map {:?} found for type {}", map, type_name)
            }
            other => other.to_string(),
        }
    }
}
pub fn from_json < C > ( json : & Value , type_name : & str , rti : & ReflectedTypeInfo , ctx : & mut C ) -> String where C : TestDeserializeContext , { let type_name = normalize_type_name ( type_name ) ; if let Some ( result ) = ctx . reverse_syntax_override ( json , & type_name , rti ) { return result ; } if let Some ( ( names , types ) ) = rti . struct_dict . get ( & type_name [ .. ] ) { if types . is_empty ( ) { " " . to_string ( ) } else { format ! ( " " from_json_fields ( json names types rti ctx ) ) } } else if let Some ( enum_dict ) = rti . enum_dict . get ( & type_name [ .. ] ) { match json { // A unit enum in JSON is `"variant"`. In the spec it is `variant`. Value :: String ( s ) => unquote ( s ) , // An enum with fields is `{"variant": <fields>}` in JSON. In the // spec it is `(variant field1 .. fieldn). Value :: Object ( map ) => { // Each enum instance only belongs to one variant. assert_eq ! ( map len ( ) 1 " " map type_name ) ; for ( variant , data ) in map . iter ( ) { if let Some ( ( names , types ) ) = enum_dict . get ( & variant [ .. ] ) { return format ! ( " " variant to_string ( ) from_json_fields ( data names types rti ctx ) ) ; } } unreachable ! ( ) } _ => unreachable ! ( " " json type_name ) , } } else { match json { Value :: Array ( members ) => { let result = if type_name . starts_with ( " " ) && type_name . ends_with ( '>' ) { // This is a Vec<something>. members . iter ( ) . map ( | v | from_json ( v , & type_name [ 4 .. ( type_name . len ( ) - 1 ) ] , rti , ctx ) ) . collect :: < Vec < _ > > ( ) } else { // This is a tuple. let mut result = Vec :: new ( ) ; let type_name = & type_name [ 1 .. ( type_name . len ( ) - 1 ) ] ; let mut prev_elem_end = 0 ; let mut members_iter = members . into_iter ( ) ; while let Some ( ( next_elem_begin , next_elem_end ) ) = find_next_type_in_tuple ( type_name , prev_elem_end ) { match members_iter . next ( ) { Some ( elem ) => result . push ( from_json ( elem , & type_name [ next_elem_begin .. next_elem_end ] , rti , ctx , ) ) , // we have reached the end of the tuple. None => break , } prev_elem_end = next_elem_end ; } result } ; // The spec for both is `[elem1 .. elemn]` format ! ( " " separated ( " " result ) ) } Value :: Object ( map ) => { unreachable ! ( " " map type_name ) } other => other . to_string ( ) , } } }
Converts serialized JSON to the syntax that [to_json] handles.
Converts serialized JSON to the syntax that [ to_json ] handles .
json
&Value
type_name
&str
rti
&ReflectedTypeInfo
ctx
&mut C
json
&Value
type_name
&str
rti
&ReflectedTypeInfo
ctx
&mut C