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
5d390e91
Unverified
Commit
5d390e91
authored
Dec 16, 2018
by
Niels Lohmann
Committed by
GitHub
Dec 16, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1392 from mtalliance/feature/addFileInputAdapter
Feature/add file input adapter
parents
e73dfef6
c1c85b02
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
83 additions
and
2 deletions
+83
-2
include/nlohmann/detail/input/input_adapters.hpp
include/nlohmann/detail/input/input_adapters.hpp
+24
-1
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+24
-1
test/src/unit-testsuites.cpp
test/src/unit-testsuites.cpp
+35
-0
No files found.
include/nlohmann/detail/input/input_adapters.hpp
View file @
5d390e91
...
...
@@ -10,6 +10,7 @@
#include <string> // string, char_traits
#include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
#include <utility> // pair, declval
#include <cstdio> //FILE *
#include <nlohmann/detail/macro_scope.hpp>
...
...
@@ -45,6 +46,27 @@ struct input_adapter_protocol
/// a type to simplify interfaces
using
input_adapter_t
=
std
::
shared_ptr
<
input_adapter_protocol
>
;
/*!
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
buffer. This adapter is a very low level adapter.
*/
class
file_input_adapter
:
public
input_adapter_protocol
{
public:
explicit
file_input_adapter
(
std
::
FILE
*
f
)
noexcept
:
m_file
(
f
)
{}
std
::
char_traits
<
char
>::
int_type
get_character
()
noexcept
override
{
return
std
::
fgetc
(
m_file
);
}
private:
/// the file pointer to read from
std
::
FILE
*
m_file
;
};
/*!
Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
beginning of input. Does not support changing the underlying std::streambuf
...
...
@@ -293,7 +315,8 @@ class input_adapter
{
public:
// native support
input_adapter
(
std
::
FILE
*
file
)
:
ia
(
std
::
make_shared
<
file_input_adapter
>
(
file
))
{}
/// input adapter for input stream
input_adapter
(
std
::
istream
&
i
)
:
ia
(
std
::
make_shared
<
input_stream_adapter
>
(
i
))
{}
...
...
single_include/nlohmann/json.hpp
View file @
5d390e91
...
...
@@ -2058,6 +2058,7 @@ constexpr const auto& to_json = detail::static_const<detail::to_json_fn>::value;
#include <string> // string, char_traits
#include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
#include <utility> // pair, declval
#include <cstdio> //FILE *
// #include <nlohmann/detail/macro_scope.hpp>
...
...
@@ -2094,6 +2095,27 @@ struct input_adapter_protocol
/// a type to simplify interfaces
using
input_adapter_t
=
std
::
shared_ptr
<
input_adapter_protocol
>
;
/*!
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
buffer. This adapter is a very low level adapter.
*/
class
file_input_adapter
:
public
input_adapter_protocol
{
public:
explicit
file_input_adapter
(
std
::
FILE
*
f
)
noexcept
:
m_file
(
f
)
{}
std
::
char_traits
<
char
>::
int_type
get_character
()
noexcept
override
{
return
std
::
fgetc
(
m_file
);
}
private:
/// the file pointer to read from
std
::
FILE
*
m_file
;
};
/*!
Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
beginning of input. Does not support changing the underlying std::streambuf
...
...
@@ -2342,7 +2364,8 @@ class input_adapter
{
public:
// native support
input_adapter
(
std
::
FILE
*
file
)
:
ia
(
std
::
make_shared
<
file_input_adapter
>
(
file
))
{}
/// input adapter for input stream
input_adapter
(
std
::
istream
&
i
)
:
ia
(
std
::
make_shared
<
input_stream_adapter
>
(
i
))
{}
...
...
test/src/unit-testsuites.cpp
View file @
5d390e91
...
...
@@ -384,6 +384,41 @@ TEST_CASE("json.org examples")
json
j
;
CHECK_NOTHROW
(
f
>>
j
);
}
SECTION
(
"FILE 1.json"
)
{
std
::
unique_ptr
<
std
::
FILE
,
decltype
(
&
std
::
fclose
)
>
f
(
std
::
fopen
(
"test/data/json.org/1.json"
,
"r"
),
&
std
::
fclose
);
json
j
;
CHECK_NOTHROW
(
j
.
parse
(
f
.
get
()));
}
SECTION
(
"FILE 2.json"
)
{
std
::
unique_ptr
<
std
::
FILE
,
decltype
(
&
std
::
fclose
)
>
f
(
std
::
fopen
(
"test/data/json.org/2.json"
,
"r"
),
&
std
::
fclose
);
json
j
;
CHECK_NOTHROW
(
j
.
parse
(
f
.
get
()));
}
SECTION
(
"FILE 3.json"
)
{
std
::
unique_ptr
<
std
::
FILE
,
decltype
(
&
std
::
fclose
)
>
f
(
std
::
fopen
(
"test/data/json.org/3.json"
,
"r"
),
&
std
::
fclose
);
json
j
;
CHECK_NOTHROW
(
j
.
parse
(
f
.
get
()));
}
SECTION
(
"FILE 4.json"
)
{
std
::
unique_ptr
<
std
::
FILE
,
decltype
(
&
std
::
fclose
)
>
f
(
std
::
fopen
(
"test/data/json.org/4.json"
,
"r"
),
&
std
::
fclose
);
json
j
;
CHECK_NOTHROW
(
j
.
parse
(
f
.
get
()));
}
SECTION
(
"FILE 5.json"
)
{
std
::
unique_ptr
<
std
::
FILE
,
decltype
(
&
std
::
fclose
)
>
f
(
std
::
fopen
(
"test/data/json.org/5.json"
,
"r"
),
&
std
::
fclose
);
json
j
;
CHECK_NOTHROW
(
j
.
parse
(
f
.
get
()));
}
}
TEST_CASE
(
"RFC 7159 examples"
)
...
...
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