File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 10
10
#include < sourcemeta/core/json.h>
11
11
12
12
#include < filesystem> // std::filesystem
13
+ #include < istream> // std::basic_istream
13
14
14
15
// / @defgroup yaml YAML
15
16
// / @brief A YAML compatibility library based on `libyaml`.
22
23
23
24
namespace sourcemeta ::core {
24
25
26
+ // / @ingroup yaml
27
+ // /
28
+ // / Create a JSON document from a C++ standard input stream that represents a
29
+ // / YAML document. For example:
30
+ // /
31
+ // / ```cpp
32
+ // / #include <sourcemeta/core/json.h>
33
+ // / #include <cassert>
34
+ // / #include <sstream>
35
+ // /
36
+ // / std::istringstream stream{"foo: bar"};
37
+ // / const sourcemeta::core::JSON document =
38
+ // / sourcemeta::core::parse_yaml(stream);
39
+ // / assert(document.is_object());
40
+ // / ```
41
+ SOURCEMETA_CORE_YAML_EXPORT
42
+ auto parse_yaml (std::basic_istream<JSON::Char, JSON::CharTraits> &stream)
43
+ -> JSON;
44
+
25
45
// / @ingroup yaml
26
46
// /
27
47
// / Create a JSON document from a C++ standard input stream that represents a
Original file line number Diff line number Diff line change @@ -91,6 +91,13 @@ static auto internal_parse_json(yaml_parser_t *parser)
91
91
92
92
namespace sourcemeta ::core {
93
93
94
+ auto parse_yaml (std::basic_istream<JSON::Char, JSON::CharTraits> &stream)
95
+ -> JSON {
96
+ std::basic_ostringstream<JSON::Char, JSON::CharTraits> buffer;
97
+ buffer << stream.rdbuf ();
98
+ return parse_yaml (buffer.str ());
99
+ }
100
+
94
101
auto parse_yaml (const JSON::String &input) -> JSON {
95
102
yaml_parser_t parser;
96
103
if (!yaml_parser_initialize (&parser)) {
Original file line number Diff line number Diff line change 1
1
#include < gtest/gtest.h>
2
2
3
+ #include < sstream>
4
+
3
5
#include < sourcemeta/core/json.h>
4
6
#include < sourcemeta/core/yaml.h>
5
7
@@ -81,3 +83,16 @@ TEST(YAML_parse, file_not_exists) {
81
83
" not_exists.yaml" ),
82
84
std::filesystem::filesystem_error);
83
85
}
86
+
87
+ TEST (YAML_parse, istringstream) {
88
+ std::istringstream stream{" hello: world\n foo: 1\n bar: true" };
89
+ const auto result{sourcemeta::core::parse_yaml (stream)};
90
+
91
+ const sourcemeta::core::JSON expected = sourcemeta::core::parse_json (R"JSON( {
92
+ "hello": "world",
93
+ "foo": 1,
94
+ "bar": true
95
+ })JSON" );
96
+
97
+ EXPECT_EQ (result, expected);
98
+ }
You can’t perform that action at this time.
0 commit comments