Skip to content

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 input sources.
Throws a fkyaml::exception if the deserialization process detects an error from the input sources.

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 <typename InputType>
static basic_node deserialize(InputType&& input);

Template Parameters

InputType

Type of a compatible input, for instance:

  • an std::istream object
  • a FILE pointer (must not be nullptr)
  • a C-style array of characters (char, char16_t or char32_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 which begin(obj) and end(obj) produces a valid pair of iterators
    • std::basic_string, std::array, std::string_view (with C++17 or better) and the likes.

Parameters

input [in]
An input source in the YAML format.

Return Value

The resulting basic_node object deserialized from the input source.

Overload (2)

template <typename ItrType>
static basic_node deserialize(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() and std::string::end()
  • a pair of pointers such as ptr and ptr + len

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 object 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"(
    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:

1
123
3.14

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:

1
123
3.14

Example (a FILE pointer)
input.yaml
foo: true
bar: 123
baz: 3.14
#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);

    // 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:

1
123
3.14

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:

true

See Also