Skip to content

Commit

Permalink
Feat/optimize (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-para authored May 15, 2024
1 parent f17ef8e commit db365b1
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 13 deletions.
2 changes: 2 additions & 0 deletions include/common/array.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <initializer_list>
Expand Down
2 changes: 2 additions & 0 deletions include/common/exception.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <exception>
Expand Down
2 changes: 2 additions & 0 deletions include/common/object.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <initializer_list>
Expand Down
6 changes: 4 additions & 2 deletions include/common/serialization.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <type_traits>
Expand All @@ -13,8 +15,8 @@ template <typename in_t, typename serializer_t>
class is_serializable
{
template <typename U>
static auto
test(int) -> decltype(std::declval<serializer_t>()(std::declval<U>()), std::true_type());
static auto test(int)
-> decltype(std::declval<serializer_t>()(std::declval<U>()), std::true_type());

template <typename U>
static std::false_type test(...);
Expand Down
2 changes: 2 additions & 0 deletions include/common/types.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include "array.hpp"
Expand Down
30 changes: 25 additions & 5 deletions include/common/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <iomanip>
#include <limits>
#include <sstream>
#include <string>
#include <type_traits>

Expand Down Expand Up @@ -163,7 +168,7 @@ class has_from_json_in_templ_spec
};

template <typename string_t>
static constexpr string_t unescape_string(const string_t& str)
inline constexpr string_t unescape_string(const string_t& str)
{
using char_t = typename string_t::value_type;

Expand Down Expand Up @@ -211,26 +216,41 @@ static constexpr string_t unescape_string(const string_t& str)
}

template <typename string_t>
static constexpr string_t true_string()
inline constexpr string_t true_string()
{
return { 't', 'r', 'u', 'e' };
}

template <typename string_t>
static constexpr string_t false_string()
inline constexpr string_t false_string()
{
return { 'f', 'a', 'l', 's', 'e' };
}

template <typename string_t>
static constexpr string_t null_string()
inline constexpr string_t null_string()
{
return { 'n', 'u', 'l', 'l' };
}

template <typename string_t, typename any_t>
string_t to_basic_string(any_t&& arg)
inline string_t to_basic_string(any_t&& arg)
{
#ifdef MEOJSON_KEEP_FLOATING_PRECISION
using real_type = std::remove_reference_t<any_t>;
if constexpr (std::is_floating_point_v<real_type>) {
if constexpr (std::is_same_v<string_t, std::string>) {
std::ostringstream oss;
oss << std::setprecision(std::numeric_limits<real_type>::max_digits10) << arg;
return oss.str();
}
else if constexpr (std::is_same_v<string_t, std::wstring>) {
std::wostringstream oss;
oss << std::setprecision(std::numeric_limits<real_type>::max_digits10) << arg;
return oss.str();
}
}
#endif
if constexpr (std::is_same_v<string_t, std::string>) {
return std::to_string(std::forward<any_t>(arg));
}
Expand Down
14 changes: 8 additions & 6 deletions include/common/value.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <cstddef>
Expand Down Expand Up @@ -611,16 +613,16 @@ inline auto basic_value<string_t>::get_helper(
{
if constexpr (std::is_constructible_v<string_t, first_key_t>) {
return is_object() ? as_object().get_helper(
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
: default_value;
}
else if constexpr (std::is_integral_v<std::decay_t<first_key_t>>) {
return is_array() ? as_array().get_helper(
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
default_value,
std::forward<first_key_t>(first),
std::forward<rest_keys_t>(rest)...)
: default_value;
}
else {
Expand Down
4 changes: 4 additions & 0 deletions include/json.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#pragma once

// IWYU pragma: begin_exports

#include "common/serialization.hpp"
#include "common/types.hpp"
#include "parser/parser.hpp"
#include "reflection/jsonization.hpp"

// IWYU pragma: end_exports
4 changes: 4 additions & 0 deletions include/json5.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#pragma once

// IWYU pragma: begin_exports

#include "common/types.hpp"
#include "parser5/parser5.hpp"

// IWYU pragma: end_exports
2 changes: 2 additions & 0 deletions include/parser/bitops.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#if __cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
Expand Down
2 changes: 2 additions & 0 deletions include/parser/packed_bytes.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <cstdint>
Expand Down
2 changes: 2 additions & 0 deletions include/parser/packed_bytes_arm.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

// current NEON implementation doesn't outperform 64-bit scalar implementation
Expand Down
3 changes: 3 additions & 0 deletions include/parser/packed_bytes_x86.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include "packed_bytes.hpp"

#include <emmintrin.h>
Expand Down
2 changes: 2 additions & 0 deletions include/parser/parser.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <fstream>
Expand Down
2 changes: 2 additions & 0 deletions include/parser5/parser5.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json5.hpp>

#pragma once

#include <algorithm>
Expand Down
2 changes: 2 additions & 0 deletions include/parser5/unicode.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json5.hpp>

// This is a generated file. Do not edit.
#pragma once
#include <array>
Expand Down
2 changes: 2 additions & 0 deletions include/reflection/jsonization.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// IWYU pragma: private, include <meojson/json.hpp>

#pragma once

#include <string>
Expand Down
4 changes: 4 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "include_test.h"
#include "json5_test.h"
#include "precision_test.h"
#include "serializing_test.h"

int main()
Expand All @@ -23,6 +24,9 @@ int main()
std::cout << "\n*** json5_test ***\n" << std::endl;
success &= test_json5();

std::cout << "\n*** precision_test ***\n" << std::endl;
success &= precision_test();

if (!success) {
std::cout << "\n****** Test failed ******\n" << std::endl;
return -1;
Expand Down
18 changes: 18 additions & 0 deletions test/precision_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#define MEOJSON_KEEP_FLOATING_PRECISION

#include "../include/json.hpp"

#include <iomanip>
#include <iostream>

bool precision_test()
{
double value = 3.141592653589793;
json::object obj_old = json::object { { "double", value } };
std::string obj_str = obj_old.to_string();
std::cout << obj_str << std::endl;
json::object obj_new = json::parse(obj_str).value().as_object();
std::cout << "old:" << std::hexfloat << value << std::endl;
std::cout << "new:" << std::hexfloat << obj_new.at("double").as_double() << std::endl;
return obj_new.at("double").as_double() == value;
}
3 changes: 3 additions & 0 deletions test/precision_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

extern bool precision_test();

0 comments on commit db365b1

Please sign in to comment.