Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
1bdb6acb
Commit
1bdb6acb
authored
Apr 08, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
overworked type conversion
parent
0a96116b
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
1105 additions
and
382 deletions
+1105
-382
src/json.hpp
src/json.hpp
+855
-350
src/json.hpp.re2c
src/json.hpp.re2c
+148
-32
test/unit.cpp
test/unit.cpp
+102
-0
No files found.
src/json.hpp
View file @
1bdb6acb
This diff is collapsed.
Click to expand it.
src/json.hpp.re2c
View file @
1bdb6acb
...
...
@@ -58,16 +58,15 @@ namespace nlohmann
// Helper to determine whether there's a key_type for T.
// http://stackoverflow.com/a/7728728/266378
template<typename T>
struct has_
key
_type
struct has_
mapped
_type
{
private:
template<typename C> static char test(typename C::
key
_type*);
template<typename C> static char test(typename C::
mapped
_type*);
template<typename C> static int test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
/*!
@brief JSON
...
...
@@ -814,7 +813,7 @@ class basic_json
return m_type;
}
private:
//////////////////////
// value conversion //
//////////////////////
...
...
@@ -822,89 +821,206 @@ class basic_json
/// get an object (explicit)
template <class T, typename
std::enable_if<
std::is_con
structible<typename T::key_type, typename object_t
::key_type>::value and
std::is_con
structible<typename T::mapped_type, basic_json>::value, int>::typ
e
= 0>
inline T get
(
) const
std::is_con
vertible<typename object_t::key_type, typename T
::key_type>::value and
std::is_con
vertible<basic_json, typename T::mapped_type>::valu
e
, int>::type
= 0>
inline T get
_impl(T*
) const
{
switch (m_type)
{
case (value_t::object):
{
return T(m_value.object->begin(), m_value.object->end());
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get an object (explicit)
inline object_t get_impl(object_t*) const
{
switch (m_type)
{
case (value_t::object):
{
return *(m_value.object);
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to object");
}
}
}
/// get an array (explicit)
template <class T, typename
std::enable_if<
not std::is_same<T, string_t>::value and
not has_key_type<T>::value and
std::is_constructible<typename T::value_type, basic_json>::value, int>::type
= 0>
inline T get() const
std::is_convertible<basic_json, typename T::value_type>::value and
not std::is_same<basic_json, typename T::value_type>::value and
not std::is_arithmetic<T>::value and
not std::is_convertible<std::string, T>::value and
not has_mapped_type<T>::value
, int>::type = 0>
inline T get_impl(T*) const
{
switch (m_type)
{
case (value_t::array):
return T(m_value.array->begin(), m_value.array->end());
{
T to_vector;
//to_vector.reserve(m_value.array->size());
std::transform(m_value.array->begin(), m_value.array->end(),
std::inserter(to_vector, to_vector.end()), [](basic_json i)
{
return i.get<typename T::value_type>();
});
return to_vector;
// return T(m_value.array->begin(), m_value.array->end());
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get a
string
(explicit)
template <
typename
T, typename
/// get a
n array
(explicit)
template <
class
T, typename
std::enable_if<
std::is_constructible<T, string_t>::value, int>::type
= 0>
inline T get() const
std::is_convertible<basic_json, T>::value and
not std::is_same<basic_json, T>::value
, int>::type = 0>
inline std::vector<T> get_impl(std::vector<T>*) const
{
switch (m_type)
{
case (value_t::string):
return *m_value.string;
case (value_t::array):
{
std::vector<T> to_vector;
to_vector.reserve(m_value.array->size());
std::transform(m_value.array->begin(), m_value.array->end(),
std::inserter(to_vector, to_vector.end()), [](basic_json i)
{
return i.get<T>();
});
return to_vector;
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get a boolean (explicit)
/// get an array (explicit)
template <class T, typename
std::enable_if<
std::is_same<basic_json, typename T::value_type>::value and
not has_mapped_type<T>::value
, int>::type = 0>
inline T get_impl(T*) const
{
switch (m_type)
{
case (value_t::array):
{
return T(m_value.array->begin(), m_value.array->end());
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
inline array_t get_impl(array_t*) const
{
switch (m_type)
{
case (value_t::array):
{
return *(m_value.array);
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to array");
}
}
}
/// get a string (explicit)
template <typename T, typename
std::enable_if<
std::is_
same<boolean_t, T>::value, int>::typ
e
= 0>
inline T get
(
) const
std::is_
convertible<string_t, T>::valu
e
, int>::type
= 0>
inline T get
_impl(T*
) const
{
switch (m_type)
{
case (value_t::boolean):
return m_value.boolean;
case (value_t::string):
{
return *m_value.string;
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get a number (explicit)
template<typename T, typename
std::enable_if<
not std::is_same<boolean_t, T>::value and
std::is_arithmetic<T>::value, int>::type
= 0>
inline T get() const
std::is_arithmetic<T>::value
, int>::type = 0>
inline T get_impl(T*) const
{
switch (m_type)
{
case (value_t::number_integer):
{
return static_cast<T>(m_value.number_integer);
}
case (value_t::number_float):
{
return static_cast<T>(m_value.number_float);
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get a boolean (explicit)
inline boolean_t get_impl(boolean_t*) const
{
switch (m_type)
{
case (value_t::boolean):
{
return m_value.boolean;
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(boolean_t).name());
}
}
}
public:
/// get a value (explicit)
// <http://stackoverflow.com/a/8315197/266378>
template<typename T>
inline T get() const
{
return get_impl(static_cast<T*>(nullptr));
}
/// get a value (implicit)
template<typename T>
inline operator T() const
...
...
@@ -1077,7 +1193,7 @@ class basic_json
// at only works for objects
if (m_type != value_t::object)
{
throw std::runtime_error("cannot use
at
with " + type_name());
throw std::runtime_error("cannot use
erase
with " + type_name());
}
return m_value.object->erase(key);
...
...
test/unit.cpp
View file @
1bdb6acb
...
...
@@ -2006,6 +2006,108 @@ TEST_CASE("value conversion")
CHECK
(
json
(
n
).
m_value
.
number_float
==
Approx
(
j
.
m_value
.
number_float
));
}
}
SECTION
(
"more involved conversions"
)
{
SECTION
(
"object-like STL containers"
)
{
json
j1
=
{{
"one"
,
1
},
{
"two"
,
2
},
{
"three"
,
3
}};
json
j2
=
{{
"one"
,
1.1
},
{
"two"
,
2.2
},
{
"three"
,
3.3
}};
json
j3
=
{{
"one"
,
true
},
{
"two"
,
false
},
{
"three"
,
true
}};
json
j4
=
{{
"one"
,
"eins"
},
{
"two"
,
"zwei"
},
{
"three"
,
"drei"
}};
SECTION
(
"std::map"
)
{
auto
m1
=
j1
.
get
<
std
::
map
<
std
::
string
,
int
>>
();
auto
m2
=
j2
.
get
<
std
::
map
<
std
::
string
,
double
>>
();
auto
m3
=
j3
.
get
<
std
::
map
<
std
::
string
,
bool
>>
();
//auto m4 = j4.get<std::map<std::string, std::string>>();
}
SECTION
(
"std::unordered_map"
)
{
auto
m1
=
j1
.
get
<
std
::
unordered_map
<
std
::
string
,
int
>>
();
auto
m2
=
j2
.
get
<
std
::
unordered_map
<
std
::
string
,
double
>>
();
auto
m3
=
j3
.
get
<
std
::
unordered_map
<
std
::
string
,
bool
>>
();
//auto m4 = j4.get<std::unordered_map<std::string, std::string>>();
//CHECK(m4["one"] == "eins");
}
SECTION
(
"std::multimap"
)
{
auto
m1
=
j1
.
get
<
std
::
multimap
<
std
::
string
,
int
>>
();
auto
m2
=
j2
.
get
<
std
::
multimap
<
std
::
string
,
double
>>
();
auto
m3
=
j3
.
get
<
std
::
multimap
<
std
::
string
,
bool
>>
();
//auto m4 = j4.get<std::multimap<std::string, std::string>>();
//CHECK(m4["one"] == "eins");
}
SECTION
(
"std::unordered_multimap"
)
{
auto
m1
=
j1
.
get
<
std
::
unordered_multimap
<
std
::
string
,
int
>>
();
auto
m2
=
j2
.
get
<
std
::
unordered_multimap
<
std
::
string
,
double
>>
();
auto
m3
=
j3
.
get
<
std
::
unordered_multimap
<
std
::
string
,
bool
>>
();
//auto m4 = j4.get<std::unordered_multimap<std::string, std::string>>();
//CHECK(m4["one"] == "eins");
}
}
SECTION
(
"array-like STL containers"
)
{
json
j1
=
{
1
,
2
,
3
,
4
};
json
j2
=
{
1.2
,
2.3
,
3.4
,
4.5
};
json
j3
=
{
true
,
false
,
true
};
json
j4
=
{
"one"
,
"two"
,
"three"
};
SECTION
(
"std::list"
)
{
auto
m1
=
j1
.
get
<
std
::
list
<
int
>>
();
auto
m2
=
j2
.
get
<
std
::
list
<
double
>>
();
auto
m3
=
j3
.
get
<
std
::
list
<
bool
>>
();
auto
m4
=
j4
.
get
<
std
::
list
<
std
::
string
>>
();
}
//SECTION("std::forward_list")
//{
// auto m1 = j1.get<std::forward_list<int>>();
// auto m2 = j2.get<std::forward_list<double>>();
// auto m3 = j3.get<std::forward_list<bool>>();
// auto m4 = j4.get<std::forward_list<std::string>>();
//}
SECTION
(
"std::vector"
)
{
auto
m1
=
j1
.
get
<
std
::
vector
<
int
>>
();
auto
m2
=
j2
.
get
<
std
::
vector
<
double
>>
();
auto
m3
=
j3
.
get
<
std
::
vector
<
bool
>>
();
auto
m4
=
j4
.
get
<
std
::
vector
<
std
::
string
>>
();
}
SECTION
(
"std::deque"
)
{
auto
m1
=
j1
.
get
<
std
::
deque
<
int
>>
();
auto
m2
=
j2
.
get
<
std
::
deque
<
double
>>
();
auto
m3
=
j3
.
get
<
std
::
deque
<
bool
>>
();
auto
m4
=
j4
.
get
<
std
::
deque
<
std
::
string
>>
();
}
SECTION
(
"std::set"
)
{
auto
m1
=
j1
.
get
<
std
::
set
<
int
>>
();
auto
m2
=
j2
.
get
<
std
::
set
<
double
>>
();
auto
m3
=
j3
.
get
<
std
::
set
<
bool
>>
();
auto
m4
=
j4
.
get
<
std
::
set
<
std
::
string
>>
();
}
SECTION
(
"std::unordered_set"
)
{
auto
m1
=
j1
.
get
<
std
::
unordered_set
<
int
>>
();
auto
m2
=
j2
.
get
<
std
::
unordered_set
<
double
>>
();
auto
m3
=
j3
.
get
<
std
::
unordered_set
<
bool
>>
();
auto
m4
=
j4
.
get
<
std
::
unordered_set
<
std
::
string
>>
();
}
}
}
}
TEST_CASE
(
"element access"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment