mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2026-01-13 11:13:16 +01:00
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.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "json/json.h"
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
/** \brief Parse from stream, collect comments and capture error info.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "json/json.h"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
/**
|
||||
* \brief Parse a raw string into Value object using the CharReaderBuilder
|
||||
@@ -10,9 +11,9 @@
|
||||
* 20
|
||||
*/
|
||||
int main() {
|
||||
const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
|
||||
const auto rawJsonLength = static_cast<int>(rawJson.length());
|
||||
constexpr bool shouldUseOldWay = false;
|
||||
const std::string rawJson = "{\"Age\": 20, \"Name\": \"colin\"}";
|
||||
const int rawJsonLength = static_cast<int>(rawJson.length());
|
||||
JSONCPP_CONST bool shouldUseOldWay = false;
|
||||
JSONCPP_STRING err;
|
||||
Json::Value root;
|
||||
|
||||
@@ -21,12 +22,13 @@ int main() {
|
||||
reader.parse(rawJson, root);
|
||||
} else {
|
||||
Json::CharReaderBuilder builder;
|
||||
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
|
||||
Json::CharReader* reader(builder.newCharReader());
|
||||
if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
|
||||
&err)) {
|
||||
std::cout << "error" << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
delete reader;
|
||||
}
|
||||
const std::string name = root["Name"].asString();
|
||||
const int age = root["Age"].asInt();
|
||||
|
||||
@@ -12,11 +12,11 @@
|
||||
int main() {
|
||||
Json::Value root;
|
||||
Json::StreamWriterBuilder builder;
|
||||
const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
|
||||
Json::StreamWriter* writer(builder.newStreamWriter());
|
||||
|
||||
root["Name"] = "robin";
|
||||
root["Age"] = 20;
|
||||
writer->write(root, &std::cout);
|
||||
|
||||
delete writer;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "json/json.h"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
/** \brief Write a Value object to a string.
|
||||
* Example Usage:
|
||||
@@ -15,7 +16,7 @@
|
||||
int main() {
|
||||
Json::Value root;
|
||||
Json::Value data;
|
||||
constexpr bool shouldUseOldWay = false;
|
||||
JSONCPP_CONST bool shouldUseOldWay = false;
|
||||
root["action"] = "run";
|
||||
data["number"] = 1;
|
||||
root["data"] = data;
|
||||
|
||||
Reference in New Issue
Block a user