-
-
Save sm9cc/ff5553991626058bf7e8243a890dc0e0 to your computer and use it in GitHub Desktop.
Glaze Test
This file contains 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
BasedOnStyle: WebKit | |
AccessModifierOffset: -3 | |
AllowShortLoopsOnASingleLine: true | |
AlignConsecutiveAssignments: true | |
AlignConsecutiveMacros: true | |
AlignTrailingComments: true | |
BreakBeforeBraces: Custom | |
BreakConstructorInitializers: BeforeColon | |
BreakStringLiterals: false | |
ColumnLimit: 120 | |
ConstructorInitializerIndentWidth: 2 | |
Cpp11BracedListStyle: true | |
PointerAlignment: Left | |
FixNamespaceComments: true | |
SpaceBeforeCpp11BracedList: false | |
SpacesBeforeTrailingComments: 2 | |
Standard: Latest | |
TabWidth: 4 | |
UseTab: Never | |
This file contains 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
Checks: '-android-cloexec-fopen, | |
-cppcoreguidelines-*, | |
-fuchsia-default-arguments-calls, | |
-fuchsia-default-arguments-declarations, | |
-fuchsia-overloaded-operator, | |
-google-explicit-constructor, | |
-google-readability-function-size, | |
-google-runtime-int, | |
-google-runtime-references, | |
-hicpp-*, | |
-llvm-header-guard, | |
-llvm-include-order, | |
-llvmlibc-*, | |
-misc-*, | |
-modernize-*, | |
-readability-*, | |
-performance-*' | |
FormatStyle: 'file' | |
CheckOptions: | |
- key: hicpp-special-member-functions.AllowSoleDefaultDtor | |
value: 1 | |
HeaderFilterRegex: '.*hpp$' | |
This file contains 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
cmake_minimum_required(VERSION 3.27) | |
project(GlazeTests) | |
set(CMAKE_CXX_STANDARD 23) | |
set(CMAKE_CXX_STANDARD_REQUIRED ON) | |
option(USE_CPM "Use CPM to fetch dependencies" ON) | |
if (USE_CPM) | |
message(STATUS "Using CPM to fetch dependencies.") | |
include(cmake/CPM.cmake) # Include CPM.cmake from your project's cmake directory | |
CPMAddPackage( | |
NAME cpr | |
GITHUB_REPOSITORY libcpr/cpr | |
GIT_TAG 1.10.4 | |
) | |
CPMAddPackage( | |
NAME fmt | |
GITHUB_REPOSITORY fmtlib/fmt | |
GIT_TAG 10.1.1 | |
) | |
CPMAddPackage( | |
NAME glaze | |
GITHUB_REPOSITORY stephenberry/glaze | |
GIT_TAG 1.4.2 | |
) | |
else () | |
message(STATUS "Using system libraries to fetch dependencies.") | |
find_package(cpr REQUIRED) | |
find_package(fmt REQUIRED) | |
find_package(glaze REQUIRED) | |
endif () | |
find_program(iwyu_path NAMES include-what-you-use iwyu) | |
if (iwyu_path) | |
message(STATUS "Found include-what-you-use at ${iwyu_path}") | |
set(CMAKE_C_INCLUDE_WHAT_YOU_USE ${iwyu_path}) | |
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path}) | |
endif () | |
add_executable(GlazeTests main.cpp) | |
target_link_libraries(GlazeTests PRIVATE fmt::fmt cpr::cpr glaze::glaze) | |
This file contains 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
#include <bits/chrono.h> // for duration_cast, operator-, duration | |
#include <ctime> // for mktime, tm | |
#include <fmt/core.h> // for print | |
#include <glaze/core/common.hpp> // for format_error, object | |
#include <glaze/core/context.hpp> // for parse_error | |
#include <glaze/json/read.hpp> // for read_json | |
#include <iomanip> // for get_time, operator>> | |
#include <sstream> // for basic_istringstream, basic_istream | |
#include <stdexcept> // for runtime_error | |
#include <string> // for basic_string, char_traits, allocator | |
int getAge(const std::string& birthdateStr) { | |
std::tm birthdate = {}; | |
std::istringstream ss(birthdateStr); | |
ss >> std::get_time(&birthdate, "%d/%m/%Y"); | |
if (ss.fail()) { | |
fmt::print("Failed to parse birthdate: {}\n", birthdateStr); | |
return -1; | |
} | |
const auto birth = std::chrono::system_clock::from_time_t(std::mktime(&birthdate)); | |
const auto age_seconds = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - birth); | |
return static_cast<int>(age_seconds.count()) / (60 * 60 * 24 * 365); | |
} | |
namespace glz { | |
template <class T> struct meta; | |
} | |
struct Person { | |
std::string name; | |
int age; | |
std::string city; | |
std::string residence; | |
}; | |
template <> struct glz::meta<Person> { | |
using T = Person; | |
static constexpr auto value = | |
glz::object( | |
"name", &T::name, "full_name", &T::name, | |
"age", &T::age, "years_old", &T::age, "date_of_birth", &T::age, | |
"city", &T::city, "residence", &T::residence | |
); | |
}; | |
int main() { | |
const auto jsonDataLayout1 = R"( { | |
"name": "Jane", | |
"age": 30, | |
"city": "New York" | |
} | |
)"; | |
const auto jsonDataLayout2 = R"( { | |
"full_name": "John Doe", | |
"years_old": 25, | |
"residence": "Los Angeles" | |
} | |
)"; | |
// In this layout, we have the date of birth instead of the age. | |
// I would like to be able to map the date of birth to the int age member using the getAge function. | |
// Is this possible? | |
const auto jsonDataLayout3 = R"( { | |
"full_name": "Brian Smith", | |
"date_of_birth": "01/01/1990", | |
"residence": "San Francisco" | |
} | |
)"; | |
// In this layout, the age is represented as a string. | |
// I would like to be able to map the string age to the int age member. | |
// Is this possible? | |
const auto jsonDataLayout4 = R"( { | |
"full_name": "Peter Parker", | |
"age": "52", | |
"residence": "Chicago" | |
} | |
)"; | |
Person personLayout1; | |
Person personLayout2; | |
Person personLayout3; | |
Person personLayout4; | |
const auto ec1 = glz::read_json(personLayout1, jsonDataLayout1); | |
const auto ec2 = glz::read_json(personLayout2, jsonDataLayout2); | |
const auto ec3 = glz::read_json(personLayout3, jsonDataLayout3); | |
const auto ec4 = glz::read_json(personLayout4, jsonDataLayout4); | |
if (!ec1) { | |
fmt::print("Layout 1 - Name: {}\n", personLayout1.name); | |
fmt::print("Layout 1 - Age: {}\n", personLayout1.age); | |
fmt::print("Layout 1 - City: {}\n", personLayout1.city); | |
} else { | |
throw std::runtime_error(format_error(ec1, jsonDataLayout1)); | |
} | |
if (!ec2) { | |
fmt::print("Layout 2 - Full Name: {}\n", personLayout2.name); | |
fmt::print("Layout 2 - Age: {}\n", personLayout2.age); | |
fmt::print("Layout 2 - Residence: {}\n", personLayout2.residence); | |
} else { | |
throw std::runtime_error(format_error(ec2, jsonDataLayout2)); | |
} | |
if (!ec3) { | |
fmt::print("Layout 3 - Full Name: {}\n", personLayout3.name); | |
fmt::print("Layout 3 - Age: {}\n", personLayout3.age); | |
fmt::print("Layout 3 - Residence: {}\n", personLayout3.residence); | |
} else { | |
throw std::runtime_error(format_error(ec3, jsonDataLayout3)); | |
} | |
if (!ec4) { | |
fmt::print("Layout 4 - Full Name: {}\n", personLayout4.name); | |
fmt::print("Layout 4 - Age: {}\n", personLayout4.age); | |
fmt::print("Layout 4 - Residence: {}\n", personLayout4.residence); | |
} else { | |
throw std::runtime_error(format_error(ec4, jsonDataLayout4)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment