Defined in header <fkYAML/node.hpp>
fkyaml::basic_node::deserialize¶
template <typename InputType>
static basic_node deserialize(InputType&& input); // (1)
template <typename ItrType>
static basic_node deserialize(ItrType begin, ItrType end); // (2)
Deserializes from compatible inputs.
Note that this function deserializes only the first YAML document in the given input and ignore the rest.
Use this function when the input consists of a single YAML document or only the first needs to be deserialized.
Otherwise, use the deserialize_docs()
function instead.
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.
If an input does not start either of them, the input is considered to be encoded in the UTF-8 encoding starting with no BOM.
Furthermore, allowed encoding types differ depending on the type of input characters. If a deduced encoding type is not allowed for the type, a [fkyaml::exception] will be thrown.
type | allowed encodings |
---|---|
char |
UTF-8, UTF-16, UTF-32 |
char8_t (since C++20) |
UTF-8 |
char16_t |
UTF-16 |
char32_t |
UTF-32 |
Supported line break formats
fkYAML supports LF (Unix style) and CR+LF (Windows style) as line break formats.
Inside the deserialization processes, fkYAML normalizes them into line feeds (LF, 0x0A
).
In other words, if an input uses the Windows style as its line break format, there happens a copy of the input contents. So, the Unix style is recommended for better performance.
Currently, there is no way to restore the original line break style.
Template Parameters¶
InputType
-
the 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 with which
begin(input)
andend(input)
produces a valid pair of iterators- std::basic_string, std::array, std::string_view (with C++17 or better) and the likes.
- an
ItrType
-
Type of a compatible iterator, for instance:
- a pair of iterators which implement the LegacyInputIterator requirements, e.g., return values of
std::string::begin()
andstd::string::begin()
- a pair of pointers such as
ptr
andptr + len
- a pair of iterators which implement the LegacyInputIterator requirements, e.g., return values of
Parameters¶
input
[in]- An input source such as streams or arrays.
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
object of deserialization.
Examples¶
Example (a character array)
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
// deserialize a YAML string.
char input[] = R"(
foo: true
bar: 123
baz: 3.14
)";
fkyaml::node n = fkyaml::node::deserialize(input);
// check the deserialization result.
std::cout << n["foo"].get_value<bool>() << std::endl;
std::cout << n["bar"].get_value<std::int64_t>() << std::endl;
std::cout << std::setprecision(3) << n["baz"].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"(
foo: true
bar: 123
baz: 3.14
)";
fkyaml::node n = fkyaml::node::deserialize(s);
// check the deserialization result.
std::cout << n["foo"].get_value<bool>() << std::endl;
std::cout << n["bar"].get_value<std::int64_t>() << std::endl;
std::cout << std::setprecision(3) << n["baz"].get_value<double>() << std::endl;
return 0;
}
output:
Example (a FILE pointer)
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
#include <fkYAML/node.hpp>
int main() {
// deserialize a YAML string.
FILE* p_file = std::fopen("input.yaml", "r");
if (!p_file) {
// You must not pass a null FILE pointer.
return -1;
}
fkyaml::node n = fkyaml::node::deserialize(p_file);
std::fclose(p_file);
// check the deserialization result.
std::cout << n["foo"].get_value<bool>() << std::endl;
std::cout << n["bar"].get_value<std::int64_t>() << std::endl;
std::cout << std::setprecision(3) << n["baz"].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.
std::array<char, 14> input = {'f', 'o', 'o', ':', ' ', 't', 'r', 'u', 'e', 'd', 'u', 'm', 'm', 'y'};
fkyaml::node n = fkyaml::node::deserialize(input.begin(), input.begin() + 9);
// check the deserialization result.
std::cout << std::boolalpha << n["foo"].get_value<bool>() << std::endl;
return 0;
}
output: