Skip to content
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

support yaml 17 #209

Merged
merged 4 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++20")
add_compile_options(/utf-8)
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pthread -g -std=c++20")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pthread -g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
endif(MSVC)
set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -ldl")
Expand Down Expand Up @@ -79,14 +79,11 @@ add_executable(xml_example ${XML_EXAMPLE})
add_executable(test_xml ${TEST_XML})
add_executable(xml_benchmark ${XMLBENCH})
add_executable(test_xml_nothrow ${TEST_XMLNOTHROW})
if(CMAKE_CXX_STANDARD GREATER_EQUAL 20)
add_executable(yaml_example ${YAML_EXAMPLE})
add_executable(test_yaml ${TEST_YAML})
add_executable(yaml_benchmark ${YAMLBENCH})
add_executable(test_nothrow ${TEST_NOTHROW})
add_executable(test_util ${TEST_UTIL})

endif()
add_executable(yaml_example ${YAML_EXAMPLE})
add_executable(test_yaml ${TEST_YAML})
add_executable(yaml_benchmark ${YAMLBENCH})
add_executable(test_nothrow ${TEST_NOTHROW})
add_executable(test_util ${TEST_UTIL})

# unit test
option(BUILD_UNIT_TESTS "Build unit tests" ON)
Expand All @@ -107,10 +104,8 @@ endif()
add_test(NAME test_some COMMAND test_some)
add_test(NAME test_ut COMMAND test_ut)
add_test(NAME test_json_files COMMAND test_json_files)
if(CMAKE_CXX_STANDARD GREATER_EQUAL 20)
add_test(NAME test_xml COMMAND test_xml)
add_test(NAME test_yaml COMMAND test_yaml)
add_test(NAME test_nothrow COMMAND test_nothrow)
add_test(NAME test_util COMMAND test_util)
add_test(NAME test_xml_nothrow COMMAND test_xml_nothrow)
endif()
add_test(NAME test_xml COMMAND test_xml)
add_test(NAME test_yaml COMMAND test_yaml)
add_test(NAME test_nothrow COMMAND test_nothrow)
add_test(NAME test_util COMMAND test_util)
add_test(NAME test_xml_nothrow COMMAND test_xml_nothrow)
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,57 @@ We can slove the problem1 easily with c++17:
//now you can operate the file
```

### how to handle the enum type as strings?

By default, Iguana handle enum type as number type during serialization and deserialization.
To handle the enum type as strings during serialization and deserialization with Iguana, we need to define a full specialization template in the "iguana" namespace. This template is a struct that contains an array with the underlying numbers corresponding to the enum type.
For example, if we have the following enum type:

```c++
enum class Status { STOP = 10, START };
```

And we want to handle the enum type as strings when parsing JSON:

```c++
std::string str = R"(
{
"a": "START",
"b": "STOP",
}
)";
```

To do this, we define the full specialization template in the "iguana" namespace:

```c++
namespace iguana {
template <> struct enum_value<Status> {
constexpr static std::array<int, 2> value = {10, 11};
};
} // namespace iguana
```

Once this is done, we can continue writing the rest of the code as usual.

```c++
struct enum_t {
Status a;
Status b;
};
REFLECTION(enum_t, a, b);

// deserialization
enum_t e;
iguana::from_json(e, str);
// serialization
enum_t e;
e.a = Status::START;
e.b = Status::STOP;
std::string ss;
iguana::to_json(e ss);
```

### Full sources:


Expand Down
2 changes: 1 addition & 1 deletion example/yaml_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ void library_example() {
libraries:
- name:
Central Library
location: "Main\tStreet"
location: "Main\tStreet" #this is a comment
books:
- title:
categories:
Expand Down
1 change: 1 addition & 0 deletions iguana/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "define.h"
#include "detail/charconv.h"
#include "enum_reflection.hpp"
#include "error_code.h"
#include "reflection.hpp"

#include <filesystem>
Expand Down
Loading
Loading