29#include <sptk5/Variant.h>
30#include <sptk5/xdoc/Attributes.h>
34enum class DataFormat : uint8_t
40enum class SearchMode : uint8_t
47 :
public std::enable_shared_from_this<Node>
50 using SNode = std::shared_ptr<Node>;
51 using Nodes = std::vector<SNode>;
52 using Vector = std::vector<SNode>;
53 using iterator = Nodes::iterator;
54 using const_iterator = Nodes::const_iterator;
56 enum class Type : uint8_t
70 Node(
const String& nodeName =
"", Type type = Type::Null);
72 virtual ~Node() =
default;
76 virtual void clearChildren();
83 void name(
const String& name)
98 SNode& pushNode(
const String& name, Type type = Type::Null);
127 double getNumber(
const String& name =
"")
const;
129 bool getBoolean(
const String& name =
"")
const;
131 const Nodes& nodes(
const String& name =
"")
const;
133 const Variant& getValue()
const
139 void set(
const T& value)
145 SNode& set(
const String& name,
const T& value)
147 auto& node = findOrCreate(name);
148 node->m_value = value;
149 node->type(variantTypeToNodeType(node->m_value.dataType()));
154 bool remove(
const String& name);
156 bool remove(
const SNode& node);
158 SNode& findOrCreate(
const String& name);
160 SNode findFirst(
const String& name, SearchMode searchMode = SearchMode::Recursive)
const;
167 const SNode& parent()
const
180 void load(DataFormat dataFormat,
const Buffer& data,
bool xmlKeepFormatting =
false);
182 void load(DataFormat dataFormat,
const String& data,
bool xmlKeepFormatting =
false);
184 void exportTo(DataFormat dataFormat,
Buffer& data,
bool formatted)
const;
186 void exportTo(DataFormat dataFormat, std::ostream& stream,
bool formatted)
const;
200 static void clone(
const SNode& destination,
const SNode& source);
203 SNode m_parent {
nullptr};
205 Type m_type {Type::Null};
207 Attributes m_attributes;
214using SNode = Node::SNode;
220bool isFloat(
const String& str);
226bool isInteger(
const String& str);
232bool isBoolean(
const String& str);
Definition: Variant.h:372
Definition: Attributes.h:35
SNode & pushValue(const String &name, const Variant &value, Node::Type type=Node::Type::Null)
Push named property to object.
SNode & pushValue(const Variant &value, Node::Type type=Node::Type::Null)
Push value to array.
static void importJson(const SNode &jsonElement, const sptk::Buffer &jsonStr)
Node::Vector select(const String &xpath)
Select a list of sub-nodes matching xpath.
static void clone(const SNode &destination, const SNode &source)
Perform a deep copy of the source to destination.
VariantDataType
Definition: VariantData.h:44