Skip to content

Commit

Permalink
Merge pull request #38 from injae/dev
Browse files Browse the repository at this point in the history
default dependencies change: remove fmt
  • Loading branch information
injae authored Sep 15, 2021
2 parents d82f4c2 + 8abcaf9 commit 922053b
Show file tree
Hide file tree
Showing 23 changed files with 229 additions and 123 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
ubuntu-bionic-gcc:
runs-on: ubuntu-18.04
env:
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON -DSERDEPP_USE_RAPIDJSON=ON'
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON -DSERDEPP_USE_RAPIDJSON=ON -DSERDEPP_USE_FMT=ON'
strategy:
matrix:
build-type: ['Release', 'Debug']
Expand Down Expand Up @@ -42,7 +42,7 @@ jobs:
ubuntu-bionic-llvm:
runs-on: ubuntu-18.04
env:
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON -DSERDEPP_USE_RAPIDJSON=ON'
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON -DSERDEPP_USE_RAPIDJSON=ON -DSERDEPP_USE_FMT=ON'
strategy:
matrix:
build-type: ['Release', 'Debug']
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
macos-clang:
runs-on: macos-latest
env:
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON -DSERDEPP_USE_RAPIDJSON=ON'
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON -DSERDEPP_USE_RAPIDJSON=ON -DSERDEPP_USE_FMT=ON'
strategy:
matrix:
build-type: ['Release', 'Debug']
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/window.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
windows-msvc:
runs-on: windows-2019
env:
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON -DSERDEPP_USE_RAPIDJSON=ON'
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON -DSERDEPP_USE_RAPIDJSON=ON -DSERDEPP_USE_FMT=ON'
strategy:
matrix:
build-type: ['Release', 'Debug']
Expand Down
24 changes: 19 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ set(CPPM_VERSION 0.0.13)
include(cmake/cppm_loader.cmake)
cppm_project()

project(serdepp VERSION 0.1.1 LANGUAGES C CXX)
project(serdepp VERSION 0.1.2 LANGUAGES C CXX)
cppm_setting()
cppm_cxx_standard(17)
cppm_compiler_option(DEFAULT)

find_cppkg(benchmark 1.5.2 MODULE benchmark::benchmark TYPE lib OPTIONAL OFF)
find_cppkg(RapidJSON 1.1.1 MODULE rapidjson TYPE lib OPTIONAL OFF)
find_cppkg(nlohmann_json 3.9.1 MODULE nlohmann_json::nlohmann_json TYPE lib OPTIONAL OFF)
find_cppkg(ryml 0.2.0 MODULE ryml::ryml TYPE lib OPTIONAL OFF)
find_cppkg(toml11 3.7.0 MODULE toml11::toml11 TYPE lib OPTIONAL OFF)
find_cppkg(yaml-cpp 0.6.3 MODULE yaml-cpp TYPE lib OPTIONAL OFF)
find_cppkg(nameof 0.10.0 MODULE nameof::nameof TYPE lib)
find_cppkg(fmt 7.1.3 MODULE fmt::fmt TYPE lib OPTIONAL OFF)
find_cppkg(magic_enum 0.7.3 MODULE magic_enum::magic_enum TYPE lib)
find_cppkg(fmt 7.1.3 MODULE fmt::fmt TYPE lib)
find_cppkg(nameof 0.10.0 MODULE nameof::nameof TYPE lib)


cppm_target_define(serdepp INTERFACE)
cppm_target_define(serdepp STATIC
SOURCES
src/to_static.cpp
)

cppm_examples_area()
if(SERDEPP_BUILD_EXAMPLES)
Expand Down Expand Up @@ -64,6 +68,11 @@ SOURCES
examples/sugar.cpp
)

cppm_target_define(print BINARY
SOURCES
examples/print.cpp
)

endif()

if(SERDEPP_BUILD_BENCHMARKS)
Expand All @@ -86,7 +95,7 @@ SOURCES
endif()

set(serdepp_global_deps PRIVATE benchmark
PUBLIC RapidJSON nlohmann_json toml11 yaml-cpp nameof magic_enum fmt)
PUBLIC RapidJSON nlohmann_json ryml toml11 yaml-cpp fmt magic_enum nameof)
cppm_target_dependencies(serdepp
${serdepp_global_deps})

Expand Down Expand Up @@ -122,6 +131,10 @@ cppm_target_dependencies(sugar
${serdepp_global_deps}
serdepp)

cppm_target_dependencies(print
${serdepp_global_deps}
serdepp)

cppm_target_dependencies(benchmark
${serdepp_global_deps}
serdepp)
Expand All @@ -144,4 +157,5 @@ cppm_target_install(variant_example)
cppm_target_install(parse_file_example)
cppm_target_install(struct_attribute_example)
cppm_target_install(sugar)
cppm_target_install(print)

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ fmt:{"vec: {"a", "b", "c"}", "t_enum: B"}
```
## Dependencies
- [fmt](https://github.com/fmtlib/fmt) (Auto Install)
- [nameof](https://github.com/Neargye/nameof) (Auto Install)
- [magic_enum](https://github.com/Neargye/magic_enum) (Auto Install)
- [fmt](https://github.com/fmtlib/fmt) (optional) (Install CMAKE FLAG: -DSERDEPP_USE_FMT=ON)
- [nlohmann_json](https://github.com/nlohmann/json) (optional) (Install CMAKE FLAG: -DSERDEPP_USE_NLOHMANN_JSON=ON)
- [rapidjson](https://github.com/Tencent/rapidjson) (optional) (Install CMAKE FLAG: -DSERDEPP_USE_RAPIDJSON=ON)
- [toml11](https://github.com/ToruNiina/toml11) (optional) (Install CMAKE FLAG: -DSERDEPP_USE_TOML11=ON)
Expand Down Expand Up @@ -212,7 +212,6 @@ template<typename serde_ctx>
#include <serdepp/adaptor/toml11.hpp>
#include <serdepp/adaptor/yaml-cpp.hpp>
#include <serdepp/adaptor/fmt.hpp>
#include <serdepp/ostream.hpp>

using namespace serde::ostream;

Expand Down
15 changes: 10 additions & 5 deletions cppm.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "serdepp"
version = "0.1.1"
version = "0.1.2"
description = "c++ universal serialize deserialize library like rust serde"
#tool-version = "dev"

[lib]
name = "serdepp"
#type = "static"
#source= ["src/to_static.cpp"]
type = "header-only"
type = "static"
#type = "header-only"
source= ["src/to_static.cpp"]


[[example]]
Expand Down Expand Up @@ -43,6 +43,10 @@
name = "sugar"
source = ["examples/sugar.cpp"]

[[example]]
name = "print"
source = ["examples/print.cpp"]

#[[example]]
# name = "simdjson_example"
# source = ["examples/simdjson.cpp"]
Expand All @@ -60,11 +64,12 @@
source = ["benchmark/syntax_benchmark.cpp"]

[dependencies]
fmt = {version="7.1.3", link="public"}
nameof = { version="0.10.0", link="public"}
magic_enum = { version= "0.7.3", link="public"}
fmt = {version="7.1.3", link="public", optional=true}
yaml-cpp = { version="0.6.3", link="public", optional=true}
toml11 = { version="3.7.0", link="public", optional=true}
ryml = { version= "0.2.0", link="public", optional=true}
nlohmann_json = { version="3.9.1", link="public", optional=true}
RapidJSON = { version="1.1.1", link="public", optional=true}
benchmark = { version="1.5.2", optional=true}
Expand Down
6 changes: 1 addition & 5 deletions examples/example.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#include <serdepp/serializer.hpp>
#include <serdepp/serde.hpp>
#include <serdepp/adaptor/nlohmann_json.hpp>
#include <serdepp/adaptor/toml11.hpp>
#include <serdepp/adaptor/fmt.hpp>
#include <serdepp/adaptor/yaml-cpp.hpp>
#include "serdepp/adaptor/rapidjson.hpp"
#include <serdepp/ostream.hpp>

#include <serdepp/attributes.hpp>
#include <serdepp/utility.hpp>
#include <memory>

using namespace serde::ostream;
Expand Down
3 changes: 2 additions & 1 deletion examples/example1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <serdepp/utility.hpp>
#include <serdepp/serde.hpp>
#include <serdepp/adaptor/nlohmann_json.hpp>
#include <serdepp/adaptor/fmt.hpp>

enum class t_enum { A, B };

Expand Down
3 changes: 2 additions & 1 deletion examples/flatten.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <serdepp/utility.hpp>
#include <serdepp/serde.hpp>
#include <serdepp/adaptor/fmt.hpp>
#include <serdepp/adaptor/nlohmann_json.hpp>
#include <serdepp/adaptor/rapidjson.hpp>

Expand Down
2 changes: 1 addition & 1 deletion examples/parse_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "serdepp/adaptor/rapidjson.hpp"
#include "serdepp/adaptor/toml11.hpp"
#include "serdepp/adaptor/yaml-cpp.hpp"
#include "serdepp/adaptor/fmt.hpp"

enum class tenum { INPUT, OUTPUT, INPUT_2 , OUTPUT_2 };

Expand Down Expand Up @@ -55,6 +56,5 @@ int main(int argc, char *argv[])
fmt::print("{}\n",from_rpjson);
fmt::print("{}\n",from_toml11);
fmt::print("{}\n",from_yamlpp);

return 0;
}
40 changes: 40 additions & 0 deletions examples/print.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

#include <serdepp/serde.hpp>
#include <serdepp/adaptor/nlohmann_json.hpp>

struct Object {
DERIVE_SERDE(Object,
(&Self::radius, "radius")
(&Self::width, "width")
(&Self::height, "height"))
std::optional<int> radius;
std::optional<int> width;
std::optional<int> height;
};
struct Test {
DERIVE_SERDE(Test,
(&Self::type, "type")
[attributes(flatten)]
(&Self::object, "object"))
std::string type;
Object object;
};

int main(int argc, char *argv[])
{
nlohmann::json jflat = R"([
{"type": "circle", "radius": 5},
{"type": "rectangle", "width": 6, "height": 5}
])"_json;

nlohmann::json j = R"([
{"type": "circle", "object": {"radius" : 5}},
{"type": "rectangle", "object": {"width": 6, "height": 5}}
])"_json;

auto j_flatten = serde::deserialize<std::vector<Test>>(jflat);

std::cout << serde::serialize<serde::serde_sstream>(j_flatten).str();

return 0;
}
5 changes: 1 addition & 4 deletions examples/simple_example.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#include <serdepp/serializer.hpp>
#include <serdepp/serde.hpp>
#include <serdepp/adaptor/rapidjson.hpp>
#include <serdepp/adaptor/nlohmann_json.hpp>
#include <serdepp/adaptor/toml11.hpp>
#include <serdepp/adaptor/yaml-cpp.hpp>
#include <serdepp/adaptor/fmt.hpp>
#include <serdepp/ostream.hpp>

#include <serdepp/attributes.hpp>

using namespace serde::ostream;

Expand Down
1 change: 1 addition & 0 deletions examples/struct_attribute.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "serdepp/serde.hpp"
#include "serdepp/adaptor/fmt.hpp"

namespace serde::attribute {
namespace detail {
Expand Down
1 change: 1 addition & 0 deletions examples/sugar.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "serdepp/serde.hpp"
#include "serdepp/adaptor/fmt.hpp"

namespace serde::attribute {
namespace detail {
Expand Down
1 change: 1 addition & 0 deletions examples/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <serdepp/adaptor/toml11.hpp>
#include <serdepp/adaptor/yaml-cpp.hpp>
#include <serdepp/adaptor/rapidjson.hpp>
#include <serdepp/adaptor/fmt.hpp>

enum class T { A, B };

Expand Down
71 changes: 6 additions & 65 deletions include/serdepp/adaptor/fmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,24 @@
#ifndef __SERDEPP_ADAPTOR_FMT_HPP__
#define __SERDEPP_ADAPTOR_FMT_HPP__

#include <serdepp/serializer.hpp>
#include <fmt/ranges.h>
#include <serdepp/adaptor/sstream.hpp>
#include <fmt/format.h>

namespace serde {
using namespace fmt::literals;
struct string_converter {
struct _element {
std::string_view id;
std::string value;
explicit _element(std::string_view id, const std::string& value) noexcept : id(id), value(value) {}
std::string to_str() const {
return id.empty() ? value : "{} : \"{}\""_format(id, value);
}
};
std::vector<_element> iter;
template<typename T>
string_converter& add(std::string_view key, const T& data) {
iter.push_back(_element(key, fmt::format("{}", data)));
return *this;
}
std::string to_str() const { return iter.size() == 1 ? fmt::format("{}", iter[0]) : fmt::format("{}", iter); }
};

template<> struct serde_adaptor_helper<string_converter>: derive_serde_adaptor_helper<string_converter> {
inline constexpr static bool is_null(string_converter& adaptor, std::string_view key) { return false; }
inline constexpr static size_t size(string_converter& adaptor) { return 1; }
inline constexpr static bool is_struct(string_converter& adaptor) { return true; }
};

template<typename T, typename U> struct serde_adaptor<string_converter, T, U> {
static void from(string_converter& s, std::string_view key, T& data){
throw serde::unimplemented_error(fmt::format("serde_adaptor<{}>::from(string_converter, key data)",
nameof::nameof_short_type<string_converter>()));
}
static void into(string_converter& s, std::string_view key, const T& data) { s.add(key, data); }
};

template<typename... T>
struct serde_adaptor<string_converter, std::variant<T...>> {
constexpr static void from(string_converter& s, std::string_view key, std::variant<T...>& data) {
throw serde::unimplemented_error(fmt::format("serde_adaptor<{}>::from(string_converter, key data)",
nameof::nameof_short_type<string_converter>()));
}
constexpr static void into(string_converter& s, std::string_view key, const std::variant<T...>& data) {
std::visit([&](auto& type){ serialize_to<string_converter>(type, s, key); }, data);
}
};

template<typename T>
inline std::string to_string(const T& type) { return serialize<string_converter>(type).to_str(); }
template<typename T>
inline std::string to_str(const T& type) { return serialize<string_converter>(type).to_str(); }
}
#include <fmt/ranges.h>

template <typename T>
struct fmt::formatter<T, std::enable_if_t<serde::type::is_struct_v<T>, char>>
: fmt::formatter<std::string> {
template <typename format_ctx>
auto format(const T& serde_type, format_ctx& ctx) {
return formatter<std::string>::format(serde::serialize<serde::string_converter>(serde_type).to_str(), ctx);
}
};

template<>
struct fmt::formatter<serde::string_converter> : fmt::formatter<std::string> {
template <typename format_ctx>
auto format(const serde::string_converter& serde_type, format_ctx& ctx) {
return formatter<std::string>::format(serde_type.to_str(), ctx);
return formatter<std::string>::format(serde::to_string(serde_type), ctx);
}
};

template<>
struct fmt::formatter<serde::string_converter::_element> : fmt::formatter<std::string> {
struct fmt::formatter<serde::serde_sstream> : fmt::formatter<std::string> {
template <typename format_ctx>
auto format(const serde::string_converter::_element& serde_type, format_ctx& ctx) {
return formatter<std::string>::format(serde_type.to_str(), ctx);
auto format(const serde::serde_sstream& serde_type, format_ctx& ctx) {
return formatter<std::string>::format(serde_type.str(), ctx);
}
};

Expand Down
Loading

0 comments on commit 922053b

Please sign in to comment.