@@ -155,34 +155,36 @@ PropertyTree PropertyTree::fromYAML(const YAML::Node& node)
155155 return tree;
156156}
157157
158- YAML::Node PropertyTree::toYAML () const
158+ YAML::Node PropertyTree::toYAML (bool exclude_attributes ) const
159159{
160- // Always emit a mapping if attributes exist or children exist
161- if (!attributes_.empty () || !children_.empty ())
162- {
163- YAML::Node node (YAML::NodeType::Map);
164- // emit attributes first
165- if (!attributes_.empty ())
166- {
167- YAML::Node attr_node (YAML::NodeType::Map);
168- for (const auto & pair : attributes_)
169- attr_node[pair.first ] = pair.second ;
160+ // pure leaf without attributes or children: emit scalar/sequence directly
161+ if (attributes_.empty () && children_.empty ())
162+ return value_;
170163
171- node[ATTRIBUTES_KEY] = attr_node;
172- }
173- // emit children
174- for (const auto & pair : children_)
175- node[pair.first ] = pair.second .toYAML ();
164+ // pure leaf with attributes excluded and no children: emit scalar/sequence directly
165+ if (exclude_attributes && children_.empty ())
166+ return value_;
176167
177- // if leaf (no children) but value present, emit under 'value'
178- if (children_.empty () && value_)
179- node[VALUE_KEY] = value_;
168+ // Always emit a mapping if attributes exist or children exist
169+ YAML::Node node (YAML::NodeType::Map);
170+ // emit attributes first
171+ if (!exclude_attributes && !attributes_.empty ())
172+ {
173+ YAML::Node attr_node (YAML::NodeType::Map);
174+ for (const auto & pair : attributes_)
175+ attr_node[pair.first ] = pair.second ;
180176
181- return node;
177+ node[ATTRIBUTES_KEY] = attr_node ;
182178 }
179+ // emit children
180+ for (const auto & pair : children_)
181+ node[pair.first ] = pair.second .toYAML (exclude_attributes);
182+
183+ // if leaf (no children) but value present, emit under 'value'
184+ if (children_.empty () && value_)
185+ node[VALUE_KEY] = value_;
183186
184- // pure leaf without attributes: emit scalar/sequence directly
185- return value_;
187+ return node;
186188}
187189
188190void validateRequired (const PropertyTree& node)
0 commit comments