Skip to content

Commit

Permalink
fix issue#199; update README
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbgan committed Jul 30, 2023
1 parent 2e0f8da commit 896cf64
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 19 deletions.
12 changes: 5 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,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
11 changes: 10 additions & 1 deletion iguana/yaml_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,16 @@ IGUANA_INLINE void parse_value(U &value, It &&value_begin, It &&value_end) {
++start;
--end;
if (*end != '"')
IGUANA_UNLIKELY { throw std::runtime_error(R"(Expected ")"); }
IGUANA_UNLIKELY {
// TODO: improve
auto it = start;
while (*it != '"' && it != end) {
++it;
}
if (it == end || (*(it + 1) != '#'))
IGUANA_UNLIKELY { throw std::runtime_error(R"(Expected ")"); }
end = it;
}
if constexpr (string_v<T>) {
parse_escape_str(value, start, end);
return;
Expand Down
19 changes: 10 additions & 9 deletions iguana/yaml_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace iguana {
// return true when it==end
template <typename It>
IGUANA_INLINE bool skip_space_till_end(It &&it, It &&end) {
while (it != end && *it == ' ')
while (it != end && *it < 33)
++it;
return it == end;
}
Expand All @@ -25,14 +25,15 @@ template <typename It> IGUANA_INLINE auto skip_till_newline(It &&it, It &&end) {
}
else if (*it == '#')
IGUANA_UNLIKELY {
if (*(it - 1) == ' ')
IGUANA_UNLIKELY {
// it - 1 should be legal because this func is for parse value
while ((it != end) && *it != '\n') {
++it;
}
return res;
if (*(it - 1) == ' ') {
// it - 1 should be legal because this func is for parse value
while ((it != end) && *it != '\n') {
++it;
}
return res;
} else {
++it;
}
}
else
IGUANA_LIKELY { ++it; }
Expand Down Expand Up @@ -100,7 +101,7 @@ IGUANA_INLINE size_t skip_space_and_lines(It &&it, It &&end, size_t minspaces) {
it = start;
}
}
} else if (*it == ' ') {
} else if (*it == ' ' || *it == '\t') {
++it;
++res;
} else if (*it == '#') {
Expand Down
2 changes: 1 addition & 1 deletion test/test_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ TEST_CASE("test example libraries") {
libraries:
- name:
Central Library
location: "Main\tStreet"
location: "Main\tStreet"#this is a comment
books:
- title:
categories:
Expand Down

0 comments on commit 896cf64

Please sign in to comment.