rapidyaml 0.16.0
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 5825 of file quickstart.cpp.

5826{
5827 ryml::csubstr json = ""
5828 "{" "\n"
5829 " \"doe\": \"a deer, a female deer\"," "\n"
5830 " \"ray\": \"a drop of golden sun\"," "\n"
5831 " \"me\": \"a name, I call myself\"," "\n"
5832 " \"far\": \"a long long way to go\"" "\n"
5833 "}" "\n"
5834 "";
5835 // Since JSON is a subset of YAML, parsing JSON is just the
5836 // same as YAML:
5837 ryml::Tree tree = ryml::parse_in_arena(json);
5838 // If you are sure the source is valid json, you can use the
5839 // appropriate parse_json overload, which is stricter and faster
5840 // because json has a much smaller grammar:
5841 ryml::Tree json_tree = ryml::parse_json_in_arena(json);
5842 // to emit JSON:
5844 CHECK(ryml::emitrs_json<std::string>(json_tree) == json);
5845 // to emit JSON to a stream:
5846 std::stringstream ss;
5847 ss << ryml::as_json(tree); // <- mark it like this
5848 CHECK(ss.str() == json);
5849 // Note the following limitations on the json emitter:
5850 //
5851 // - YAML streams are emitted as seqs by default. If you want to
5852 // flag this, and want it to be an error, there is a setting to
5853 // control this in EmitOptions.
5854 //
5855 // - YAML tags cannot be emitted as JSON, and are allowed only if the
5856 // relevant setting is enabled in EmitOptions.
5857 //
5858 // - Likewise, anchors and references cannot be emitted as JSON
5859 // and are allowed only if the relevant setting is enabled in
5860 // EmitOptions.
5861 //
5862
5863 // Note that when parsing JSON, ryml will set the style of each
5864 // node in the JSON. This means that if you emit YAML from a tree
5865 // parsed from JSON, it will look mostly the same as the original
5866 // JSON:
5867 CHECK(ryml::emitrs_yaml<std::string>(json_tree) == json);
5868 // If you want to avoid this, you will need to clear the style.
5869 json_tree.rootref().clear_style(); // clear the style of the map (without recursing)
5870 // note that this is now block mode. That is because when no
5871 // style is set, ryml defaults to emitting in block mode.
5872 CHECK(ryml::emitrs_yaml<std::string>(json_tree) == ""
5873 "\"doe\": \"a deer, a female deer\"" "\n"
5874 "\"ray\": \"a drop of golden sun\"" "\n"
5875 "\"me\": \"a name, I call myself\"" "\n"
5876 "\"far\": \"a long long way to go\"" "\n"
5877 "");
5878 // if you don't want the double quotes in the scalar, you can
5879 // recurse:
5880 json_tree.rootref().clear_style(/*recurse*/true);
5881 // so now when emitting you will get this:
5882 CHECK(ryml::emitrs_yaml<std::string>(json_tree) == ""
5883 "doe: a deer, a female deer" "\n"
5884 "ray: a drop of golden sun" "\n"
5885 "me: a name, I call myself" "\n"
5886 "far: a long long way to go" "\n"
5887 "");
5888 // see in particular sample_style() for more examples
5889}
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().