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
e9d64113
Unverified
Commit
e9d64113
authored
Jan 16, 2021
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🐛
proper JSON Pointer escape in diagnostic messages
parent
7633a21e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
252 additions
and
199 deletions
+252
-199
diagnostics_t.hpp
include/nlohmann/detail/diagnostics_t.hpp
+2
-1
json_pointer.hpp
include/nlohmann/detail/json_pointer.hpp
+4
-43
string_escape.hpp
include/nlohmann/detail/string_escape.hpp
+63
-0
json.hpp
include/nlohmann/json.hpp
+2
-2
json.hpp
single_include/nlohmann/json.hpp
+181
-153
No files found.
include/nlohmann/detail/diagnostics_t.hpp
View file @
e9d64113
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
#include <string>
#include <string>
#include <vector>
#include <vector>
#include <nlohmann/detail/value_t.hpp>
#include <nlohmann/detail/value_t.hpp>
#include <nlohmann/detail/string_escape.hpp>
namespace
nlohmann
namespace
nlohmann
{
{
...
@@ -70,7 +71,7 @@ class diagnostics_t
...
@@ -70,7 +71,7 @@ class diagnostics_t
return
"("
+
std
::
accumulate
(
tokens
.
rbegin
(),
tokens
.
rend
(),
std
::
string
{},
return
"("
+
std
::
accumulate
(
tokens
.
rbegin
(),
tokens
.
rend
(),
std
::
string
{},
[](
const
std
::
string
&
a
,
const
std
::
string
&
b
)
[](
const
std
::
string
&
a
,
const
std
::
string
&
b
)
{
{
return
a
+
"/"
+
b
;
return
a
+
"/"
+
detail
::
escape
(
b
)
;
})
+
") "
;
})
+
") "
;
#else
#else
return
""
;
return
""
;
...
...
include/nlohmann/detail/json_pointer.hpp
View file @
e9d64113
...
@@ -11,6 +11,7 @@
...
@@ -11,6 +11,7 @@
#include <nlohmann/detail/diagnostics_t.hpp>
#include <nlohmann/detail/diagnostics_t.hpp>
#include <nlohmann/detail/exceptions.hpp>
#include <nlohmann/detail/exceptions.hpp>
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/detail/string_escape.hpp>
#include <nlohmann/detail/value_t.hpp>
#include <nlohmann/detail/value_t.hpp>
namespace
nlohmann
namespace
nlohmann
...
@@ -70,7 +71,7 @@ class json_pointer
...
@@ -70,7 +71,7 @@ class json_pointer
std
::
string
{},
std
::
string
{},
[](
const
std
::
string
&
a
,
const
std
::
string
&
b
)
[](
const
std
::
string
&
a
,
const
std
::
string
&
b
)
{
{
return
a
+
"/"
+
escape
(
b
);
return
a
+
"/"
+
detail
::
escape
(
b
);
});
});
}
}
...
@@ -791,53 +792,13 @@ class json_pointer
...
@@ -791,53 +792,13 @@ class json_pointer
}
}
// finally, store the reference token
// finally, store the reference token
unescape
(
reference_token
);
detail
::
unescape
(
reference_token
);
result
.
push_back
(
reference_token
);
result
.
push_back
(
reference_token
);
}
}
return
result
;
return
result
;
}
}
/*!
@brief replace all occurrences of a substring by another string
@param[in,out] s the string to manipulate; changed so that all
occurrences of @a f are replaced with @a t
@param[in] f the substring to replace with @a t
@param[in] t the string to replace @a f
@pre The search string @a f must not be empty. **This precondition is
enforced with an assertion.**
@since version 2.0.0
*/
static
void
replace_substring
(
std
::
string
&
s
,
const
std
::
string
&
f
,
const
std
::
string
&
t
)
{
JSON_ASSERT
(
!
f
.
empty
());
for
(
auto
pos
=
s
.
find
(
f
);
// find first occurrence of f
pos
!=
std
::
string
::
npos
;
// make sure f was found
s
.
replace
(
pos
,
f
.
size
(),
t
),
// replace with t, and
pos
=
s
.
find
(
f
,
pos
+
t
.
size
()))
// find next occurrence of f
{}
}
JSON_PRIVATE_UNLESS_TESTED
:
/// escape "~" to "~0" and "/" to "~1"
static
std
::
string
escape
(
std
::
string
s
)
{
replace_substring
(
s
,
"~"
,
"~0"
);
replace_substring
(
s
,
"/"
,
"~1"
);
return
s
;
}
/// unescape "~1" to tilde and "~0" to slash (order is important!)
static
void
unescape
(
std
::
string
&
s
)
{
replace_substring
(
s
,
"~1"
,
"/"
);
replace_substring
(
s
,
"~0"
,
"~"
);
}
private
:
private
:
/*!
/*!
@param[in] reference_string the reference string to the current value
@param[in] reference_string the reference string to the current value
...
@@ -883,7 +844,7 @@ class json_pointer
...
@@ -883,7 +844,7 @@ class json_pointer
// iterate object and use keys as reference string
// iterate object and use keys as reference string
for
(
const
auto
&
element
:
*
value
.
m_value
.
object
)
for
(
const
auto
&
element
:
*
value
.
m_value
.
object
)
{
{
flatten
(
reference_string
+
"/"
+
escape
(
element
.
first
),
element
.
second
,
result
);
flatten
(
reference_string
+
"/"
+
detail
::
escape
(
element
.
first
),
element
.
second
,
result
);
}
}
}
}
break
;
break
;
...
...
include/nlohmann/detail/string_escape.hpp
0 → 100644
View file @
e9d64113
#pragma once
#include <string>
#include <nlohmann/detail/macro_scope.hpp>
namespace
nlohmann
{
namespace
detail
{
/*!
@brief replace all occurrences of a substring by another string
@param[in,out] s the string to manipulate; changed so that all
occurrences of @a f are replaced with @a t
@param[in] f the substring to replace with @a t
@param[in] t the string to replace @a f
@pre The search string @a f must not be empty. **This precondition is
enforced with an assertion.**
@since version 2.0.0
*/
inline
void
replace_substring
(
std
::
string
&
s
,
const
std
::
string
&
f
,
const
std
::
string
&
t
)
{
JSON_ASSERT
(
!
f
.
empty
());
for
(
auto
pos
=
s
.
find
(
f
);
// find first occurrence of f
pos
!=
std
::
string
::
npos
;
// make sure f was found
s
.
replace
(
pos
,
f
.
size
(),
t
),
// replace with t, and
pos
=
s
.
find
(
f
,
pos
+
t
.
size
()))
// find next occurrence of f
{}
}
/*!
* @brief string escaping as described in RFC 6901 (Sect. 4)
* @param[in] s string to escape
* @return escaped string
*
* Note the order of escaping "~" to "~0" and "/" to "~1" is important.
*/
inline
std
::
string
escape
(
std
::
string
s
)
{
replace_substring
(
s
,
"~"
,
"~0"
);
replace_substring
(
s
,
"/"
,
"~1"
);
return
s
;
}
/*!
* @brief string unescaping as described in RFC 6901 (Sect. 4)
* @param[in] s string to unescape
* @return unescaped string
*
* Note the order of escaping "~1" to "/" and "~0" to "~" is important.
*/
static
void
unescape
(
std
::
string
&
s
)
{
replace_substring
(
s
,
"~1"
,
"/"
);
replace_substring
(
s
,
"~0"
,
"~"
);
}
}
// namespace detail
}
// namespace nlohmann
include/nlohmann/json.hpp
View file @
e9d64113
...
@@ -8651,7 +8651,7 @@ class basic_json
...
@@ -8651,7 +8651,7 @@ class basic_json
for
(
auto
it
=
source
.
cbegin
();
it
!=
source
.
cend
();
++
it
)
for
(
auto
it
=
source
.
cbegin
();
it
!=
source
.
cend
();
++
it
)
{
{
// escape the key name to be used in a JSON patch
// escape the key name to be used in a JSON patch
const
auto
key
=
json_pointer
::
escape
(
it
.
key
());
const
auto
key
=
detail
::
escape
(
it
.
key
());
if
(
target
.
find
(
it
.
key
())
!=
target
.
end
())
if
(
target
.
find
(
it
.
key
())
!=
target
.
end
())
{
{
...
@@ -8675,7 +8675,7 @@ class basic_json
...
@@ -8675,7 +8675,7 @@ class basic_json
if
(
source
.
find
(
it
.
key
())
==
source
.
end
())
if
(
source
.
find
(
it
.
key
())
==
source
.
end
())
{
{
// found a key that is not in this -> add it
// found a key that is not in this -> add it
const
auto
key
=
json_pointer
::
escape
(
it
.
key
());
const
auto
key
=
detail
::
escape
(
it
.
key
());
result
.
push_back
(
result
.
push_back
(
{
{
{
"op"
,
"add"
},
{
"path"
,
path
+
"/"
+
key
},
{
"op"
,
"add"
},
{
"path"
,
path
+
"/"
+
key
},
...
...
single_include/nlohmann/json.hpp
View file @
e9d64113
...
@@ -161,116 +161,10 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept
...
@@ -161,116 +161,10 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept
}
// namespace detail
}
// namespace detail
}
// namespace nlohmann
}
// namespace nlohmann
// #include <nlohmann/detail/string_escape.hpp>
namespace
nlohmann
{
namespace
detail
{
template
<
typename
BasicJsonType
>
class
diagnostics_t
{
public
:
diagnostics_t
()
noexcept
=
default
;
diagnostics_t
(
const
BasicJsonType
&
j
)
noexcept
:
m_j
(
&
j
)
{}
std
::
string
diagnostics
()
const
{
#if JSON_DIAGNOSTICS
if
(
m_j
==
nullptr
)
{
return
""
;
}
std
::
vector
<
std
::
string
>
tokens
;
for
(
const
auto
*
current
=
m_j
;
current
->
m_parent
!=
nullptr
;
current
=
current
->
m_parent
)
{
switch
(
current
->
m_parent
->
type
())
{
case
value_t
:
:
array
:
{
for
(
std
::
size_t
i
=
0
;
i
<
current
->
m_parent
->
m_value
.
array
->
size
();
++
i
)
{
if
(
current
->
m_parent
->
m_value
.
array
->
operator
[](
i
)
==
*
current
)
{
tokens
.
emplace_back
(
std
::
to_string
(
i
));
continue
;
}
}
break
;
}
case
value_t
:
:
object
:
{
for
(
const
auto
&
element
:
*
current
->
m_parent
->
m_value
.
object
)
{
if
(
element
.
second
==
*
current
)
{
tokens
.
emplace_back
(
element
.
first
.
c_str
());
continue
;
}
}
break
;
}
default
:
break
;
}
}
if
(
tokens
.
empty
())
{
return
""
;
}
return
"("
+
std
::
accumulate
(
tokens
.
rbegin
(),
tokens
.
rend
(),
std
::
string
{},
[](
const
std
::
string
&
a
,
const
std
::
string
&
b
)
{
return
a
+
"/"
+
b
;
})
+
") "
;
#else
return
""
;
#endif
}
private
:
const
BasicJsonType
*
m_j
=
static_cast
<
const
BasicJsonType
*>
(
nullptr
);
};
}
// namespace detail
}
// namespace nlohmann
// #include <nlohmann/detail/input/position_t.hpp>
#include <cstddef> // size_t
namespace
nlohmann
{
namespace
detail
{
/// struct to capture the start position of the current token
struct
position_t
{
/// the total number of characters read
std
::
size_t
chars_read_total
=
0
;
/// the number of characters read in the current line
std
::
size_t
chars_read_current_line
=
0
;
/// the number of lines read
std
::
size_t
lines_read
=
0
;
/// conversion to size_t to preserve SAX interface
constexpr
operator
size_t
()
const
{
return
chars_read_total
;
}
};
}
// namespace detail
}
// namespace nlohmann
#include <string>
// #include <nlohmann/detail/macro_scope.hpp>
// #include <nlohmann/detail/macro_scope.hpp>
...
@@ -2553,6 +2447,178 @@ namespace nlohmann
...
@@ -2553,6 +2447,178 @@ namespace nlohmann
{
{
namespace
detail
namespace
detail
{
{
/*!
@brief replace all occurrences of a substring by another string
@param[in,out] s the string to manipulate; changed so that all
occurrences of @a f are replaced with @a t
@param[in] f the substring to replace with @a t
@param[in] t the string to replace @a f
@pre The search string @a f must not be empty. **This precondition is
enforced with an assertion.**
@since version 2.0.0
*/
inline
void
replace_substring
(
std
::
string
&
s
,
const
std
::
string
&
f
,
const
std
::
string
&
t
)
{
JSON_ASSERT
(
!
f
.
empty
());
for
(
auto
pos
=
s
.
find
(
f
);
// find first occurrence of f
pos
!=
std
::
string
::
npos
;
// make sure f was found
s
.
replace
(
pos
,
f
.
size
(),
t
),
// replace with t, and
pos
=
s
.
find
(
f
,
pos
+
t
.
size
()))
// find next occurrence of f
{}
}
/*!
* @brief string escaping as described in RFC 6901 (Sect. 4)
* @param[in] s string to escape
* @return escaped string
*
* Note the order of escaping "~" to "~0" and "/" to "~1" is important.
*/
inline
std
::
string
escape
(
std
::
string
s
)
{
replace_substring
(
s
,
"~"
,
"~0"
);
replace_substring
(
s
,
"/"
,
"~1"
);
return
s
;
}
/*!
* @brief string unescaping as described in RFC 6901 (Sect. 4)
* @param[in] s string to unescape
* @return unescaped string
*
* Note the order of escaping "~1" to "/" and "~0" to "~" is important.
*/
static
void
unescape
(
std
::
string
&
s
)
{
replace_substring
(
s
,
"~1"
,
"/"
);
replace_substring
(
s
,
"~0"
,
"~"
);
}
}
// namespace detail
}
// namespace nlohmann
namespace
nlohmann
{
namespace
detail
{
template
<
typename
BasicJsonType
>
class
diagnostics_t
{
public
:
diagnostics_t
()
noexcept
=
default
;
diagnostics_t
(
const
BasicJsonType
&
j
)
noexcept
:
m_j
(
&
j
)
{}
std
::
string
diagnostics
()
const
{
#if JSON_DIAGNOSTICS
if
(
m_j
==
nullptr
)
{
return
""
;
}
std
::
vector
<
std
::
string
>
tokens
;
for
(
const
auto
*
current
=
m_j
;
current
->
m_parent
!=
nullptr
;
current
=
current
->
m_parent
)
{
switch
(
current
->
m_parent
->
type
())
{
case
value_t
:
:
array
:
{
for
(
std
::
size_t
i
=
0
;
i
<
current
->
m_parent
->
m_value
.
array
->
size
();
++
i
)
{
if
(
current
->
m_parent
->
m_value
.
array
->
operator
[](
i
)
==
*
current
)
{
tokens
.
emplace_back
(
std
::
to_string
(
i
));
continue
;
}
}
break
;
}
case
value_t
:
:
object
:
{
for
(
const
auto
&
element
:
*
current
->
m_parent
->
m_value
.
object
)
{
if
(
element
.
second
==
*
current
)
{
tokens
.
emplace_back
(
element
.
first
.
c_str
());
continue
;
}
}
break
;
}
default
:
break
;
}
}
if
(
tokens
.
empty
())
{
return
""
;
}
return
"("
+
std
::
accumulate
(
tokens
.
rbegin
(),
tokens
.
rend
(),
std
::
string
{},
[](
const
std
::
string
&
a
,
const
std
::
string
&
b
)
{
return
a
+
"/"
+
detail
::
escape
(
b
);
})
+
") "
;
#else
return
""
;
#endif
}
private
:
const
BasicJsonType
*
m_j
=
static_cast
<
const
BasicJsonType
*>
(
nullptr
);
};
}
// namespace detail
}
// namespace nlohmann
// #include <nlohmann/detail/input/position_t.hpp>
#include <cstddef> // size_t
namespace
nlohmann
{
namespace
detail
{
/// struct to capture the start position of the current token
struct
position_t
{
/// the total number of characters read
std
::
size_t
chars_read_total
=
0
;
/// the number of characters read in the current line
std
::
size_t
chars_read_current_line
=
0
;
/// the number of lines read
std
::
size_t
lines_read
=
0
;
/// conversion to size_t to preserve SAX interface
constexpr
operator
size_t
()
const
{
return
chars_read_total
;
}
};
}
// namespace detail
}
// namespace nlohmann
// #include <nlohmann/detail/macro_scope.hpp>
namespace
nlohmann
{
namespace
detail
{
////////////////
////////////////
// exceptions //
// exceptions //
////////////////
////////////////
...
@@ -11798,6 +11864,8 @@ class json_reverse_iterator : public std::reverse_iterator<Base>
...
@@ -11798,6 +11864,8 @@ class json_reverse_iterator : public std::reverse_iterator<Base>
// #include <nlohmann/detail/macro_scope.hpp>
// #include <nlohmann/detail/macro_scope.hpp>
// #include <nlohmann/detail/string_escape.hpp>
// #include <nlohmann/detail/value_t.hpp>
// #include <nlohmann/detail/value_t.hpp>
...
@@ -11858,7 +11926,7 @@ class json_pointer
...
@@ -11858,7 +11926,7 @@ class json_pointer
std
::
string
{},
std
::
string
{},
[](
const
std
::
string
&
a
,
const
std
::
string
&
b
)
[](
const
std
::
string
&
a
,
const
std
::
string
&
b
)
{
{
return
a
+
"/"
+
escape
(
b
);
return
a
+
"/"
+
detail
::
escape
(
b
);
});
});
}
}
...
@@ -12579,53 +12647,13 @@ class json_pointer
...
@@ -12579,53 +12647,13 @@ class json_pointer
}
}
// finally, store the reference token
// finally, store the reference token
unescape
(
reference_token
);
detail
::
unescape
(
reference_token
);
result
.
push_back
(
reference_token
);
result
.
push_back
(
reference_token
);
}
}
return
result
;
return
result
;
}
}
/*!
@brief replace all occurrences of a substring by another string
@param[in,out] s the string to manipulate; changed so that all
occurrences of @a f are replaced with @a t
@param[in] f the substring to replace with @a t
@param[in] t the string to replace @a f
@pre The search string @a f must not be empty. **This precondition is
enforced with an assertion.**
@since version 2.0.0
*/
static
void
replace_substring
(
std
::
string
&
s
,
const
std
::
string
&
f
,
const
std
::
string
&
t
)
{
JSON_ASSERT
(
!
f
.
empty
());
for
(
auto
pos
=
s
.
find
(
f
);
// find first occurrence of f
pos
!=
std
::
string
::
npos
;
// make sure f was found
s
.
replace
(
pos
,
f
.
size
(),
t
),
// replace with t, and
pos
=
s
.
find
(
f
,
pos
+
t
.
size
()))
// find next occurrence of f
{}
}
JSON_PRIVATE_UNLESS_TESTED
:
/// escape "~" to "~0" and "/" to "~1"
static
std
::
string
escape
(
std
::
string
s
)
{
replace_substring
(
s
,
"~"
,
"~0"
);
replace_substring
(
s
,
"/"
,
"~1"
);
return
s
;
}
/// unescape "~1" to tilde and "~0" to slash (order is important!)
static
void
unescape
(
std
::
string
&
s
)
{
replace_substring
(
s
,
"~1"
,
"/"
);
replace_substring
(
s
,
"~0"
,
"~"
);
}
private
:
private
:
/*!
/*!
@param[in] reference_string the reference string to the current value
@param[in] reference_string the reference string to the current value
...
@@ -12671,7 +12699,7 @@ class json_pointer
...
@@ -12671,7 +12699,7 @@ class json_pointer
// iterate object and use keys as reference string
// iterate object and use keys as reference string
for
(
const
auto
&
element
:
*
value
.
m_value
.
object
)
for
(
const
auto
&
element
:
*
value
.
m_value
.
object
)
{
{
flatten
(
reference_string
+
"/"
+
escape
(
element
.
first
),
element
.
second
,
result
);
flatten
(
reference_string
+
"/"
+
detail
::
escape
(
element
.
first
),
element
.
second
,
result
);
}
}
}
}
break
;
break
;
...
@@ -25406,7 +25434,7 @@ class basic_json
...
@@ -25406,7 +25434,7 @@ class basic_json
for
(
auto
it
=
source
.
cbegin
();
it
!=
source
.
cend
();
++
it
)
for
(
auto
it
=
source
.
cbegin
();
it
!=
source
.
cend
();
++
it
)
{
{
// escape the key name to be used in a JSON patch
// escape the key name to be used in a JSON patch
const
auto
key
=
json_pointer
::
escape
(
it
.
key
());
const
auto
key
=
detail
::
escape
(
it
.
key
());
if
(
target
.
find
(
it
.
key
())
!=
target
.
end
())
if
(
target
.
find
(
it
.
key
())
!=
target
.
end
())
{
{
...
@@ -25430,7 +25458,7 @@ class basic_json
...
@@ -25430,7 +25458,7 @@ class basic_json
if
(
source
.
find
(
it
.
key
())
==
source
.
end
())
if
(
source
.
find
(
it
.
key
())
==
source
.
end
())
{
{
// found a key that is not in this -> add it
// found a key that is not in this -> add it
const
auto
key
=
json_pointer
::
escape
(
it
.
key
());
const
auto
key
=
detail
::
escape
(
it
.
key
());
result
.
push_back
(
result
.
push_back
(
{
{
{
"op"
,
"add"
},
{
"path"
,
path
+
"/"
+
key
},
{
"op"
,
"add"
},
{
"path"
,
path
+
"/"
+
key
},
...
...
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