Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
0f073e26
Commit
0f073e26
authored
Sep 26, 2019
by
Julien Hamaide
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow items() to be used with custom string
parent
99d7518d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
8 deletions
+52
-8
iteration_proxy.hpp
include/nlohmann/detail/iterators/iteration_proxy.hpp
+9
-4
json.hpp
single_include/nlohmann/json.hpp
+9
-4
unit-alt-string.cpp
test/src/unit-alt-string.cpp
+34
-0
No files found.
include/nlohmann/detail/iterators/iteration_proxy.hpp
View file @
0f073e26
...
@@ -12,6 +12,10 @@ namespace nlohmann
...
@@ -12,6 +12,10 @@ namespace nlohmann
{
{
namespace
detail
namespace
detail
{
{
void
int_to_string
(
std
::
string
&
target
,
int
value
)
{
target
=
std
::
to_string
(
value
);
}
template
<
typename
IteratorType
>
class
iteration_proxy_value
template
<
typename
IteratorType
>
class
iteration_proxy_value
{
{
public
:
public
:
...
@@ -20,6 +24,7 @@ template <typename IteratorType> class iteration_proxy_value
...
@@ -20,6 +24,7 @@ template <typename IteratorType> class iteration_proxy_value
using
pointer
=
value_type
*
;
using
pointer
=
value_type
*
;
using
reference
=
value_type
&
;
using
reference
=
value_type
&
;
using
iterator_category
=
std
::
input_iterator_tag
;
using
iterator_category
=
std
::
input_iterator_tag
;
using
string_type
=
typename
std
::
remove_cv
<
typename
std
::
remove_reference
<
decltype
(
std
::
declval
<
IteratorType
>
().
key
()
)
>::
type
>::
type
;
private
:
private
:
/// the iterator
/// the iterator
...
@@ -29,9 +34,9 @@ template <typename IteratorType> class iteration_proxy_value
...
@@ -29,9 +34,9 @@ template <typename IteratorType> class iteration_proxy_value
/// last stringified array index
/// last stringified array index
mutable
std
::
size_t
array_index_last
=
0
;
mutable
std
::
size_t
array_index_last
=
0
;
/// a string representation of the array index
/// a string representation of the array index
mutable
st
d
::
string
array_index_str
=
"0"
;
mutable
st
ring_type
array_index_str
=
"0"
;
/// an empty string (to return a reference for primitive values)
/// an empty string (to return a reference for primitive values)
const
st
d
::
string
empty_str
=
""
;
const
st
ring_type
empty_str
=
""
;
public
:
public
:
explicit
iteration_proxy_value
(
IteratorType
it
)
noexcept
:
anchor
(
it
)
{}
explicit
iteration_proxy_value
(
IteratorType
it
)
noexcept
:
anchor
(
it
)
{}
...
@@ -64,7 +69,7 @@ template <typename IteratorType> class iteration_proxy_value
...
@@ -64,7 +69,7 @@ template <typename IteratorType> class iteration_proxy_value
}
}
/// return key of the iterator
/// return key of the iterator
const
st
d
::
string
&
key
()
const
const
st
ring_type
&
key
()
const
{
{
assert
(
anchor
.
m_object
!=
nullptr
);
assert
(
anchor
.
m_object
!=
nullptr
);
...
@@ -75,7 +80,7 @@ template <typename IteratorType> class iteration_proxy_value
...
@@ -75,7 +80,7 @@ template <typename IteratorType> class iteration_proxy_value
{
{
if
(
array_index
!=
array_index_last
)
if
(
array_index
!=
array_index_last
)
{
{
array_index_str
=
std
::
to_string
(
array_index
);
int_to_string
(
array_index_str
,
array_index
);
array_index_last
=
array_index
;
array_index_last
=
array_index
;
}
}
return
array_index_str
;
return
array_index_str
;
...
...
single_include/nlohmann/json.hpp
View file @
0f073e26
...
@@ -3186,6 +3186,10 @@ namespace nlohmann
...
@@ -3186,6 +3186,10 @@ namespace nlohmann
{
{
namespace
detail
namespace
detail
{
{
void
int_to_string
(
std
::
string
&
target
,
int
value
)
{
target
=
std
::
to_string
(
value
);
}
template
<
typename
IteratorType
>
class
iteration_proxy_value
template
<
typename
IteratorType
>
class
iteration_proxy_value
{
{
public
:
public
:
...
@@ -3194,6 +3198,7 @@ template <typename IteratorType> class iteration_proxy_value
...
@@ -3194,6 +3198,7 @@ template <typename IteratorType> class iteration_proxy_value
using
pointer
=
value_type
*
;
using
pointer
=
value_type
*
;
using
reference
=
value_type
&
;
using
reference
=
value_type
&
;
using
iterator_category
=
std
::
input_iterator_tag
;
using
iterator_category
=
std
::
input_iterator_tag
;
using
string_type
=
typename
std
::
remove_cv
<
typename
std
::
remove_reference
<
decltype
(
std
::
declval
<
IteratorType
>
().
key
()
)
>::
type
>::
type
;
private
:
private
:
/// the iterator
/// the iterator
...
@@ -3203,9 +3208,9 @@ template <typename IteratorType> class iteration_proxy_value
...
@@ -3203,9 +3208,9 @@ template <typename IteratorType> class iteration_proxy_value
/// last stringified array index
/// last stringified array index
mutable
std
::
size_t
array_index_last
=
0
;
mutable
std
::
size_t
array_index_last
=
0
;
/// a string representation of the array index
/// a string representation of the array index
mutable
st
d
::
string
array_index_str
=
"0"
;
mutable
st
ring_type
array_index_str
=
"0"
;
/// an empty string (to return a reference for primitive values)
/// an empty string (to return a reference for primitive values)
const
st
d
::
string
empty_str
=
""
;
const
st
ring_type
empty_str
=
""
;
public
:
public
:
explicit
iteration_proxy_value
(
IteratorType
it
)
noexcept
:
anchor
(
it
)
{}
explicit
iteration_proxy_value
(
IteratorType
it
)
noexcept
:
anchor
(
it
)
{}
...
@@ -3238,7 +3243,7 @@ template <typename IteratorType> class iteration_proxy_value
...
@@ -3238,7 +3243,7 @@ template <typename IteratorType> class iteration_proxy_value
}
}
/// return key of the iterator
/// return key of the iterator
const
st
d
::
string
&
key
()
const
const
st
ring_type
&
key
()
const
{
{
assert
(
anchor
.
m_object
!=
nullptr
);
assert
(
anchor
.
m_object
!=
nullptr
);
...
@@ -3249,7 +3254,7 @@ template <typename IteratorType> class iteration_proxy_value
...
@@ -3249,7 +3254,7 @@ template <typename IteratorType> class iteration_proxy_value
{
{
if
(
array_index
!=
array_index_last
)
if
(
array_index
!=
array_index_last
)
{
{
array_index_str
=
std
::
to_string
(
array_index
);
int_to_string
(
array_index_str
,
array_index
);
array_index_last
=
array_index
;
array_index_last
=
array_index
;
}
}
return
array_index_str
;
return
array_index_str
;
...
...
test/src/unit-alt-string.cpp
View file @
0f073e26
...
@@ -154,6 +154,11 @@ class alt_string
...
@@ -154,6 +154,11 @@ class alt_string
friend
bool
::
operator
<
(
const
char
*
,
const
alt_string
&
);
friend
bool
::
operator
<
(
const
char
*
,
const
alt_string
&
);
};
};
void
int_to_string
(
alt_string
&
target
,
int
value
)
{
target
=
std
::
to_string
(
value
).
c_str
();
}
using
alt_json
=
nlohmann
::
basic_json
<
using
alt_json
=
nlohmann
::
basic_json
<
std
::
map
,
std
::
map
,
std
::
vector
,
std
::
vector
,
...
@@ -232,6 +237,35 @@ TEST_CASE("alternative string type")
...
@@ -232,6 +237,35 @@ TEST_CASE("alternative string type")
CHECK
(
dump
==
R"({"foo":"bar"})"
);
CHECK
(
dump
==
R"({"foo":"bar"})"
);
}
}
SECTION
(
"items"
)
{
auto
doc
=
alt_json
::
parse
(
"{
\"
foo
\"
:
\"
bar
\"
}"
);
for
(
auto
item
:
doc
.
items
()
)
{
CHECK
(
item
.
key
()
==
"foo"
);
CHECK
(
item
.
value
()
==
"bar"
);
}
auto
doc_array
=
alt_json
::
parse
(
"[
\"
foo
\"
,
\"
bar
\"
]"
);
for
(
auto
item
:
doc_array
.
items
()
)
{
if
(
item
.
key
()
==
"0"
)
{
CHECK
(
item
.
value
()
==
"foo"
);
}
else
if
(
item
.
key
()
==
"1"
)
{
CHECK
(
item
.
value
()
==
"bar"
);
}
else
{
CHECK
(
false
);
}
}
}
SECTION
(
"equality"
)
SECTION
(
"equality"
)
{
{
alt_json
doc
;
alt_json
doc
;
...
...
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