rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches

Functions

void sample_json ()
 JSON parsing and emitting.

Detailed Description

Function Documentation

◆ sample_json()

void sample_json ( )

JSON parsing and emitting.

shows how to parse and emit JSON.

To emit YAML parsed from JSON, see also sample_style() for info on clearing the style flags (example below).

Definition at line 5741 of file quickstart.cpp.

5742{
5743 ryml::csubstr json = ""
5744 "{" "\n"
5745 " \"doe\": \"a deer, a female deer\"," "\n"
5746 " \"ray\": \"a drop of golden sun\"," "\n"
5747 " \"me\": \"a name, I call myself\"," "\n"
5748 " \"far\": \"a long long way to go\"" "\n"
5749 "}" "\n"
5750 "";
5751 // Since JSON is a subset of YAML, parsing JSON is just the
5752 // same as YAML:
5753 ryml::Tree tree = ryml::parse_in_arena(json);
5754 // If you are sure the source is valid json, you can use the
5755 // appropriate parse_json overload, which is stricter and faster
5756 // because json has a much smaller grammar:
5757 ryml::Tree json_tree = ryml::parse_json_in_arena(json);
5758 // to emit JSON:
5760 CHECK(ryml::emitrs_json<std::string>(json_tree) == json);
5761 // to emit JSON to a stream:
5762 std::stringstream ss;
5763 ss << ryml::as_json(tree); // <- mark it like this
5764 CHECK(ss.str() == json);
5765 // Note the following limitations on the json emitter:
5766 //
5767 // - YAML streams are emitted as seqs by default. If you want to
5768 // flag this, and want it to be an error, there is a setting to
5769 // control this in EmitOptions.
5770 //
5771 // - YAML tags cannot be emitted as JSON, and are allowed only if the
5772 // relevant setting is enabled in EmitOptions.
5773 //
5774 // - Likewise, anchors and references cannot be emitted as JSON
5775 // and are allowed only if the relevant setting is enabled in
5776 // EmitOptions.
5777 //
5778
5779 // Note that when parsing JSON, ryml will set the style of each
5780 // node in the JSON. This means that if you emit YAML from a tree
5781 // parsed from JSON, it will look mostly the same as the original
5782 // JSON:
5783 CHECK(ryml::emitrs_yaml<std::string>(json_tree) == json);
5784 // If you want to avoid this, you will need to clear the style.
5785 json_tree.rootref().clear_style(); // clear the style of the map (without recursing)
5786 // note that this is now block mode. That is because when no
5787 // style is set, ryml defaults to emitting in block mode.
5788 CHECK(ryml::emitrs_yaml<std::string>(json_tree) == ""
5789 "\"doe\": \"a deer, a female deer\"" "\n"
5790 "\"ray\": \"a drop of golden sun\"" "\n"
5791 "\"me\": \"a name, I call myself\"" "\n"
5792 "\"far\": \"a long long way to go\"" "\n"
5793 "");
5794 // if you don't want the double quotes in the scalar, you can
5795 // recurse:
5796 json_tree.rootref().clear_style(/*recurse*/true);
5797 // so now when emitting you will get this:
5798 CHECK(ryml::emitrs_yaml<std::string>(json_tree) == ""
5799 "doe: a deer, a female deer" "\n"
5800 "ray: a drop of golden sun" "\n"
5801 "me: a name, I call myself" "\n"
5802 "far: a long long way to go" "\n"
5803 "");
5804 // see in particular sample_style() for more examples
5805}
void clear_style(bool recurse=false)
Definition node.hpp:1305
NodeRef rootref()
Get the root as a NodeRef . Note that a non-const Tree implicitly converts to NodeRef.
Definition tree.cpp:56
substr emitrs_json(Tree const &t, id_type id, EmitOptions const &opts, CharOwningContainer *cont, bool append=false)
(1) emit+resize: emit JSON to the given std::string/std::vector<char>-like container,...
substr emitrs_yaml(Tree const &t, id_type id, EmitOptions const &opts, CharOwningContainer *cont, bool append=false)
(1) emit+resize: emit YAML to the given std::string/std::vector<char>-like container,...
void parse_in_arena(Parser *parser, csubstr filename, csubstr yaml, Tree *tree, id_type node_id)
(1) parse YAML into an existing tree node. The filename will be used in any error messages arising du...
Definition parse.cpp:209
void parse_json_in_arena(Parser *parser, csubstr filename, csubstr json, Tree *tree, id_type node_id)
(1) parse JSON into an existing tree node. The filename will be used in any error messages arising du...
Definition parse.cpp:231
#define CHECK(predicate)
a testing assertion, used only in this quickstart
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2356
tag type to mark a tree or node to be emitted as yaml when using operator<<, with options.

Referenced by main().