Skip to content

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 a single YAML document.

Throws a fkyaml::exception if the deserialization process detects an error from the input.

Since this function shares a large portion of internal implementation with the deserialize() function, supported unicode encodings and line break formats are the same. Refer to its documentation for details.

Template Parameters

InputType

the 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 with which begin(input) and end(input) produces a valid pair of iterators
    • std::basic_string, std::array, std::string_view (with C++17 or better) and the likes.
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() and std::string::begin()
  • a pair of pointers such as ptr and ptr + len

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 objects 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"(
%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].as_str() << 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:

1
123
3.14

one
456
1.414

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].as_str() << 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:

1
123
3.14

one
456
1.414

Example (a FILE pointer)
input_multi.yaml
%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].as_str() << 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:

1
123
3.14

one
456
1.414

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::string 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].as_str() << 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:

1
123
3.14

one
456
1.414

See Also