Skip to content

Commit 210fa96

Browse files
committed
Latest JSON (from other work); tweaked certain GNDStk codes as necessary.
1 parent 7fe2c2b commit 210fa96

29 files changed

+1218
-1174
lines changed

autogen/json2class-fun.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ std::string replace(const std::string &str, const char from, const char to)
138138
// Stringify JSON
139139
std::string stringify(const json::value &j)
140140
{
141-
const std::string tmp = j.dump();
142-
return j.has<json::string>()
141+
std::ostringstream oss;
142+
j.write(oss);
143+
const std::string tmp = oss.str();
144+
return j.holds<json::string>()
143145
? tmp.substr(1, tmp.size()-2)
144146
: tmp;
145147
}
@@ -235,8 +237,8 @@ const std::string &nameGNDS(
235237
// As-is, directly as stipulated in the key in the JSON spec, except
236238
// that we allow a "name" entry in the key's value to override the key.
237239
return keyval.second.has("name")
238-
? keyval.second["name"].get<json::string>()
239-
: keyval.first;
240+
? (std::string &)keyval.second["name"].get<json::string>()
241+
: (std::string &)keyval.first;
240242
}
241243

242244
// nameField
@@ -376,7 +378,7 @@ void getClassMetadata(
376378
for (const auto &field : j) {
377379
if (beginsin(field.first, "//"))
378380
continue;
379-
if (!field.second.has<json::object>())
381+
if (!field.second.holds<json::object>())
380382
continue;
381383
const json::object &metaRHS = field.second.get<json::object>();
382384

@@ -395,7 +397,7 @@ void getClassMetadata(
395397

396398
// Has default?
397399
m.defaultValue = "";
398-
if (metaRHS.has("default") && !metaRHS["default"].is_null()) {
400+
if (metaRHS.has("default") && !metaRHS["default"].holds<json::null>()) {
399401
m.defaultValue = stringify(metaRHS["default"]);
400402
// Apply the "changes.json" change, if any, to the given value
401403
const auto it = specs.mapMetaDefault.find(m.defaultValue);
@@ -452,7 +454,7 @@ void getClassChildren(
452454
for (const auto &field : j) {
453455
if (beginsin(field.first, "//"))
454456
continue;
455-
if (!field.second.has<json::object>())
457+
if (!field.second.holds<json::object>())
456458
continue;
457459
const json::object &elemRHS = field.second.get<json::object>();
458460

@@ -549,7 +551,7 @@ void getClassVariants(
549551
continue;
550552

551553
// Is it a choice child?
552-
if (!field.second.has<json::object>())
554+
if (!field.second.holds<json::object>())
553555
continue;
554556
const json::object &elemRHS = field.second.get<json::object>();
555557
const std::string &times = getTimes(per,field.first,elemRHS);
@@ -569,7 +571,7 @@ void getClassVariants(
569571
continue;
570572

571573
// Is it a choice child?
572-
if (!field.second.has<json::object>())
574+
if (!field.second.holds<json::object>())
573575
continue;
574576
const json::object &elemRHS = field.second.get<json::object>();
575577
const std::string &times = getTimes(per,field.first,elemRHS);
@@ -712,7 +714,7 @@ void readChangesFile(const std::string &file, InfoSpecs &specs)
712714

713715
// Changes to name?
714716
if (jchanges.has("name"))
715-
for (const auto &item : jchanges["name"].items())
717+
for (const auto &item : jchanges["name"].pairs())
716718
if (!isComment(item.first))
717719
specs.mapName.insert(
718720
pair(item.first, item.second.get<json::string>())
@@ -723,14 +725,14 @@ void readChangesFile(const std::string &file, InfoSpecs &specs)
723725

724726
// from/to pairs for "type"
725727
if (metadata.has("type"))
726-
for (const auto &item : metadata["type"].items())
728+
for (const auto &item : metadata["type"].pairs())
727729
if (!isComment(item.first))
728730
specs.mapMetaType.insert(
729731
pair(item.first,item.second.get<json::string>())
730732
);
731733
// from/to pairs for "default"
732734
if (metadata.has("default"))
733-
for (const auto &item : metadata["default"].items())
735+
for (const auto &item : metadata["default"].pairs())
734736
if (!isComment(item.first))
735737
specs.mapMetaDefault.insert(
736738
pair(item.first,item.second.get<json::string>())
@@ -749,17 +751,17 @@ void printSingletons(const std::string &file)
749751
for (const auto &item : jfile) {
750752
if (beginsin(item.first, "//"))
751753
continue;
752-
if (!item.second.has<json::object>())
754+
if (!item.second.holds<json::object>())
753755
continue;
754756
const json::object &rhs = item.second.get<json::object>();
755757

756758
if (!isClass(item))
757759
continue;
758760

759761
const bool hasdata =
760-
(rhs.has("string" ) && !rhs["string" ].is_null()) ||
761-
(rhs.has("vector" ) && !rhs["vector" ].is_null()) ||
762-
(rhs.has("bodyText") && !rhs["bodyText"].is_null());
762+
(rhs.has("string" ) && !rhs["string" ].holds<json::null>()) ||
763+
(rhs.has("vector" ) && !rhs["vector" ].holds<json::null>()) ||
764+
(rhs.has("bodyText") && !rhs["bodyText"].holds<json::null>());
763765

764766
const json::object metadata = getMetadataJSON(rhs);
765767
const json::object children = getChildrenJSON(rhs);
@@ -1114,9 +1116,9 @@ void getClass(
11141116

11151117
// data-node information
11161118
const bool
1117-
str = classRHS.has("string" ) && !classRHS["string" ].is_null(),
1118-
vec = classRHS.has("vector" ) && !classRHS["vector" ].is_null(),
1119-
body = classRHS.has("bodyText") && !classRHS["bodyText"].is_null();
1119+
str = classRHS.has("string" ) && !classRHS["string" ].holds<json::null>(),
1120+
vec = classRHS.has("vector" ) && !classRHS["vector" ].holds<json::null>(),
1121+
body = classRHS.has("bodyText") && !classRHS["bodyText"].holds<json::null>();
11201122
assert(int(str) + int(vec) + int(body) <= 1); // no more than one
11211123

11221124
per.isDataString = str;

simple-json/src/json-array.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
class array : public std::vector<value> {
88
public:
9+
JSON_IO(array);
910

1011
// ------------------------
1112
// Construction
@@ -18,7 +19,7 @@ class array : public std::vector<value> {
1819
array(const vector &from) : vector(from) { }
1920
array(vector &&from) : vector(std::move(from)) { }
2021

21-
// constructor: from std::vector<T convertible to value>
22+
// from std::vector<T convertible to value>
2223
template<class T, class = require<convertible<T,value>>>
2324
array(const std::vector<T> &from) :
2425
vector(from.begin(), from.end())
@@ -31,17 +32,14 @@ class array : public std::vector<value> {
3132
template<class T, class = require<assignable<vector, T &&>>>
3233
array &operator=(T &&from)
3334
{
34-
vector::operator=(std::forward<T>(from));
35-
return *this;
35+
return vector::operator=(std::forward<T>(from)), *this;
3636
}
3737

3838
// ------------------------
39-
// read, write
39+
// Other
4040
// ------------------------
4141

42-
template<class T = void, class U = void>
43-
auto /* std::string */ read(std::istream &is, const int = as_literal::none)
44-
-> decltype(number().read<T,U>(is,0)); // SFINAE: need number::read<T,U>
45-
46-
void write(std::ostream & = std::cout, const int = 0, const int = -1) const;
42+
// values
43+
const std::vector<value> &values() const { return *this; }
44+
std::vector<value> &values() { return *this; }
4745
};

simple-json/src/json-boolean.hpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class boolean {
88
bool b;
99

1010
public:
11+
JSON_IO(boolean);
1112

1213
// ------------------------
1314
// Construction
@@ -26,14 +27,5 @@ class boolean {
2627

2728
// to bool
2829
operator const bool &() const { return b; }
29-
operator bool &() { return b; }
30-
31-
// ------------------------
32-
// read, write
33-
// ------------------------
34-
35-
template<class T = void, class U = void>
36-
std::string read(std::istream &, const int = as_literal::none);
37-
38-
void write(std::ostream & = std::cout, const int = 0, const int = -1) const;
30+
operator bool &() { return b; }
3931
};

simple-json/src/json-chars.hpp

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@
33
// chars
44
// -----------------------------------------------------------------------------
55

6-
// Remark. <charconv> is supposed to have:
7-
// enum class chars_format {
8-
// scientific = ...,
9-
// fixed = ...,
10-
// hex = ...,
11-
// general = fixed | scientific
12-
// };
13-
// At the time of this writing, g++ appears to have it, but clang++ does not.
14-
15-
template<class = JSON_FLOATING>
166
class chars {
177
std::string str;
188

199
// SIZE here is overkill, but should be adequate
20-
static inline const usize SIZE = 1000;
21-
static inline char buf[SIZE];
10+
static inline constexpr size_t SIZE = 100;
11+
static inline char buffer[SIZE];
2212

2313
public:
2414

@@ -29,24 +19,47 @@ class chars {
2919
// default
3020
chars() { }
3121

32-
// from float, double, or long double
33-
template<class T, class = require<floating<T> && allowed<T,number::variant>>>
22+
// from any T in number::variant
23+
template<class T, class = require<invar<T,number::variant>>>
3424
chars(
35-
const T &from,
36-
const std::chars_format format = std::chars_format::general
37-
) :
38-
str(buf, std::to_chars(buf,buf+SIZE,from,format).ptr - buf)
39-
{ }
25+
#ifdef JSON_CHARS
26+
const T &from, const std::chars_format &format = json::format
27+
#else
28+
const T &from
29+
#endif
30+
) {
31+
std::to_chars_result res;
32+
33+
if constexpr (integral<T>)
34+
res = same<T,unsigned char>
35+
? std::to_chars(buffer, buffer+SIZE, (unsigned short)from)
36+
: same<T, signed char>
37+
? std::to_chars(buffer, buffer+SIZE, ( signed short)from)
38+
: std::to_chars(buffer, buffer+SIZE, from);
39+
else {
40+
#ifdef JSON_CHARS
41+
res = std::to_chars(buffer, buffer+SIZE, from, format);
42+
#else
43+
assert(false);
44+
#endif
45+
}
46+
47+
if (res.ec == std::errc()) // std::errc() == default => no error
48+
str = (*res.ptr = '\0', buffer);
49+
else
50+
error("Error returned by std::to_chars(). Message is:\n" +
51+
std::make_error_code(res.ec).message() + '.');
52+
}
4053

4154
// ------------------------
4255
// Conversion
4356
// ------------------------
4457

4558
// to std::string
4659
operator const std::string &() const { return str; }
47-
operator std::string &() { return str; }
60+
operator std::string &() { return str; }
4861

49-
// to literal
62+
// to literal (whose construction from std::string is explicit)
5063
operator literal() const
5164
{
5265
return literal(str);

0 commit comments

Comments
 (0)