Skip to content

Commit d2c2052

Browse files
committed
Rewrite and revert some code to build in pre-C++11
Rewerite and revert some code that this library can be built in pre-C++11 and C++11 env. Main Change List: 1. using -> typedef 2. not using auto & decltype 3. not using raw string literals 4. ..., other c++11 features will be chosen to compile, depending on env.
1 parent a4fb5db commit d2c2052

File tree

22 files changed

+690
-513
lines changed

22 files changed

+690
-513
lines changed

example/readFromStream/readFromStream.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "json/json.h"
2+
#include <cstdlib>
23
#include <fstream>
34
#include <iostream>
45
/** \brief Parse from stream, collect comments and capture error info.

example/readFromString/readFromString.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "json/json.h"
2+
#include <cstdlib>
23
#include <iostream>
34
/**
45
* \brief Parse a raw string into Value object using the CharReaderBuilder
@@ -10,9 +11,9 @@
1011
* 20
1112
*/
1213
int main() {
13-
const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
14-
const auto rawJsonLength = static_cast<int>(rawJson.length());
15-
constexpr bool shouldUseOldWay = false;
14+
const std::string rawJson = "{\"Age\": 20, \"Name\": \"colin\"}";
15+
const int rawJsonLength = static_cast<int>(rawJson.length());
16+
JSONCPP_CONST bool shouldUseOldWay = false;
1617
JSONCPP_STRING err;
1718
Json::Value root;
1819

@@ -21,12 +22,13 @@ int main() {
2122
reader.parse(rawJson, root);
2223
} else {
2324
Json::CharReaderBuilder builder;
24-
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
25+
Json::CharReader* reader(builder.newCharReader());
2526
if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
2627
&err)) {
2728
std::cout << "error" << std::endl;
2829
return EXIT_FAILURE;
2930
}
31+
delete reader;
3032
}
3133
const std::string name = root["Name"].asString();
3234
const int age = root["Age"].asInt();

example/streamWrite/streamWrite.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
int main() {
1313
Json::Value root;
1414
Json::StreamWriterBuilder builder;
15-
const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
15+
Json::StreamWriter* writer(builder.newStreamWriter());
1616

1717
root["Name"] = "robin";
1818
root["Age"] = 20;
1919
writer->write(root, &std::cout);
20-
20+
delete writer;
2121
return EXIT_SUCCESS;
2222
}

example/stringWrite/stringWrite.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "json/json.h"
2+
#include <cstdlib>
23
#include <iostream>
34
/** \brief Write a Value object to a string.
45
* Example Usage:
@@ -15,7 +16,7 @@
1516
int main() {
1617
Json::Value root;
1718
Json::Value data;
18-
constexpr bool shouldUseOldWay = false;
19+
JSONCPP_CONST bool shouldUseOldWay = false;
1920
root["action"] = "run";
2021
data["number"] = 1;
2122
root["data"] = data;

include/json/assertions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@
5858
} \
5959
} while (0)
6060

61+
#if JSONCPP_CXX_STD_11
62+
#define JSONCPP_STATIC_ASSERT static_assert
63+
#else
64+
#define JSONCPP_STATIC_ASSERT JSON_ASSERT_MESSAGE
65+
#endif
66+
6167
#endif // JSON_ASSERTIONS_H_INCLUDED

include/json/config.h

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55

66
#ifndef JSON_CONFIG_H_INCLUDED
77
#define JSON_CONFIG_H_INCLUDED
8-
#include <cstddef>
9-
#include <cstdint>
8+
109
#include <istream>
1110
#include <memory>
1211
#include <ostream>
1312
#include <sstream>
1413
#include <string>
15-
#include <type_traits>
14+
15+
#if JSONCPP_CXX_STD_11
16+
#include <cstddef> // typedef ptrdiff_t
17+
#include <cstdint> // typedef int64_t, uint64_t
18+
#else
19+
#include <stddef.h>
20+
#include <stdint.h>
21+
#endif
1622

1723
// If non-zero, the library uses exceptions to report bad input instead of C
1824
// assertion macros. The default is to use exceptions.
@@ -50,11 +56,6 @@
5056
#define JSON_API
5157
#endif
5258

53-
#if defined(_MSC_VER) && _MSC_VER < 1800
54-
#error \
55-
"ERROR: Visual Studio 12 (2013) with _MSC_VER=1800 is the oldest supported compiler with sufficient C++11 capabilities"
56-
#endif
57-
5859
#if defined(_MSC_VER) && _MSC_VER < 1900
5960
// As recommended at
6061
// https://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
@@ -70,10 +71,41 @@ extern JSON_API int msvc_pre1900_c99_snprintf(char* outBuf, size_t size,
7071
// Storages, and 64 bits integer support is disabled.
7172
// #define JSON_NO_INT64 1
7273

73-
// JSONCPP_OVERRIDE is maintained for backwards compatibility of external tools.
74-
// C++11 should be used directly in JSONCPP.
74+
#if __cplusplus >= 201103L || defined(_MSC_VER)
75+
#define JSONCPP_OP_EXPLICIT explicit
76+
#else
77+
#define JSONCPP_OP_EXPLICIT
78+
#endif
79+
80+
// These Macros are maintained for backwards compatibility of external tools.
81+
#if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
82+
(defined(__GNUC__) && __cplusplus >= 201103L) || \
83+
(defined(__clang__) && __clang_major__ == 3 && __clang_minor__ >= 3)
84+
85+
#define JSONCPP_CXX_STD_11 1
86+
#else
87+
#define JSONCPP_CXX_STD_11 0
88+
#endif
89+
90+
#if JSONCPP_CXX_STD_11
91+
#define JSONCPP_NULL nullptr
92+
#define JSONCPP_CONST constexpr
93+
#define JSONCPP_CTOR_DELETE = delete
94+
#define JSONCPP_NOEXCEPT noexcept
7595
#define JSONCPP_OVERRIDE override
96+
#define JSONCPP_MOVE(value) std::move(value)
97+
#else
98+
#define JSONCPP_NULL NULL
99+
#define JSONCPP_CONST const
100+
#define JSONCPP_CTOR_DELETE
101+
#define JSONCPP_NOEXCEPT throw()
102+
#define JSONCPP_OVERRIDE
103+
#define JSONCPP_MOVE(value) value
104+
#endif
76105

106+
// Define *deprecated* attribute
107+
// [[deprecated]] is in C++14 or in Visual Studio 2015 and later
108+
// For compatibility, [[deprecated]] is not used
77109
#ifdef __clang__
78110
#if __has_extension(attribute_deprecated_with_message)
79111
#define JSONCPP_DEPRECATED(message) __attribute__((deprecated(message)))
@@ -98,33 +130,36 @@ extern JSON_API int msvc_pre1900_c99_snprintf(char* outBuf, size_t size,
98130
#endif
99131

100132
#if !defined(JSON_IS_AMALGAMATION)
101-
133+
#if JSONCPP_CXX_STD_11
102134
#include "allocator.h"
135+
#endif
103136
#include "version.h"
104137

105138
#endif // if !defined(JSON_IS_AMALGAMATION)
106139

107140
namespace Json {
108-
using Int = int;
109-
using UInt = unsigned int;
141+
142+
typedef int Int;
143+
typedef unsigned int UInt;
110144
#if defined(JSON_NO_INT64)
111-
using LargestInt = int;
112-
using LargestUInt = unsigned int;
145+
typedef int LargestInt;
146+
typedef unsigned int LargestUInt;
113147
#undef JSON_HAS_INT64
114148
#else // if defined(JSON_NO_INT64)
115149
// For Microsoft Visual use specific types as long long is not supported
116150
#if defined(_MSC_VER) // Microsoft Visual Studio
117-
using Int64 = __int64;
118-
using UInt64 = unsigned __int64;
151+
typedef __int64 Int64;
152+
typedef unsigned __int64 UInt64;
119153
#else // if defined(_MSC_VER) // Other platforms, use long long
120-
using Int64 = int64_t;
121-
using UInt64 = uint64_t;
154+
typedef int64_t Int64;
155+
typedef uint64_t UInt64;
122156
#endif // if defined(_MSC_VER)
123-
using LargestInt = Int64;
124-
using LargestUInt = UInt64;
157+
typedef Int64 LargestInt;
158+
typedef UInt64 LargestUInt;
125159
#define JSON_HAS_INT64
126160
#endif // if defined(JSON_NO_INT64)
127161

162+
#if JSONCPP_CXX_STD_11
128163
template <typename T>
129164
using Allocator =
130165
typename std::conditional<JSONCPP_USING_SECURE_MEMORY, SecureAllocator<T>,
@@ -138,13 +173,20 @@ using OStringStream =
138173
String::allocator_type>;
139174
using IStream = std::istream;
140175
using OStream = std::ostream;
176+
#else
177+
typedef std::string String;
178+
typedef std::istringstream IStringStream;
179+
typedef std::ostringstream OStringStream;
180+
typedef std::istream IStream;
181+
typedef std::ostream OStream;
182+
#endif // JSONCPP_CXX_STD_11
141183
} // namespace Json
142184

143185
// Legacy names (formerly macros).
144-
using JSONCPP_STRING = Json::String;
145-
using JSONCPP_ISTRINGSTREAM = Json::IStringStream;
146-
using JSONCPP_OSTRINGSTREAM = Json::OStringStream;
147-
using JSONCPP_ISTREAM = Json::IStream;
148-
using JSONCPP_OSTREAM = Json::OStream;
186+
typedef Json::String JSONCPP_STRING;
187+
typedef Json::IStringStream JSONCPP_ISTRINGSTREAM;
188+
typedef Json::OStringStream JSONCPP_OSTRINGSTREAM;
189+
typedef Json::IStream JSONCPP_ISTREAM;
190+
typedef Json::OStream JSONCPP_OSTREAM;
149191

150192
#endif // JSON_CONFIG_H_INCLUDED

include/json/forwards.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CharReaderBuilder;
2929
class Features;
3030

3131
// value.h
32-
using ArrayIndex = unsigned int;
32+
typedef unsigned int ArrayIndex;
3333
class StaticString;
3434
class Path;
3535
class PathArgument;

include/json/json_features.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ class JSON_API Features {
4141
Features();
4242

4343
/// \c true if comments are allowed. Default: \c true.
44-
bool allowComments_{true};
44+
bool allowComments_;
4545

4646
/// \c true if root must be either an array or an object value. Default: \c
4747
/// false.
48-
bool strictRoot_{false};
48+
bool strictRoot_;
4949

5050
/// \c true if dropped null placeholders are allowed. Default: \c false.
51-
bool allowDroppedNullPlaceholders_{false};
51+
bool allowDroppedNullPlaceholders_;
5252

5353
/// \c true if numeric object key are allowed. Default: \c false.
54-
bool allowNumericKeys_{false};
54+
bool allowNumericKeys_;
5555
};
5656

5757
} // namespace Json

include/json/reader.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ namespace Json {
3636
class JSONCPP_DEPRECATED(
3737
"Use CharReader and CharReaderBuilder instead.") JSON_API Reader {
3838
public:
39-
using Char = char;
40-
using Location = const Char*;
39+
typedef char Char;
40+
typedef const Char* Location;
4141

4242
/** \brief An error tagged with where in the JSON text it was encountered.
4343
*
@@ -187,7 +187,7 @@ class JSONCPP_DEPRECATED(
187187
Location extra_;
188188
};
189189

190-
using Errors = std::deque<ErrorInfo>;
190+
typedef std::deque<ErrorInfo> Errors;
191191

192192
bool readToken(Token& token);
193193
void skipSpaces();
@@ -210,7 +210,8 @@ class JSONCPP_DEPRECATED(
210210
unsigned int& unicode);
211211
bool decodeUnicodeEscapeSequence(Token& token, Location& current,
212212
Location end, unsigned int& unicode);
213-
bool addError(const String& message, Token& token, Location extra = nullptr);
213+
bool addError(const String& message, Token& token,
214+
Location extra = JSONCPP_NULL);
214215
bool recoverFromError(TokenType skipUntilToken);
215216
bool addErrorAndRecover(const String& message, Token& token,
216217
TokenType skipUntilToken);
@@ -226,25 +227,25 @@ class JSONCPP_DEPRECATED(
226227
static bool containsNewLine(Location begin, Location end);
227228
static String normalizeEOL(Location begin, Location end);
228229

229-
using Nodes = std::stack<Value*>;
230+
typedef std::stack<Value*> Nodes;
230231
Nodes nodes_;
231232
Errors errors_;
232233
String document_;
233-
Location begin_{};
234-
Location end_{};
235-
Location current_{};
236-
Location lastValueEnd_{};
237-
Value* lastValue_{};
234+
Location begin_;
235+
Location end_;
236+
Location current_;
237+
Location lastValueEnd_;
238+
Value* lastValue_;
238239
String commentsBefore_;
239240
Features features_;
240-
bool collectComments_{};
241+
bool collectComments_;
241242
}; // Reader
242243

243244
/** Interface for reading JSON from a char array.
244245
*/
245246
class JSON_API CharReader {
246247
public:
247-
virtual ~CharReader() = default;
248+
virtual ~CharReader() {}
248249
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
249250
* document. The document must be a UTF-8 encoded string containing the
250251
* document to read.
@@ -266,7 +267,7 @@ class JSON_API CharReader {
266267

267268
class JSON_API Factory {
268269
public:
269-
virtual ~Factory() = default;
270+
virtual ~Factory() {}
270271
/** \brief Allocate a CharReader via operator new().
271272
* \throw std::exception if something goes wrong (e.g. invalid settings)
272273
*/
@@ -332,9 +333,9 @@ class JSON_API CharReaderBuilder : public CharReader::Factory {
332333
Json::Value settings_;
333334

334335
CharReaderBuilder();
335-
~CharReaderBuilder() override;
336+
~CharReaderBuilder() JSONCPP_OVERRIDE;
336337

337-
CharReader* newCharReader() const override;
338+
CharReader* newCharReader() const JSONCPP_OVERRIDE;
338339

339340
/** \return true if 'settings' are legal and consistent;
340341
* otherwise, indicate bad settings via 'invalid'.

0 commit comments

Comments
 (0)