MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / from_json

Function from_json

src/lowertest/src/lib.rs:666–757  ·  view source on GitHub ↗

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.

(json: &Value, type_name: &str, rti: &ReflectedTypeInfo, ctx: &mut C)

Source from the content-addressed store, hash-verified

664/// `ctx` is responsible for converting serialized JSON to any syntax
665/// extensions or overrides.
666pub fn from_json<C>(json: &Value, type_name: &str, rti: &ReflectedTypeInfo, ctx: &mut C) -> String
667where
668 C: TestDeserializeContext,
669{
670 let (type_name, option_found) = normalize_type_name(type_name);
671 // If type is `Option<T>`, convert the value to "null" if it is null,
672 // otherwise, try to convert it to a spec corresponding to an object of
673 // type `T`.
674 if option_found {
675 if let Value::Null = json {
676 return "null".to_string();
677 }
678 }
679 if let Some(result) = ctx.reverse_syntax_override(json, &type_name) {
680 return result;
681 }
682 if let Some((names, types)) = rti.struct_dict.get(&type_name[..]) {
683 if types.is_empty() {
684 "".to_string()
685 } else {
686 format!("({})", from_json_fields(json, names, types, rti, ctx))
687 }
688 } else if let Some(enum_dict) = rti.enum_dict.get(&type_name[..]) {
689 match json {
690 // A unit enum in JSON is `"variant"`. In the spec it is `variant`.
691 Value::String(s) => unquote(s),
692 // An enum with fields is `{"variant": <fields>}` in JSON. In the
693 // spec it is `(variant field1 .. fieldn).
694 Value::Object(map) => {
695 // Each enum instance only belongs to one variant.
696 assert_eq!(
697 map.len(),
698 1,
699 "Multivariant instance {:?} found for enum {}",
700 map,
701 type_name
702 );
703 for (variant, data) in map.iter() {
704 if let Some((names, types)) = enum_dict.get(&variant[..]) {
705 return format!(
706 "({} {})",
707 variant,
708 from_json_fields(data, names, types, rti, ctx)
709 );
710 }
711 }
712 unreachable!()
713 }
714 _ => unreachable!("Invalid json {:?} for enum type {}", json, type_name),
715 }
716 } else {
717 match json {
718 Value::Array(members) => {
719 let result = if type_name.starts_with("Vec<") && type_name.ends_with('>') {
720 // This is a Vec<something>.
721 members
722 .iter()
723 .map(|v| from_json(v, &type_name[4..(type_name.len() - 1)], rti, ctx))

Callers 4

serialize_genericFunction · 0.70
serializeFunction · 0.70
from_json_fieldsFunction · 0.70
read_credentialsMethod · 0.50

Calls 13

normalize_type_nameFunction · 0.85
find_next_type_in_tupleFunction · 0.85
unquoteFunction · 0.70
to_stringMethod · 0.45
getMethod · 0.45
is_emptyMethod · 0.45
iterMethod · 0.45
mapMethod · 0.45
lenMethod · 0.45
into_iterMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected