• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fktn-k / fkYAML / 6284412085

23 Sep 2023 03:01PM UTC coverage: 94.266%. First build
6284412085

push

github

web-flow
Merge pull request #90 from fktn-k/release/0.0.0

Release fkYAML v0.0.0

428 of 488 branches covered (0.0%)

Branch coverage included in aggregate %.

1500 of 1500 new or added lines in 7 files covered. (100.0%)

1446 of 1500 relevant lines covered (96.4%)

112.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.15
/include/fkYAML/Serializer.hpp
1
/**
2
 *   __ _  __     __      __  __ _
3
 *  / _| | \ \   / //\   |  \/  | |
4
 * | |_| | _\ \_/ //  \  | \  / | |
5
 * |  _| |/ /\   // /\ \ | |\/| | |
6
 * | | |   <  | |/ ____ \| |  | | |____
7
 * |_| |_|\_\ |_/_/    \_\_|  |_|______|
8
 *
9
 * @file Deserializer.hpp
10
 * @brief Implementation of the serializer for YAML nodes.
11
 * @version 0.0.0
12
 *
13
 * Copyright (c) 2023 fktn
14
 * Distributed under the MIT License (https://opensource.org/licenses/MIT)
15
 */
16

17
#ifndef FK_YAML_SERIALIZER_HPP_
18
#define FK_YAML_SERIALIZER_HPP_
19

20
#include <cmath>
21
#include <sstream>
22
#include <string>
23

24
#include "fkYAML/VersioningMacros.hpp"
25
#include "fkYAML/Exception.hpp"
26
#include "fkYAML/Node.hpp"
27
#include "fkYAML/NodeType.hpp"
28
#include "fkYAML/NodeTypeTraits.hpp"
29

30
FK_YAML_NAMESPACE_BEGIN
31

32
template <typename BasicNodeType = Node>
33
class BasicSerializer
34
{
35
    static_assert(IsBasicNode<BasicNodeType>::value, "BasicSerializer only accepts (const) BasicNode<...>");
36

37
public:
38
    /**
39
     * @brief Construct a new BasicSerializer object.
40
     */
41
    BasicSerializer() = default;
42

43
    std::string Serialize(BasicNodeType& node)
16✔
44
    {
45
        std::string str {};
16✔
46
        SerializeNode(node, 0, str);
16✔
47
        return str;
16✔
48
    }
×
49

50
private:
51
    void SerializeNode(BasicNodeType& node, const uint32_t cur_indent, std::string& str)
26✔
52
    {
53
        switch (node.Type())
26✔
54
        {
55
        case NodeType::SEQUENCE:
3✔
56
            for (auto& seq_item : node)
11✔
57
            {
58
                InsertIndentation(cur_indent, str);
5✔
59
                str += "-";
5✔
60
                if (seq_item.IsScalar())
5✔
61
                {
62
                    str += " ";
4✔
63
                    SerializeNode(seq_item, cur_indent, str);
4✔
64
                    str += "\n";
4✔
65
                }
66
                else
67
                {
68
                    str += "\n";
1✔
69
                    SerializeNode(seq_item, cur_indent + 2, str);
1✔
70
                }
71
            }
72
            break;
3✔
73
        case NodeType::MAPPING:
3✔
74
            for (auto itr = node.Begin(); itr != node.End(); ++itr)
8✔
75
            {
76
                InsertIndentation(cur_indent, str);
5✔
77
                SerializeKey(itr.Key(), str);
5✔
78
                if (itr->IsScalar())
5✔
79
                {
80
                    str += " ";
4✔
81
                    SerializeNode(*itr, cur_indent, str);
4✔
82
                    str += "\n";
4✔
83
                }
84
                else
85
                {
86
                    str += "\n";
1✔
87
                    SerializeNode(*itr, cur_indent + 2, str);
1✔
88
                }
89
            }
90
            break;
3✔
91
        case NodeType::NULL_OBJECT:
3✔
92
            str += "null";
3✔
93
            break;
3✔
94
        case NodeType::BOOLEAN:
6✔
95
            if (node.ToBoolean())
6✔
96
            {
97
                str += "true";
3✔
98
            }
99
            else
100
            {
101
                str += "false";
3✔
102
            }
103
            break;
6✔
104
        case NodeType::INTEGER:
4✔
105
            str += std::to_string(node.ToInteger());
4✔
106
            break;
4✔
107
        case NodeType::FLOAT_NUMBER: {
5✔
108
            typename BasicNodeType::float_number_type float_val = node.ToFloatNumber();
5✔
109
            if (std::isnan(float_val))
5✔
110
            {
111
                str += ".nan";
1✔
112
            }
113
            else if (std::isinf(float_val))
4✔
114
            {
115
                if (float_val == std::numeric_limits<typename BasicNodeType::float_number_type>::infinity())
2✔
116
                {
117
                    str += ".inf";
1✔
118
                }
119
                else
120
                {
121
                    str += "-.inf";
1✔
122
                }
123
            }
124
            else
125
            {
126
                std::stringstream ss;
2✔
127
                ss << node.ToFloatNumber();
2✔
128
                str += ss.str();
2✔
129
            }
2✔
130
            break;
5✔
131
        }
132
        case NodeType::STRING:
2✔
133
            str += node.ToString();
2✔
134
            break;
2✔
135
        default:
×
136
            throw Exception("Unsupported node type found.");
×
137
        }
138
    }
26✔
139

140
    void SerializeKey(const std::string& key, std::string& str)
5✔
141
    {
142
        str += key + ":";
5✔
143
    }
5✔
144

145
    void InsertIndentation(const uint32_t cur_indent, std::string& str)
10✔
146
    {
147
        for (uint32_t i = 0; i < cur_indent; ++i)
18✔
148
        {
149
            str += " ";
8✔
150
        }
151
    }
10✔
152
};
153

154
using Serializer = BasicSerializer<>;
155

156
FK_YAML_NAMESPACE_END
157

158
#endif /* FK_YAML_SERIALIZER_HPP_ */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc