Reading YAML and JSON Strings¶
Parsing text with Node::parse()¶
Node.parse() parses YAML and JSON strings into a Node tree.
yaml_txt = "mykey: 42.0"
n = conduit.Node()
n.parse(yaml_txt,"yaml")
print(n["mykey"])
print(n.schema())
42.0
{
"mykey": {"dtype":"float64","number_of_elements": 1,"offset": 0,"stride": 8,"element_bytes": 8,"endianness": "little"}
}
json_txt = '{"mykey": 42.0}'
n = conduit.Node()
n.parse(json_txt,"json")
print(n["mykey"])
print(n.schema())
42.0
{
"mykey": {"dtype":"float64","number_of_elements": 1,"offset": 0,"stride": 8,"element_bytes": 8,"endianness": "little"}
}
The first argument is the string to parse and the second argument selects the protocol to use when parsing.
Valid Protocols: json, conduit_json, conduit_base64_json, yaml.
jsonandyamlprotocols parse pure JSON or YAML strings. For leaf nodes wide types such as int64, uint64, and float64 are inferred.
Homogeneous numeric lists are parsed as Conduit arrays.
yaml_txt = "myarray: [0.0, 10.0, 20.0, 30.0]"
n = conduit.Node()
n.parse(yaml_txt,"yaml")
print(n["myarray"])
print(n.fetch("myarray").schema())
[ 0. 10. 20. 30.]
{"dtype":"float64","number_of_elements": 4,"offset": 0,"stride": 8,"element_bytes": 8,"endianness": "little"}
conduit_jsonparses JSON with conduit data type information, allowing you to specify bitwidth style types, strides, etc.conduit_base64_jsoncombines the conduit_json protocol with an embedded base64-encoded data block
Generators¶
Using Generator instances¶
Node.parse() is sufficient for most use cases, but you can also use a Generator instance to parse JSON and YAML. Additionally, Generators can parse a conduit JSON schema and bind it to in-core data.
g = conduit.Generator("{test: {dtype: float64, value: 100.0}}",
"conduit_json")
n = conduit.Node()
g.walk(n)
print(n["test"])
print(n)
100.0
test: 100.0
Like Node::parse(), Generators can also parse pure JSON or YAML. For leaf nodes: wide types such as int64, uint64, and float64 are inferred.
g = conduit.Generator("{test: 100.0}",
"json")
n = conduit.Node()
g.walk(n)
print(n["test"])
print(n)
100.0
test: 100.0
g = conduit.Generator("test: 100.0",
"yaml")
n = conduit.Node()
g.walk(n)
print(n["test"])
print(n)
100.0
test: 100.0