Skip to content

Commit 164db27

Browse files
committed
simplifying the API to be in terms of a string_view
1 parent 1e3bbec commit 164db27

File tree

4 files changed

+51
-61
lines changed

4 files changed

+51
-61
lines changed

lib/include/cpp-json/json.h

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ inline bool has_key(const value &v, const std::string &key) noexcept;
7070
inline bool has_key(const object &o, const std::string &key) noexcept;
7171

7272
// create a value from some JSON
73-
template <class In>
74-
inline value parse(In first, In last);
75-
inline value parse(std::istream &is);
76-
inline value parse(std::istream &&is);
7773
inline value parse(std::string_view s);
7874

7975
// convert a value to a JSON string
@@ -416,7 +412,6 @@ class object {
416412
friend bool operator==(const object &lhs, const object &rhs) noexcept;
417413
friend bool operator!=(const object &lhs, const object &rhs) noexcept;
418414

419-
template <class In>
420415
friend class parser;
421416

422417
private:
@@ -661,7 +656,6 @@ class value {
661656
friend bool operator==(const value &lhs, const value &rhs);
662657
friend bool operator!=(const value &lhs, const value &rhs);
663658

664-
template <class In>
665659
friend class parser;
666660

667661
private:
@@ -915,11 +909,10 @@ inline value &array::at(std::size_t n) {
915909
/**
916910
* @brief The parser class
917911
*/
918-
template <class In>
919912
class parser {
920913
public:
921-
parser(In first, In last)
922-
: begin_(first), cur_(first), end_(last) {
914+
parser(std::string_view s)
915+
: begin_(s.begin()), cur_(s.begin()), end_(s.end()) {
923916
}
924917

925918
public:
@@ -1132,28 +1125,14 @@ class parser {
11321125
}
11331126

11341127
private:
1135-
In begin_;
1136-
In cur_;
1137-
In end_;
1128+
std::string_view::const_iterator begin_;
1129+
std::string_view::const_iterator cur_;
1130+
std::string_view::const_iterator end_;
11381131

11391132
int line_ = 1;
11401133
int column_ = 0;
11411134
};
11421135

1143-
template <class In>
1144-
value parse(In first, In last) {
1145-
1146-
parser<In> p(first, last);
1147-
1148-
try {
1149-
return p.parse();
1150-
} catch (exception &e) {
1151-
e.line = p.line();
1152-
e.column = p.column();
1153-
throw;
1154-
}
1155-
}
1156-
11571136
inline std::string to_string(const value &v) {
11581137
return as_string(v);
11591138
}
@@ -1253,16 +1232,16 @@ inline bool has_key(const object &o, const std::string &key) noexcept {
12531232
return o.find(key) != o.end();
12541233
}
12551234

1256-
inline value parse(std::istream &&is) {
1257-
return parse(is);
1258-
}
1259-
1260-
inline value parse(std::istream &is) {
1261-
return parse(std::istreambuf_iterator<char>{is}, std::istreambuf_iterator<char>{});
1262-
}
1263-
12641235
inline value parse(std::string_view s) {
1265-
return parse(s.begin(), s.end());
1236+
parser p(s);
1237+
1238+
try {
1239+
return p.parse();
1240+
} catch (exception &e) {
1241+
e.line = p.line();
1242+
e.column = p.column();
1243+
throw;
1244+
}
12661245
}
12671246

12681247
inline bool is_string(const value &v) noexcept {
@@ -2123,11 +2102,10 @@ inline bool operator!=(const array &lhs, const array &rhs) noexcept {
21232102
}
21242103

21252104
/**
2126-
* @brief parser<In>::get_string
2105+
* @brief parser::get_string
21272106
* @return
21282107
*/
2129-
template <class In>
2130-
std::string parser<In>::get_string() {
2108+
std::string parser::get_string() {
21312109

21322110
if (read() != Quote) {
21332111
throw string_expected();
@@ -2229,11 +2207,10 @@ std::string parser<In>::get_string() {
22292207
}
22302208

22312209
/**
2232-
* @brief parser<In>::get_number
2210+
* @brief parser::get_number
22332211
* @return
22342212
*/
2235-
template <class In>
2236-
std::string parser<In>::get_number() {
2213+
std::string parser::get_number() {
22372214
std::string s;
22382215
s.reserve(10);
22392216
std::back_insert_iterator<std::string> out = back_inserter(s);

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ target_compile_options(example3 PUBLIC -pedantic -W -Wall -Wmissing-field-initia
1919
add_test(
2020
NAME example1
2121
COMMAND $<TARGET_FILE:example1>
22+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/test/
2223
)
2324

2425
add_test(

test/example1.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

22
#include "cpp-json/json.h"
3-
#include <iostream>
3+
#include <cassert>
44
#include <fstream>
5+
#include <iostream>
6+
#include <iterator>
57

68
/**
79
* @brief main
@@ -11,8 +13,15 @@ int main() {
1113

1214
// construct from a file
1315
std::ifstream file("example1.json");
14-
if(file) {
15-
auto v1 = json::parse(file);
16-
std::cout << stringify(v1, json::PrettyPrint | json::EscapeUnicode) << '\n';
17-
}
16+
assert(file);
17+
18+
std::string s{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
19+
auto v1 = json::parse(s);
20+
std::cout << stringify(v1, json::PrettyPrint | json::EscapeUnicode) << '\n';
21+
22+
assert(json::to_number<int>(v1["test1"]) == 1);
23+
assert(json::to_bool(v1["b1"]) == true);
24+
assert(json::to_bool(v1["b2"]) == false);
25+
assert(json::is_null(v1["n1"]));
26+
assert(json::to_number<int>(v1["n2"]) == -1234);
1827
}

test/example1.json

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
{
2-
"test1":1,
3-
"object1":{
4-
"object2":{
5-
"test2":2,
6-
"object3":{
7-
"test3":3,
8-
"test4":[1,2,3,4]
2+
"test1": 1,
3+
"object1": {
4+
"object2": {
5+
"test2": 2,
6+
"object3": {
7+
"test3": 3,
8+
"test4": [
9+
1,
10+
2,
11+
3,
12+
4
13+
]
914
}
1015
}
1116
},
12-
"test\nreturn":"hello\tworld",
13-
"u":"\u1234",
14-
"b1":true,
15-
"b2":false,
16-
"n1":null,
17-
"n2":-1234
18-
17+
"test\nreturn": "hello\tworld",
18+
"u": "\u1234",
19+
"b1": true,
20+
"b2": false,
21+
"n1": null,
22+
"n2": -1234
1923
}
20-

0 commit comments

Comments
 (0)