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
abac6a0e
Unverified
Commit
abac6a0e
authored
Mar 13, 2018
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' into feature/sax2
parents
3d4f6a29
919d1fef
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
238 additions
and
20 deletions
+238
-20
README.md
README.md
+1
-0
include/nlohmann/detail/input/lexer.hpp
include/nlohmann/detail/input/lexer.hpp
+3
-2
include/nlohmann/detail/input/parser.hpp
include/nlohmann/detail/input/parser.hpp
+2
-1
include/nlohmann/detail/output/output_adapters.hpp
include/nlohmann/detail/output/output_adapters.hpp
+6
-6
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+1
-1
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+12
-10
test/Makefile
test/Makefile
+1
-0
test/src/unit-alt-string.cpp
test/src/unit-alt-string.cpp
+212
-0
No files found.
README.md
View file @
abac6a0e
...
@@ -975,6 +975,7 @@ I deeply appreciate the help of the following people.
...
@@ -975,6 +975,7 @@ I deeply appreciate the help of the following people.
-
[
Paul Fultz II
](
https://github.com/pfultz2
)
added a note on the cget package manager.
-
[
Paul Fultz II
](
https://github.com/pfultz2
)
added a note on the cget package manager.
-
[
Wilson Lin
](
https://github.com/wla80
)
made the integration section of the README more concise.
-
[
Wilson Lin
](
https://github.com/wla80
)
made the integration section of the README more concise.
-
[
RalfBielig
](
https://github.com/ralfbielig
)
detected and fixed a memory leak in the parser callback.
-
[
RalfBielig
](
https://github.com/ralfbielig
)
detected and fixed a memory leak in the parser callback.
-
[
agrianius
](
https://github.com/agrianius
)
allowed to dump JSON to an alternative string type
Thanks a lot for helping out! Please
[
let me know
](
mailto:mail@nlohmann.me
)
if I forgot someone.
Thanks a lot for helping out! Please
[
let me know
](
mailto:mail@nlohmann.me
)
if I forgot someone.
...
...
include/nlohmann/detail/input/lexer.hpp
View file @
abac6a0e
...
@@ -32,6 +32,7 @@ class lexer
...
@@ -32,6 +32,7 @@ class lexer
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
using
string_t
=
typename
BasicJsonType
::
string_t
;
public:
public:
/// token types for the parser
/// token types for the parser
...
@@ -1130,7 +1131,7 @@ scan_number_done:
...
@@ -1130,7 +1131,7 @@ scan_number_done:
}
}
/// return current string value (implicitly resets the token; useful only once)
/// return current string value (implicitly resets the token; useful only once)
st
d
::
string
&&
move_string
()
st
ring_t
&&
move_string
()
{
{
return
std
::
move
(
token_buffer
);
return
std
::
move
(
token_buffer
);
}
}
...
@@ -1260,7 +1261,7 @@ scan_number_done:
...
@@ -1260,7 +1261,7 @@ scan_number_done:
std
::
vector
<
char
>
token_string
{};
std
::
vector
<
char
>
token_string
{};
/// buffer for variable-length tokens (numbers, strings)
/// buffer for variable-length tokens (numbers, strings)
st
d
::
string
token_buffer
{};
st
ring_t
token_buffer
{};
/// a description of occurred lexer errors
/// a description of occurred lexer errors
const
char
*
error_message
=
""
;
const
char
*
error_message
=
""
;
...
...
include/nlohmann/detail/input/parser.hpp
View file @
abac6a0e
...
@@ -33,6 +33,7 @@ class parser
...
@@ -33,6 +33,7 @@ class parser
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
using
string_t
=
typename
BasicJsonType
::
string_t
;
using
lexer_t
=
lexer
<
BasicJsonType
>
;
using
lexer_t
=
lexer
<
BasicJsonType
>
;
using
token_type
=
typename
lexer_t
::
token_type
;
using
token_type
=
typename
lexer_t
::
token_type
;
...
@@ -205,7 +206,7 @@ class parser
...
@@ -205,7 +206,7 @@ class parser
}
}
// parse values
// parse values
st
d
::
string
key
;
st
ring_t
key
;
BasicJsonType
value
;
BasicJsonType
value
;
while
(
true
)
while
(
true
)
{
{
...
...
include/nlohmann/detail/output/output_adapters.hpp
View file @
abac6a0e
...
@@ -68,11 +68,11 @@ class output_stream_adapter : public output_adapter_protocol<CharType>
...
@@ -68,11 +68,11 @@ class output_stream_adapter : public output_adapter_protocol<CharType>
};
};
/// output adapter for basic_string
/// output adapter for basic_string
template
<
typename
CharType
>
template
<
typename
CharType
,
typename
StringType
=
std
::
basic_string
<
CharType
>
>
class
output_string_adapter
:
public
output_adapter_protocol
<
CharType
>
class
output_string_adapter
:
public
output_adapter_protocol
<
CharType
>
{
{
public:
public:
explicit
output_string_adapter
(
std
::
basic_string
<
CharType
>
&
s
)
:
str
(
s
)
{}
explicit
output_string_adapter
(
StringType
&
s
)
:
str
(
s
)
{}
void
write_character
(
CharType
c
)
override
void
write_character
(
CharType
c
)
override
{
{
...
@@ -85,10 +85,10 @@ class output_string_adapter : public output_adapter_protocol<CharType>
...
@@ -85,10 +85,10 @@ class output_string_adapter : public output_adapter_protocol<CharType>
}
}
private:
private:
std
::
basic_string
<
CharType
>
&
str
;
StringType
&
str
;
};
};
template
<
typename
CharType
>
template
<
typename
CharType
,
typename
StringType
=
std
::
basic_string
<
CharType
>
>
class
output_adapter
class
output_adapter
{
{
public:
public:
...
@@ -98,8 +98,8 @@ class output_adapter
...
@@ -98,8 +98,8 @@ class output_adapter
output_adapter
(
std
::
basic_ostream
<
CharType
>&
s
)
output_adapter
(
std
::
basic_ostream
<
CharType
>&
s
)
:
oa
(
std
::
make_shared
<
output_stream_adapter
<
CharType
>>
(
s
))
{}
:
oa
(
std
::
make_shared
<
output_stream_adapter
<
CharType
>>
(
s
))
{}
output_adapter
(
std
::
basic_string
<
CharType
>
&
s
)
output_adapter
(
StringType
&
s
)
:
oa
(
std
::
make_shared
<
output_string_adapter
<
CharType
>>
(
s
))
{}
:
oa
(
std
::
make_shared
<
output_string_adapter
<
CharType
,
StringType
>>
(
s
))
{}
operator
output_adapter_t
<
CharType
>
()
operator
output_adapter_t
<
CharType
>
()
{
{
...
...
include/nlohmann/json.hpp
View file @
abac6a0e
...
@@ -1950,7 +1950,7 @@ class basic_json
...
@@ -1950,7 +1950,7 @@ class basic_json
const
bool
ensure_ascii
=
false
)
const
const
bool
ensure_ascii
=
false
)
const
{
{
string_t
result
;
string_t
result
;
serializer
s
(
detail
::
output_adapter
<
char
>
(
result
),
indent_char
);
serializer
s
(
detail
::
output_adapter
<
char
,
string_t
>
(
result
),
indent_char
);
if
(
indent
>=
0
)
if
(
indent
>=
0
)
{
{
...
...
single_include/nlohmann/json.hpp
View file @
abac6a0e
...
@@ -1871,6 +1871,7 @@ class lexer
...
@@ -1871,6 +1871,7 @@ class lexer
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
using
string_t
=
typename
BasicJsonType
::
string_t
;
public:
public:
/// token types for the parser
/// token types for the parser
...
@@ -2969,7 +2970,7 @@ scan_number_done:
...
@@ -2969,7 +2970,7 @@ scan_number_done:
}
}
/// return current string value (implicitly resets the token; useful only once)
/// return current string value (implicitly resets the token; useful only once)
st
d
::
string
&&
move_string
()
st
ring_t
&&
move_string
()
{
{
return
std
::
move
(
token_buffer
);
return
std
::
move
(
token_buffer
);
}
}
...
@@ -3099,7 +3100,7 @@ scan_number_done:
...
@@ -3099,7 +3100,7 @@ scan_number_done:
std
::
vector
<
char
>
token_string
{};
std
::
vector
<
char
>
token_string
{};
/// buffer for variable-length tokens (numbers, strings)
/// buffer for variable-length tokens (numbers, strings)
st
d
::
string
token_buffer
{};
st
ring_t
token_buffer
{};
/// a description of occurred lexer errors
/// a description of occurred lexer errors
const
char
*
error_message
=
""
;
const
char
*
error_message
=
""
;
...
@@ -3538,6 +3539,7 @@ class parser
...
@@ -3538,6 +3539,7 @@ class parser
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
using
string_t
=
typename
BasicJsonType
::
string_t
;
using
lexer_t
=
lexer
<
BasicJsonType
>
;
using
lexer_t
=
lexer
<
BasicJsonType
>
;
using
token_type
=
typename
lexer_t
::
token_type
;
using
token_type
=
typename
lexer_t
::
token_type
;
...
@@ -3710,7 +3712,7 @@ class parser
...
@@ -3710,7 +3712,7 @@ class parser
}
}
// parse values
// parse values
st
d
::
string
key
;
st
ring_t
key
;
BasicJsonType
value
;
BasicJsonType
value
;
while
(
true
)
while
(
true
)
{
{
...
@@ -5250,11 +5252,11 @@ class output_stream_adapter : public output_adapter_protocol<CharType>
...
@@ -5250,11 +5252,11 @@ class output_stream_adapter : public output_adapter_protocol<CharType>
};
};
/// output adapter for basic_string
/// output adapter for basic_string
template
<
typename
CharType
>
template
<
typename
CharType
,
typename
StringType
=
std
::
basic_string
<
CharType
>
>
class
output_string_adapter
:
public
output_adapter_protocol
<
CharType
>
class
output_string_adapter
:
public
output_adapter_protocol
<
CharType
>
{
{
public:
public:
explicit
output_string_adapter
(
std
::
basic_string
<
CharType
>
&
s
)
:
str
(
s
)
{}
explicit
output_string_adapter
(
StringType
&
s
)
:
str
(
s
)
{}
void
write_character
(
CharType
c
)
override
void
write_character
(
CharType
c
)
override
{
{
...
@@ -5267,10 +5269,10 @@ class output_string_adapter : public output_adapter_protocol<CharType>
...
@@ -5267,10 +5269,10 @@ class output_string_adapter : public output_adapter_protocol<CharType>
}
}
private:
private:
std
::
basic_string
<
CharType
>
&
str
;
StringType
&
str
;
};
};
template
<
typename
CharType
>
template
<
typename
CharType
,
typename
StringType
=
std
::
basic_string
<
CharType
>
>
class
output_adapter
class
output_adapter
{
{
public:
public:
...
@@ -5280,8 +5282,8 @@ class output_adapter
...
@@ -5280,8 +5282,8 @@ class output_adapter
output_adapter
(
std
::
basic_ostream
<
CharType
>&
s
)
output_adapter
(
std
::
basic_ostream
<
CharType
>&
s
)
:
oa
(
std
::
make_shared
<
output_stream_adapter
<
CharType
>>
(
s
))
{}
:
oa
(
std
::
make_shared
<
output_stream_adapter
<
CharType
>>
(
s
))
{}
output_adapter
(
std
::
basic_string
<
CharType
>
&
s
)
output_adapter
(
StringType
&
s
)
:
oa
(
std
::
make_shared
<
output_string_adapter
<
CharType
>>
(
s
))
{}
:
oa
(
std
::
make_shared
<
output_string_adapter
<
CharType
,
StringType
>>
(
s
))
{}
operator
output_adapter_t
<
CharType
>
()
operator
output_adapter_t
<
CharType
>
()
{
{
...
@@ -12060,7 +12062,7 @@ class basic_json
...
@@ -12060,7 +12062,7 @@ class basic_json
const
bool
ensure_ascii
=
false
)
const
const
bool
ensure_ascii
=
false
)
const
{
{
string_t
result
;
string_t
result
;
serializer
s
(
detail
::
output_adapter
<
char
>
(
result
),
indent_char
);
serializer
s
(
detail
::
output_adapter
<
char
,
string_t
>
(
result
),
indent_char
);
if
(
indent
>=
0
)
if
(
indent
>=
0
)
{
{
...
...
test/Makefile
View file @
abac6a0e
...
@@ -9,6 +9,7 @@ CPPFLAGS += -I ../single_include -I . -I thirdparty/catch -I thirdparty/fifo_map
...
@@ -9,6 +9,7 @@ CPPFLAGS += -I ../single_include -I . -I thirdparty/catch -I thirdparty/fifo_map
SOURCES
=
src/unit.cpp
\
SOURCES
=
src/unit.cpp
\
src/unit-algorithms.cpp
\
src/unit-algorithms.cpp
\
src/unit-allocator.cpp
\
src/unit-allocator.cpp
\
src/unit-alt-string.cpp
\
src/unit-capacity.cpp
\
src/unit-capacity.cpp
\
src/unit-cbor.cpp
\
src/unit-cbor.cpp
\
src/unit-class_const_iterator.cpp
\
src/unit-class_const_iterator.cpp
\
...
...
test/src/unit-alt-string.cpp
0 → 100644
View file @
abac6a0e
/*
__ _____ _____ _____
__| | __| | | | JSON for Modern C++ (test suite)
| | |__ | | | | | | version 3.1.1
|_____|_____|_____|_|___| https://github.com/nlohmann/json
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
Copyright (c) 2018 Vitaliy Manushkin <agri@akamo.info>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "catch.hpp"
#include <nlohmann/json.hpp>
#include <string>
#include <utility>
/*
* This is virtually a string class.
* It covers std::string under the hood.
*/
class
alt_string
{
public:
using
value_type
=
std
::
string
::
value_type
;
alt_string
(
const
char
*
str
)
:
str_impl
(
str
)
{}
alt_string
(
const
char
*
str
,
std
::
size_t
count
)
:
str_impl
(
str
,
count
)
{}
alt_string
(
size_t
count
,
char
chr
)
:
str_impl
(
count
,
chr
)
{}
alt_string
()
=
default
;
template
<
typename
...
TParams
>
alt_string
&
append
(
TParams
&&
...
params
)
{
str_impl
.
append
(
std
::
forward
<
TParams
>
(
params
)...);
return
*
this
;
}
void
push_back
(
char
c
)
{
str_impl
.
push_back
(
c
);
}
template
<
typename
op_type
>
bool
operator
==
(
op_type
&&
op
)
const
{
return
str_impl
==
op
;
}
template
<
typename
op_type
>
bool
operator
!=
(
op_type
&&
op
)
const
{
return
str_impl
!=
op
;
}
std
::
size_t
size
()
const
noexcept
{
return
str_impl
.
size
();
}
void
resize
(
std
::
size_t
n
)
{
str_impl
.
resize
(
n
);
}
void
resize
(
std
::
size_t
n
,
char
c
)
{
str_impl
.
resize
(
n
,
c
);
}
template
<
typename
op_type
>
bool
operator
<
(
op_type
&&
op
)
const
{
return
str_impl
<
op
;
}
bool
operator
<
(
const
alt_string
&
op
)
const
{
return
str_impl
<
op
.
str_impl
;
}
const
char
*
c_str
()
const
{
return
str_impl
.
c_str
();
}
char
&
operator
[](
std
::
size_t
index
)
{
return
str_impl
[
index
];
}
const
char
&
operator
[](
std
::
size_t
index
)
const
{
return
str_impl
[
index
];
}
char
&
back
()
{
return
str_impl
.
back
();
}
const
char
&
back
()
const
{
return
str_impl
.
back
();
}
void
clear
()
{
str_impl
.
clear
();
}
const
value_type
*
data
()
{
return
str_impl
.
data
();
}
private:
std
::
string
str_impl
;
};
using
alt_json
=
nlohmann
::
basic_json
<
std
::
map
,
std
::
vector
,
alt_string
,
bool
,
std
::
int64_t
,
std
::
uint64_t
,
double
,
std
::
allocator
,
nlohmann
::
adl_serializer
>
;
TEST_CASE
(
"alternative string type"
)
{
SECTION
(
"dump"
)
{
{
alt_json
doc
;
doc
[
"pi"
]
=
3.141
;
alt_string
dump
=
doc
.
dump
();
CHECK
(
dump
==
R"({"pi":3.141})"
);
}
{
alt_json
doc
;
doc
[
"happy"
]
=
true
;
alt_string
dump
=
doc
.
dump
();
CHECK
(
dump
==
R"({"happy":true})"
);
}
{
alt_json
doc
;
doc
[
"name"
]
=
"I'm Batman"
;
alt_string
dump
=
doc
.
dump
();
CHECK
(
dump
==
R"({"name":"I'm Batman"})"
);
}
{
alt_json
doc
;
doc
[
"nothing"
]
=
nullptr
;
alt_string
dump
=
doc
.
dump
();
CHECK
(
dump
==
R"({"nothing":null})"
);
}
{
alt_json
doc
;
doc
[
"answer"
][
"everything"
]
=
42
;
alt_string
dump
=
doc
.
dump
();
CHECK
(
dump
==
R"({"answer":{"everything":42}})"
);
}
{
alt_json
doc
;
doc
[
"list"
]
=
{
1
,
0
,
2
};
alt_string
dump
=
doc
.
dump
();
CHECK
(
dump
==
R"({"list":[1,0,2]})"
);
}
{
alt_json
doc
;
doc
[
"list"
]
=
{
1
,
0
,
2
};
alt_string
dump
=
doc
.
dump
();
CHECK
(
dump
==
R"({"list":[1,0,2]})"
);
}
}
SECTION
(
"parse"
)
{
auto
doc
=
alt_json
::
parse
(
"{
\"
foo
\"
:
\"
bar
\"
}"
);
alt_string
dump
=
doc
.
dump
();
CHECK
(
dump
==
R"({"foo":"bar"})"
);
}
}
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