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
1fbb82de
Unverified
Commit
1fbb82de
authored
Jun 17, 2017
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
using std::array to buffer input
parent
889006f0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
23 deletions
+24
-23
src/json.hpp
src/json.hpp
+24
-21
test/src/unit-regression.cpp
test/src/unit-regression.cpp
+0
-2
No files found.
src/json.hpp
View file @
1fbb82de
...
@@ -8761,15 +8761,15 @@ class basic_json
...
@@ -8761,15 +8761,15 @@ class basic_json
// native support
// native support
/// input adapter for input stream
/// input adapter for input stream
static
std
::
shared_ptr
<
input_adapter
>
create
(
std
::
istream
&
i
,
const
size_t
buffer_size
=
16384
)
static
std
::
shared_ptr
<
input_adapter
>
create
(
std
::
istream
&
i
)
{
{
return
std
::
shared_ptr
<
input_adapter
>
(
new
cached_input_stream_adapter
(
i
,
buffer_size
));
return
std
::
shared_ptr
<
input_adapter
>
(
new
cached_input_stream_adapter
<
16384
>
(
i
));
}
}
/// input adapter for input stream
/// input adapter for input stream
static
std
::
shared_ptr
<
input_adapter
>
create
(
std
::
istream
&&
i
,
const
size_t
buffer_size
=
16384
)
static
std
::
shared_ptr
<
input_adapter
>
create
(
std
::
istream
&&
i
)
{
{
return
std
::
shared_ptr
<
input_adapter
>
(
new
cached_input_stream_adapter
(
i
,
buffer_size
));
return
std
::
shared_ptr
<
input_adapter
>
(
new
cached_input_stream_adapter
<
16384
>
(
i
));
}
}
/// input adapter for buffer
/// input adapter for buffer
...
@@ -8841,11 +8841,12 @@ class basic_json
...
@@ -8841,11 +8841,12 @@ class basic_json
using
input_adapter_t
=
std
::
shared_ptr
<
input_adapter
>
;
using
input_adapter_t
=
std
::
shared_ptr
<
input_adapter
>
;
/// input adapter for cached stream input
/// input adapter for cached stream input
template
<
std
::
size_t
N
>
class
cached_input_stream_adapter
:
public
input_adapter
class
cached_input_stream_adapter
:
public
input_adapter
{
{
public:
public:
cached_input_stream_adapter
(
std
::
istream
&
i
,
const
size_t
buffer_size
)
cached_input_stream_adapter
(
std
::
istream
&
i
)
:
is
(
i
),
start_position
(
is
.
tellg
())
,
buffer
(
buffer_size
,
'\0'
)
:
is
(
i
),
start_position
(
is
.
tellg
())
{
{
// immediately abort if stream is erroneous
// immediately abort if stream is erroneous
if
(
JSON_UNLIKELY
(
i
.
fail
()))
if
(
JSON_UNLIKELY
(
i
.
fail
()))
...
@@ -8853,10 +8854,7 @@ class basic_json
...
@@ -8853,10 +8854,7 @@ class basic_json
JSON_THROW
(
parse_error
::
create
(
111
,
0
,
"bad input stream"
));
JSON_THROW
(
parse_error
::
create
(
111
,
0
,
"bad input stream"
));
}
}
// initial fill
fill_buffer
();
is
.
read
(
buffer
.
data
(),
static_cast
<
std
::
streamsize
>
(
buffer
.
size
()));
// store number of bytes in the buffer
fill_size
=
static_cast
<
size_t
>
(
is
.
gcount
());
// skip byte order mark
// skip byte order mark
if
(
fill_size
>=
3
and
buffer
[
0
]
==
'\xEF'
and
buffer
[
1
]
==
'\xBB'
and
buffer
[
2
]
==
'\xBF'
)
if
(
fill_size
>=
3
and
buffer
[
0
]
==
'\xEF'
and
buffer
[
1
]
==
'\xBB'
and
buffer
[
2
]
==
'\xBF'
)
...
@@ -8884,20 +8882,17 @@ class basic_json
...
@@ -8884,20 +8882,17 @@ class basic_json
// check if refilling is necessary and possible
// check if refilling is necessary and possible
if
(
buffer_pos
==
fill_size
and
not
eof
)
if
(
buffer_pos
==
fill_size
and
not
eof
)
{
{
// refill
fill_buffer
();
is
.
read
(
buffer
.
data
(),
static_cast
<
std
::
streamsize
>
(
buffer
.
size
()));
// store number of bytes in the buffer
fill_size
=
static_cast
<
size_t
>
(
is
.
gcount
());
// the buffer is ready
buffer_pos
=
0
;
// remember that filling did not yield new input
//
check and
remember that filling did not yield new input
if
(
fill_size
==
0
)
if
(
fill_size
==
0
)
{
{
eof
=
true
;
eof
=
true
;
return
std
::
char_traits
<
char
>::
eof
();
return
std
::
char_traits
<
char
>::
eof
();
}
}
// the buffer is ready
buffer_pos
=
0
;
}
}
++
processed_chars
;
++
processed_chars
;
...
@@ -8910,9 +8905,9 @@ class basic_json
...
@@ -8910,9 +8905,9 @@ class basic_json
std
::
string
result
(
length
,
'\0'
);
std
::
string
result
(
length
,
'\0'
);
// save stream position
// save stream position
auto
current_pos
=
is
.
tellg
();
const
auto
current_pos
=
is
.
tellg
();
// save stream flags
// save stream flags
auto
flags
=
is
.
rdstate
();
const
auto
flags
=
is
.
rdstate
();
// clear stream flags
// clear stream flags
is
.
clear
();
is
.
clear
();
...
@@ -8930,6 +8925,14 @@ class basic_json
...
@@ -8930,6 +8925,14 @@ class basic_json
}
}
private:
private:
void
fill_buffer
()
{
// fill
is
.
read
(
buffer
.
data
(),
static_cast
<
std
::
streamsize
>
(
buffer
.
size
()));
// store number of bytes in the buffer
fill_size
=
static_cast
<
size_t
>
(
is
.
gcount
());
}
/// the associated input stream
/// the associated input stream
std
::
istream
&
is
;
std
::
istream
&
is
;
...
@@ -8947,7 +8950,7 @@ class basic_json
...
@@ -8947,7 +8950,7 @@ class basic_json
const
std
::
streampos
start_position
;
const
std
::
streampos
start_position
;
/// internal buffer
/// internal buffer
std
::
vector
<
char
>
buffer
;
std
::
array
<
char
,
N
>
buffer
;
};
};
/// input adapter for buffer input
/// input adapter for buffer input
...
...
test/src/unit-regression.cpp
View file @
1fbb82de
...
@@ -1175,7 +1175,6 @@ TEST_CASE("regression tests")
...
@@ -1175,7 +1175,6 @@ TEST_CASE("regression tests")
// create and print a JSON from the map
// create and print a JSON from the map
json
j
=
m1
;
json
j
=
m1
;
std
::
cout
<<
j
<<
std
::
endl
;
// get the map out of JSON
// get the map out of JSON
std
::
map
<
std
::
string
,
int
>
m2
=
j
;
std
::
map
<
std
::
string
,
int
>
m2
=
j
;
...
@@ -1191,7 +1190,6 @@ TEST_CASE("regression tests")
...
@@ -1191,7 +1190,6 @@ TEST_CASE("regression tests")
// create and print a JSON from the map
// create and print a JSON from the map
json
j
=
m1
;
json
j
=
m1
;
std
::
cout
<<
j
<<
std
::
endl
;
// get the map out of JSON
// get the map out of JSON
std
::
map
<
std
::
string
,
std
::
string
>
m2
=
j
;
std
::
map
<
std
::
string
,
std
::
string
>
m2
=
j
;
...
...
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