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 parse_as_double #239

Merged
merged 1 commit into from
Jan 23, 2024
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
37 changes: 21 additions & 16 deletions iguana/json_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,11 @@ IGUANA_INLINE void from_json(T &value, const Byte *data, size_t size,
}

template <bool Is_view = false, typename It>
void parse(jvalue &result, It &&it, It &&end);
void parse(jvalue &result, It &&it, It &&end, bool parse_as_double = false);

template <bool Is_view = false, typename It>
inline void parse(jarray &result, It &&it, It &&end) {
inline void parse(jarray &result, It &&it, It &&end,
bool parse_as_double = false) {
skip_ws(it, end);
match<'['>(it, end);
if (*it == ']')
Expand All @@ -652,7 +653,7 @@ inline void parse(jarray &result, It &&it, It &&end) {
}
result.emplace_back();

parse<Is_view>(result.back(), it, end);
parse<Is_view>(result.back(), it, end, parse_as_double);

if (*it == ']')
IGUANA_UNLIKELY {
Expand All @@ -666,7 +667,8 @@ inline void parse(jarray &result, It &&it, It &&end) {
}

template <bool Is_view = false, typename It>
inline void parse(jobject &result, It &&it, It &&end) {
inline void parse(jobject &result, It &&it, It &&end,
bool parse_as_double = false) {
skip_ws(it, end);
match<'{'>(it, end);
if (*it == '}')
Expand All @@ -689,7 +691,7 @@ inline void parse(jobject &result, It &&it, It &&end) {

match<':'>(it, end);

parse<Is_view>(emplaced.first->second, it, end);
parse<Is_view>(emplaced.first->second, it, end, parse_as_double);

if (*it == '}')
IGUANA_UNLIKELY {
Expand All @@ -702,7 +704,7 @@ inline void parse(jobject &result, It &&it, It &&end) {
}

template <bool Is_view, typename It>
inline void parse(jvalue &result, It &&it, It &&end) {
inline void parse(jvalue &result, It &&it, It &&end, bool parse_as_double) {
skip_ws(it, end);
switch (*it) {
case 'n':
Expand All @@ -728,7 +730,7 @@ inline void parse(jvalue &result, It &&it, It &&end) {
case '-': {
double d{};
detail::parse_item(d, it, end);
if (static_cast<int>(d) == d)
if (!parse_as_double && (static_cast<int>(d) == d))
result.emplace<int>(d);
else
result.emplace<double>(d);
Expand All @@ -746,11 +748,11 @@ inline void parse(jvalue &result, It &&it, It &&end) {
break;
case '[':
result.template emplace<jarray>();
parse<Is_view>(std::get<jarray>(result), it, end);
parse<Is_view>(std::get<jarray>(result), it, end, parse_as_double);
break;
case '{': {
result.template emplace<jobject>();
parse<Is_view>(std::get<jobject>(result), it, end);
parse<Is_view>(std::get<jobject>(result), it, end, parse_as_double);
break;
}
default:
Expand All @@ -760,11 +762,13 @@ inline void parse(jvalue &result, It &&it, It &&end) {
skip_ws(it, end);
}

// when Is_view is true, parse str as string_view
// set Is_view == true, parse str as std::string_view
// set parse_as_double == true, parse the number as double in any case
template <bool Is_view = false, typename It>
inline void parse(jvalue &result, It &&it, It &&end, std::error_code &ec) {
inline void parse(jvalue &result, It &&it, It &&end, std::error_code &ec,
bool parse_as_double = false) {
try {
parse<Is_view>(result, it, end);
parse<Is_view>(result, it, end, parse_as_double);
ec = {};
} catch (const std::runtime_error &e) {
result.template emplace<std::nullptr_t>();
Expand All @@ -774,15 +778,16 @@ inline void parse(jvalue &result, It &&it, It &&end, std::error_code &ec) {

template <bool Is_view = false, typename T, typename View,
std::enable_if_t<json_view_v<View>, int> = 0>
inline void parse(T &result, const View &view) {
parse<Is_view>(result, std::begin(view), std::end(view));
inline void parse(T &result, const View &view, bool parse_as_double = false) {
parse<Is_view>(result, std::begin(view), std::end(view), parse_as_double);
}

template <bool Is_view = false, typename T, typename View,
std::enable_if_t<json_view_v<View>, int> = 0>
inline void parse(T &result, const View &view, std::error_code &ec) noexcept {
inline void parse(T &result, const View &view, std::error_code &ec,
bool parse_as_double = false) noexcept {
try {
parse<Is_view>(result, view);
parse<Is_view>(result, view, parse_as_double);
ec = {};
} catch (std::runtime_error &e) {
ec = iguana::make_error_code(e.what());
Expand Down
1 change: 0 additions & 1 deletion iguana/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <variant>
#include <vector>

#include "detail/itoa.hpp"
#include "detail/string_stream.hpp"
#include "detail/traits.hpp"
#include "frozen/string.h"
Expand Down
24 changes: 16 additions & 8 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
#include <string>
#include <vector>
#define DOCTEST_CONFIG_IMPLEMENT
#include "doctest.h"
#include "iguana/json_reader.hpp"
#include "iguana/prettify.hpp"
#include "iguana/value.hpp"
#include <iguana/json_util.hpp>
#include <iguana/json_writer.hpp>
#include <iostream>
#include <optional>

#include "doctest.h"
#include "iguana/json_reader.hpp"
#include "iguana/prettify.hpp"
#include "iguana/value.hpp"

struct point_t {
int x;
double y;
Expand Down Expand Up @@ -362,6 +363,12 @@ TEST_CASE("test dom parse") {
CHECK(arr[1].to_string() == "go");
CHECK(arr[2].to_string() == "java");
}
{
std::string str = R"({"name": "tom", "age": 5})";
iguana::jvalue val;
iguana::parse(val, str.begin(), str.end(), true);
CHECK(5.0f == (val.to_object())["age"].to_double());
}

std::cout << "test dom parse ok\n";
}
Expand Down Expand Up @@ -676,7 +683,7 @@ TEST_CASE("test file interface") {
CHECK(obj.string == "Hello world");

fs::remove(filename);
} // namespace fs=std::filesystem;
} // namespace fs=std::filesystem;
{
fs::path p = "empty_file.bin";
std::ofstream{p};
Expand Down Expand Up @@ -776,10 +783,11 @@ TEST_CASE("check some types") {

enum class Status { STOP = 10, START };
namespace iguana {
template <> struct enum_value<Status> {
template <>
struct enum_value<Status> {
constexpr static std::array<int, 1> value = {10};
};
} // namespace iguana
} // namespace iguana
TEST_CASE("test exception") {
{
std::string str = R"({"\u8001": "name"})";
Expand Down Expand Up @@ -839,7 +847,7 @@ TEST_CASE("test exception") {
std::unordered_map<std::string, Status> mp;
CHECK_THROWS(iguana::from_json(mp, str));
}
#if defined(__clang__) || defined(_MSC_VER) || \
#if defined(__clang__) || defined(_MSC_VER) || \
(defined(__GNUC__) && __GNUC__ > 8)
{
std::unordered_map<std::string, Status> mp;
Expand Down
Loading