-
Notifications
You must be signed in to change notification settings - Fork 1
Enhancement: Add parsers and serializers for JSON, YAML and XML #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
405744e
Add JSON/XML/YAML parsers and serializers [WIP]
zykure 4275280
Add documentation
zykure 0a2c03c
Some cleanup
zykure fa31369
Properly parse XML text content
zykure 13f0369
Fix copyright notice in data_processors.h
zykure e472091
[Copilot] Fix JSON parser issues
zykure 56a7fe2
[Copilot] Fix YAML parser issues
zykure b59ef66
Fix more issues from Copilot review
zykure 27fde8c
[Copilot] Minor changes from code review
zykure a9477a2
Fix more issues from Copilot review
zykure 7f29d06
Use ordered map in DataTree to avoid extra sorting
zykure 7583414
Fix more issues from Copilot review
zykure File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| /** | ||
| * \file data_processors.h | ||
| * \author Jan Behrens | ||
| * \date Created on 20 November 2025 | ||
| * \brief Declaration of the data processor utility functions. | ||
| * | ||
| * \copyright Copyright 2018-2025 Deutsches Elektronen-Synchrotron (DESY), Hamburg | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Lesser General Public License as published | ||
| * by the Free Software Foundation, either version 2.1 of the license, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License | ||
| * along with this program. If not, see <https://www.gnu.or | ||
| */ | ||
|
|
||
| #ifndef GUL17_DATA_PROCESSORS_H_ | ||
| #define GUL17_DATA_PROCESSORS_H_ | ||
|
|
||
| #include "DataTree.h" | ||
|
|
||
| #include "gul17/internal.h" | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace gul17 { | ||
|
|
||
| /** | ||
| * \addtogroup data_processors_h gul17/data_processors.h | ||
| * \brief Various data processor utility functions. | ||
| * @{ | ||
| */ | ||
|
|
||
| /** | ||
| * Parse a JSON string and return the corresponding DataTree representation. | ||
| * | ||
| * The function parses the input JSON string and constructs a DataTree object | ||
| * representing the hierarchical structure and data contained in the JSON. | ||
| * Throws a std::runtime_error if the input string is not valid JSON. | ||
| * | ||
| * \code | ||
| * auto a = from_json_string(R"({"foo": "bar"})"); // a == DataTree{"foo": "bar"} | ||
| * \endcode | ||
| * | ||
| * \param data The JSON string to be parsed. | ||
| * | ||
| * \see to_json_string() | ||
| * | ||
| * \since GUL version x.y.z | ||
| */ | ||
| GUL_EXPORT | ||
| DataTree from_json_string(const std::string_view& data); | ||
|
|
||
| /** | ||
| * Serialize a DataTree object to a JSON string. | ||
| * | ||
| * The function serializes the given DataTree object into a JSON-formatted string. | ||
| * The optional \c indent parameter specifies the number of spaces to use for | ||
| * indentation in the output string (default is 0, meaning no pretty-printing). | ||
| * | ||
| * \code | ||
| * auto a = to_json_string(DataTree{"foo": "bar"}); // a == "{\"foo\": \"bar\"}" | ||
| * \endcode | ||
| * | ||
| * \param value The DataTree object to be serialized. | ||
| * | ||
| * \see from_json_string() | ||
| * | ||
| * \since GUL version x.y.z | ||
| */ | ||
| GUL_EXPORT | ||
| std::string to_json_string(const DataTree& value, size_t indent = 0); | ||
|
|
||
| /** | ||
| * Parse an XML string and return the corresponding DataTree representation. | ||
| * | ||
| * The function parses the input XML string and constructs a DataTree object | ||
| * representing the hierarchical structure and data contained in the XML. | ||
| * Throws a std::runtime_error if the input string is not valid XML. | ||
| * | ||
| * \code | ||
| * auto a = from_xml_string(R"(<root><foo>bar</foo></root>)"); // a == DataTree{"foo": "bar"} | ||
| * \endcode | ||
| * | ||
| * \param data The XML string to be parsed. | ||
| * | ||
| * \see to_xml_string() | ||
| * | ||
| * \since GUL version x.y.z | ||
| */ | ||
| GUL_EXPORT | ||
| DataTree from_xml_string(const std::string_view& data); | ||
|
|
||
| /** | ||
| * Serialize a DataTree object to an XML string. | ||
| * | ||
| * The function serializes the given DataTree object into a XML-formatted string. | ||
zykure marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * The optional \c indent parameter specifies the number of spaces to use for | ||
| * indentation in the output string (default is 0, meaning no pretty-printing). | ||
| * | ||
| * \code | ||
| * auto a = to_xml_string(DataTree{"foo": "bar"}); // a == "<root><foo>bar</foo></root>" | ||
| * \endcode | ||
| * | ||
| * \param value The DataTree object to be serialized. | ||
| * | ||
| * \see from_xml_string() | ||
| * | ||
| * \since GUL version x.y.z | ||
| */ | ||
| GUL_EXPORT | ||
| std::string to_xml_string(const DataTree& value, size_t indent = 0, | ||
| const std::string& root_tag_name = "root"); | ||
|
|
||
| /** | ||
| * Parse an YAML string and return the corresponding DataTree representation. | ||
zykure marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * The function parses the input YAML string and constructs a DataTree object | ||
| * representing the hierarchical structure and data contained in the YAML. | ||
| * Throws a std::runtime_error if the input string is not valid YAML. | ||
| * | ||
| * \code | ||
| * auto a = from_yaml_string(R"(foo: bar)"); // a == DataTree{"foo": "bar"} | ||
| * \endcode | ||
| * | ||
| * \param data The YAML string to be parsed. | ||
| * | ||
| * \see to_yaml_string() | ||
| * | ||
| * \since GUL version x.y.z | ||
| */ | ||
| GUL_EXPORT | ||
| DataTree from_yaml_string(const std::string_view& data); | ||
|
|
||
| /** | ||
| * Serialize a DataTree object to a YAML string. | ||
| * | ||
| * The function serializes the given DataTree object into a YAML-formatted string. | ||
| * The optional \c indent parameter specifies the number of spaces to use for | ||
| * indentation in the output string (default is 2). | ||
| * | ||
| * \code | ||
| * auto a = to_yaml_string(DataTree{"foo": "bar"}); // a == "foo: bar\n" | ||
| * \endcode | ||
| * | ||
| * \param value The DataTree object to be serialized. | ||
| * | ||
| * \see from_yaml_string() | ||
| * | ||
| * \since GUL version x.y.z | ||
| */ | ||
| GUL_EXPORT | ||
| std::string to_yaml_string(const DataTree& value, size_t indent = 2); | ||
|
|
||
| /// @} | ||
|
|
||
| } // namespace gul17 | ||
|
|
||
| #endif // GUL17_DATA_PROCESSORS_H_ | ||
|
|
||
| // vi:ts=4:sw=4:sts=4:et | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.