Defined in header <fkYAML/node.hpp>
fkyaml::basic_node::deserialize_docs¶
template <typename InputType>
static std::vector<basic_node> deserialize_docs(InputType&& input); // (1)
template <typename ItrType>
static std::vector<basic_node> deserialize_docs(ItrType&& begin, ItrType&& end); // (2)
Deserializes from compatible inputs.
Unlike the deserialize()
function, this function deserializes all YAML documents in the input into fkyaml::basic_node
objects.
Prefer this function when your input may contain more than one YAML documents and all of them must be processed.
Otherwise, use the deserialize()
function which is optimized for processing single YAML documents.
Throws a fkyaml::exception
if the deserialization process detects an error from the input.
Supported Unicode Encodings
fkYAML supports UTF-8, UTF-16 and UTF-32 encodings for input characters.
As the YAML specification shows (here), all input streams must begin with either a byte order mark(BOM) or an ASCII character, which will allow the encoding to be deduced by the pattern of the first few bytes of the input sequence.
If an input fails to meet the above requirement, the input is interpreted as a UTF-8 encoded character sequence starting without a BOM.
If a stream with char
as a character type is used (including FILE pointers), the encoding will be automatically detected in the deserialization process, while an array/container of char16_t
and char32_t
denotes that its contents are encoded in the UTF-16BE/LE and UTF-32BE/LE format, respectively.
Furthermore, a byte order mark (BOM) can be put only at the beginning of an input sequence.
The deserialization process internally converts input characters into the UTF-8 encoded ones if they are encoded in the UTF-16 or UTF-32 format.
Supported newline codes
fkYAML supports LF (Unix style) and CR+LF (Windows style) as line break formats.
Inside the deserialization processes, however, fkYAML normalizes them into line feeds (LF, 0x0A
) just as described in the YAML specification (see the "5.4. Line Break Characters" section).
Currently, there is no way to restore the original line break style in the serialization processes.
Overload (1)¶
Template Parameters¶
InputType
-
Type of a compatible input, for instance:
- an
std::istream
object - a
FILE
pointer (must not benullptr
) - a C-style array of characters (
char
,char16_t
orchar32_t
. See the "Supported Unicode Encodings" above.)- char[N], char16_t[N], or char32_t[N] (N is the size of an array)
- a container
obj
with whichbegin(obj)
andend(obj)
produces a valid pair of iterators- std::basic_string, std::array, std::string_view (with C++17 or better) and the likes.
- an
Parameters¶
input
[in]- An input source in the YAML format.
Return Value¶
The resulting basic_node
objects deserialized from the input source.
Overload (2)¶
template <typename ItrType>
static std::vector<basic_node> deserialize_docs(ItrType&& begin, ItrType&& end);
Template Parameters¶
ItrType
-
Type of a compatible iterator, for instance:
- a pair of iterators such as return values of
std::string::begin()
andstd::string::end()
- a pair of pointers such as
ptr
andptr + len
- a pair of iterators such as return values of
Parameters¶
begin
[in]- An iterator to the first element of an input sequence
end
[in]- An iterator to the past-the-last element of an input sequence
Return Value¶
The resulting basic_node
objects deserialized from the pair of iterators.
Examples¶
Example (a character array)
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
// deserialize a YAML string.
char input[] = R"(
%YAML 1.2
---
foo: true
bar: 123
baz: 3.14
...
%TAG ! tag:test.com,2000:
---
null: one
false: 456
TRUE: 1.414
)";
std::vector<fkyaml::node> docs = fkyaml::node::deserialize_docs(input);
// check the deserialization result.
std::cout << docs[0]["foo"].get_value<bool>() << std::endl;
std::cout << docs[0]["bar"].get_value<std::int64_t>() << std::endl;
std::cout << std::setprecision(3) << docs[0]["baz"].get_value<double>() << std::endl;
std::cout << std::endl;
std::cout << docs[1][nullptr].get_value_ref<std::string&>() << std::endl;
std::cout << docs[1][false].get_value<std::int64_t>() << std::endl;
std::cout << std::setprecision(4) << docs[1][true].get_value<double>() << std::endl;
return 0;
}
output:
Example (a std::string object)
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
#include <fkYAML/node.hpp>
int main() {
// deserialize a YAML string.
std::string s = R"(
%YAML 1.2
---
foo: true
bar: 123
baz: 3.14
...
%TAG ! tag:test.com,2000:
---
null: one
false: 456
TRUE: 1.414
)";
std::vector<fkyaml::node> docs = fkyaml::node::deserialize_docs(s);
// check the deserialization result.
std::cout << docs[0]["foo"].get_value<bool>() << std::endl;
std::cout << docs[0]["bar"].get_value<std::int64_t>() << std::endl;
std::cout << std::setprecision(3) << docs[0]["baz"].get_value<double>() << std::endl;
std::cout << std::endl;
std::cout << docs[1][nullptr].get_value_ref<std::string&>() << std::endl;
std::cout << docs[1][false].get_value<std::int64_t>() << std::endl;
std::cout << std::setprecision(4) << docs[1][true].get_value<double>() << std::endl;
return 0;
}
output:
Example (a FILE pointer)
%YAML 1.2
---
foo: true
bar: 123
baz: 3.14
...
%TAG ! tag:test.com,2000:
---
null: one
false: 456
TRUE: 1.414
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
#include <fkYAML/node.hpp>
int main() {
// deserialize a YAML string.
FILE* p_file = std::fopen("input_multi.yaml", "r");
if (!p_file) {
// You must not pass a null FILE pointer.
return -1;
}
std::vector<fkyaml::node> docs = fkyaml::node::deserialize_docs(p_file);
std::fclose(p_file);
// check the deserialization result.
std::cout << docs[0]["foo"].get_value<bool>() << std::endl;
std::cout << docs[0]["bar"].get_value<std::int64_t>() << std::endl;
std::cout << std::setprecision(3) << docs[0]["baz"].get_value<double>() << std::endl;
std::cout << std::endl;
std::cout << docs[1][nullptr].get_value_ref<std::string&>() << std::endl;
std::cout << docs[1][false].get_value<std::int64_t>() << std::endl;
std::cout << std::setprecision(4) << docs[1][true].get_value<double>() << std::endl;
return 0;
}
output:
Example (a pair of iterators)
#include <array>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
#include <fkYAML/node.hpp>
int main() {
// deserialize a YAML string.
char input[] = R"(
%YAML 1.2
---
foo: true
bar: 123
baz: 3.14
...
%TAG ! tag:test.com,2000:
---
null: one
false: 456
TRUE: 1.414
)";
std::vector<fkyaml::node> docs = fkyaml::node::deserialize_docs(std::begin(input), std::end(input));
// check the deserialization result.
std::cout << docs[0]["foo"].get_value<bool>() << std::endl;
std::cout << docs[0]["bar"].get_value<std::int64_t>() << std::endl;
std::cout << std::setprecision(3) << docs[0]["baz"].get_value<double>() << std::endl;
std::cout << std::endl;
std::cout << docs[1][nullptr].get_value_ref<std::string&>() << std::endl;
std::cout << docs[1][false].get_value<std::int64_t>() << std::endl;
std::cout << std::setprecision(4) << docs[1][true].get_value<double>() << std::endl;
return 0;
}
output: