Skip to content

Commit

Permalink
fix: to_wstring for json::wvalue and remove unavailable json value type
Browse files Browse the repository at this point in the history
and add wstring testing
  • Loading branch information
MistEO committed Oct 26, 2023
1 parent ac2f009 commit 17cb823
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 69 deletions.
87 changes: 26 additions & 61 deletions include/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ using wvalue = basic_value<std::wstring>;
using warray = basic_array<std::wstring>;
using wobject = basic_object<std::wstring>;

using u16value = basic_value<std::u16string>;
using u16array = basic_array<std::u16string>;
using u16object = basic_object<std::u16string>;

using u32value = basic_value<std::u32string>;
using u32array = basic_array<std::u32string>;
using u32object = basic_object<std::u32string>;

// *********************************
// * basic_value declare *
// *********************************
Expand Down Expand Up @@ -569,23 +561,15 @@ namespace literals
{
value operator""_json(const char* str, size_t len);
wvalue operator""_json(const wchar_t* str, size_t len);
u16value operator""_json(const char16_t* str, size_t len);
u32value operator""_json(const char32_t* str, size_t len);

value operator""_jvalue(const char* str, size_t len);
wvalue operator""_jvalue(const wchar_t* str, size_t len);
u16value operator""_jvalue(const char16_t* str, size_t len);
u32value operator""_jvalue(const char32_t* str, size_t len);

array operator""_jarray(const char* str, size_t len);
warray operator""_jarray(const wchar_t* str, size_t len);
u16array operator""_jarray(const char16_t* str, size_t len);
u32array operator""_jarray(const char32_t* str, size_t len);

object operator""_jobject(const char* str, size_t len);
wobject operator""_jobject(const wchar_t* str, size_t len);
u16object operator""_jobject(const char16_t* str, size_t len);
u32object operator""_jobject(const char32_t* str, size_t len);
}

template <typename string_t = default_string_t>
Expand Down Expand Up @@ -622,6 +606,20 @@ static constexpr string_t null_string()
return { 'n', 'u', 'l', 'l' };
}

template <typename string_t, typename any_t>
inline string_t to_template_string(any_t&& arg)
{
if constexpr (std::is_same_v<string_t, std::string>) {
return std::to_string(std::forward<any_t>(arg));
}
else if constexpr (std::is_same_v<string_t, std::wstring>) {
return std::to_wstring(std::forward<any_t>(arg));
}
else {
static_assert(!sizeof(any_t), "Unsupported type");
}
}

template <typename string_t>
static constexpr string_t unescape_string(const string_t& str);

Expand All @@ -642,45 +640,48 @@ MEOJSON_INLINE basic_value<string_t>::basic_value(bool b)
{}

template <typename string_t>
MEOJSON_INLINE basic_value<string_t>::basic_value(int num) : _type(value_type::number), _raw_data(std::to_string(num))
MEOJSON_INLINE basic_value<string_t>::basic_value(int num)
: _type(value_type::number), _raw_data(to_template_string<string_t>(num))
{}

template <typename string_t>
MEOJSON_INLINE basic_value<string_t>::basic_value(unsigned num)
: _type(value_type::number), _raw_data(std::to_string(num))
: _type(value_type::number), _raw_data(to_template_string<string_t>(num))
{}

template <typename string_t>
MEOJSON_INLINE basic_value<string_t>::basic_value(long num) : _type(value_type::number), _raw_data(std::to_string(num))
MEOJSON_INLINE basic_value<string_t>::basic_value(long num)
: _type(value_type::number), _raw_data(to_template_string<string_t>(num))
{}

template <typename string_t>
MEOJSON_INLINE basic_value<string_t>::basic_value(unsigned long num)
: _type(value_type::number), _raw_data(std::to_string(num))
: _type(value_type::number), _raw_data(to_template_string<string_t>(num))
{}

template <typename string_t>
MEOJSON_INLINE basic_value<string_t>::basic_value(long long num)
: _type(value_type::number), _raw_data(std::to_string(num))
: _type(value_type::number), _raw_data(to_template_string<string_t>(num))
{}

template <typename string_t>
MEOJSON_INLINE basic_value<string_t>::basic_value(unsigned long long num)
: _type(value_type::number), _raw_data(std::to_string(num))
: _type(value_type::number), _raw_data(to_template_string<string_t>(num))
{}

template <typename string_t>
MEOJSON_INLINE basic_value<string_t>::basic_value(float num) : _type(value_type::number), _raw_data(std::to_string(num))
MEOJSON_INLINE basic_value<string_t>::basic_value(float num)
: _type(value_type::number), _raw_data(to_template_string<string_t>(num))
{}

template <typename string_t>
MEOJSON_INLINE basic_value<string_t>::basic_value(double num)
: _type(value_type::number), _raw_data(std::to_string(num))
: _type(value_type::number), _raw_data(to_template_string<string_t>(num))
{}

template <typename string_t>
MEOJSON_INLINE basic_value<string_t>::basic_value(long double num)
: _type(value_type::number), _raw_data(std::to_string(num))
: _type(value_type::number), _raw_data(to_template_string<string_t>(num))
{}

template <typename string_t>
Expand Down Expand Up @@ -2418,14 +2419,6 @@ namespace literals
{
return operator""_jvalue(str, len);
}
MEOJSON_INLINE u16value operator""_json(const char16_t* str, size_t len)
{
return operator""_jvalue(str, len);
}
MEOJSON_INLINE u32value operator""_json(const char32_t* str, size_t len)
{
return operator""_jvalue(str, len);
}

MEOJSON_INLINE value operator""_jvalue(const char* str, size_t len)
{
Expand All @@ -2435,14 +2428,6 @@ namespace literals
{
return parse(std::wstring_view(str, len)).value_or(wvalue());
}
MEOJSON_INLINE u16value operator""_jvalue(const char16_t* str, size_t len)
{
return parse(std::u16string_view(str, len)).value_or(u16value());
}
MEOJSON_INLINE u32value operator""_jvalue(const char32_t* str, size_t len)
{
return parse(std::u32string_view(str, len)).value_or(u32value());
}

MEOJSON_INLINE array operator""_jarray(const char* str, size_t len)
{
Expand All @@ -2454,16 +2439,6 @@ namespace literals
auto val = parse(std::wstring_view(str, len)).value_or(wvalue());
return val.is_array() ? val.as_array() : warray();
}
MEOJSON_INLINE u16array operator""_jarray(const char16_t* str, size_t len)
{
auto val = parse(std::u16string_view(str, len)).value_or(u16value());
return val.is_array() ? val.as_array() : u16array();
}
MEOJSON_INLINE u32array operator""_jarray(const char32_t* str, size_t len)
{
auto val = parse(std::u32string_view(str, len)).value_or(u32value());
return val.is_array() ? val.as_array() : u32array();
}

MEOJSON_INLINE object operator""_jobject(const char* str, size_t len)
{
Expand All @@ -2475,16 +2450,6 @@ namespace literals
auto val = parse(std::wstring_view(str, len)).value_or(wvalue());
return val.is_object() ? val.as_object() : wobject();
}
MEOJSON_INLINE u16object operator""_jobject(const char16_t* str, size_t len)
{
auto val = parse(std::u16string_view(str, len)).value_or(u16value());
return val.is_object() ? val.as_object() : u16object();
}
MEOJSON_INLINE u32object operator""_jobject(const char32_t* str, size_t len)
{
auto val = parse(std::u32string_view(str, len)).value_or(u32value());
return val.is_object() ? val.as_object() : u32object();
}
} // namespace literals

template <typename string_t>
Expand Down
102 changes: 94 additions & 8 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,30 @@

#include "json.hpp"

bool serializing();
bool wstring_serializing();

int main()
{
std::cout << "\n****** Testing ******\n" << std::endl;

std::cout << "\n*** serializing ***\n" << std::endl;
bool success = serializing();

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

if (!success) {
std::cout << "\n****** Test failed ******\n" << std::endl;
return -1;
}

std::cout << "\n****** Test passed ******\n" << std::endl;
return 0;
}

bool serializing()
{
json::value root;

root["hello"] = "meojson";
Expand Down Expand Up @@ -66,33 +86,99 @@ int main()
if (root["more_complex"].to_string() !=
R"({"[1,2,3]":{"1":[0.100000,0.200000],"2":[0.200000,0.300000]},"[4,5,6]":{"3":[0.400000,0.000000],"4":[0.500000,0.600000]}})") {
std::cerr << "error: " << root["more_complex"].to_string() << std::endl;
return -1;
return false;
}

root["a\\n"] = "1a\\n";
root["a\n"] = "2a\n";
if (root["a\\n"].as_string() != "1a\\n") {
std::cerr << "error: " << root["a\\n"].as_string() << std::endl;
return -1;
return false;
}
if (root["a\n"].as_string() != "2a\n") {
std::cerr << "error: " << root["a\n"].as_string() << std::endl;
return -1;
return false;
}
if (root["a\\n"].to_string() != "\"1a\\\\n\"") {
std::cerr << "error: " << root["a\\n"].to_string() << std::endl;
return -1;
return false;
}
if (root["a\n"].to_string() != "\"2a\\n\"") {
std::cerr << "error: " << root["a\n"].to_string() << std::endl;
return -1;
return false;
}

std::cout << root << std::endl;
std::ofstream ofs("test.json");
std::ofstream ofs("serializing.json");
ofs << root;
ofs.close();

std::cout << "\n****** Test passed ******\n" << std::endl;
return 0;
return true;
}

bool wstring_serializing()
{
json::wvalue root;

root[L"hello"] = L"meojson";
root[L"Pi"] = 3.1416;

root[L"obj"] = {
{ L"obj_key1", L"Hi" },
{ L"obj_key2", 123 },
{ L"obj_key3", true },
};
root[L"obj"].emplace(L"obj_key4", 789);

root[L"obj"].emplace(L"obj_key5", json::wobject { { L"key4 child", L"i am object value" } });
root[L"another_obj"][L"child"][L"grand"] = L"i am grand";

// take union
root[L"obj"] |= json::wobject {
{ L"obj_key6", L"i am string" },
{ L"obj_key7", json::warray { L"i", L"am", L"array" } },
};

root[L"arr"] = json::warray { 1, 2, 3 };
root[L"arr"].emplace(4);
root[L"arr"].emplace(5);
root[L"arr"] += json::warray { 6, 7 };

std::vector<int> vec = { 1, 2, 3, 4, 5 };
root[L"arr from vec"] = vec;

std::set<std::wstring> set = { L"a", L"bb\n\nb", L"cc\t" };
root[L"arr from set"] = set;

std::map<std::wstring, int> map {
{ L"key1", 1 },
{ L"key2", 2 },
};
root[L"obj from map"] = map;

root[L"a\\n"] = L"1a\\n";
root[L"a\n"] = L"2a\n";
if (root[L"a\\n"].as_string() != L"1a\\n") {
std::wcerr << "error: " << root[L"a\\n"].as_string() << std::endl;
return false;
}
if (root[L"a\n"].as_string() != L"2a\n") {
std::wcerr << "error: " << root[L"a\n"].as_string() << std::endl;
return false;
}
if (root[L"a\\n"].to_string() != L"\"1a\\\\n\"") {
std::wcerr << "error: " << root[L"a\\n"].to_string() << std::endl;
return false;
}
if (root[L"a\n"].to_string() != L"\"2a\\n\"") {
std::wcerr << "error: " << root[L"a\n"].to_string() << std::endl;
return false;
}

std::wcout << root << std::endl;
std::wofstream ofs("wstring_serializing.json");
ofs << root;
ofs.close();

return true;
}

0 comments on commit 17cb823

Please sign in to comment.