From 56fc9a99db4fc299a15bb7f2f2d712645e8de514 Mon Sep 17 00:00:00 2001
From: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
Date: Wed, 29 Nov 2017 15:36:05 -0500
Subject: [PATCH] use new jsoncpp API
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Use latest jsoncpp API to remove deprecation warning.

Change-Id: Id599e1e30cf201f4027ee10fa4229f14b93264e6
Reviewed-by: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
---
 src/archiver.cpp               | 18 +++++++++++-------
 src/im/message_engine.cpp      | 10 +++++-----
 src/ringdht/accountarchive.cpp | 16 ++++++++++------
 src/ringdht/namedirectory.cpp  | 15 +++++++++------
 src/ringdht/ringaccount.cpp    |  7 +++++--
 5 files changed, 40 insertions(+), 26 deletions(-)

diff --git a/src/archiver.cpp b/src/archiver.cpp
index 4c3c8631a6..78ece35c50 100644
--- a/src/archiver.cpp
+++ b/src/archiver.cpp
@@ -122,8 +122,10 @@ exportAccounts(std::vector<std::string> accountIDs,
         array.append(jsonAccount);
     }
     root["accounts"] = array;
-    Json::FastWriter fastWriter;
-    std::string output = fastWriter.write(root);
+    Json::StreamWriterBuilder wbuilder;
+    wbuilder["commentStyle"] = "None";
+    wbuilder["indentation"] = "";
+    auto output = Json::writeString(wbuilder, root);
 
     // Compress
     std::vector<uint8_t> compressed;
@@ -181,14 +183,16 @@ importAccounts(std::string archivePath, std::string password)
     }
 
     try {
-        // Decode string
-        std::string decoded {file.begin(), file.end()};
+        const auto* char_file_begin = reinterpret_cast<const char*>(&file[0]);
+        const auto* char_file_end = reinterpret_cast<const char*>(&file[file.size()]);
 
         // Add
+        std::string err;
         Json::Value root;
-        Json::Reader reader;
-        if (!reader.parse(decoded.c_str(),root)) {
-            RING_ERR("Failed to parse %s", reader.getFormattedErrorMessages().c_str());
+        Json::CharReaderBuilder rbuilder;
+        auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
+        if (!reader->parse(char_file_begin, char_file_end, &root, &err)) {
+            RING_ERR() << "Failed to parse " << err;
             return ERANGE;
         }
 
diff --git a/src/im/message_engine.cpp b/src/im/message_engine.cpp
index 143a9a0cdb..b72efcac6f 100644
--- a/src/im/message_engine.cpp
+++ b/src/im/message_engine.cpp
@@ -178,9 +178,7 @@ MessageEngine::load()
         file.open(savePath_);
 
         Json::Value root;
-        Json::Reader reader;
-        if (!reader.parse(file, root))
-            throw std::runtime_error("can't parse JSON.");
+        file >> root;
 
         long unsigned loaded {0};
         for (auto i = root.begin(); i != root.end(); ++i) {
@@ -232,11 +230,13 @@ MessageEngine::save() const
             root[msgsId.str()] = std::move(msg);
         }
         lock.unlock();
-        Json::FastWriter fastWriter;
         std::ofstream file;
         file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
         file.open(savePath_, std::ios::trunc);
-        file << fastWriter.write(root);
+        Json::StreamWriterBuilder wbuilder;
+        wbuilder["commentStyle"] = "None";
+        wbuilder["indentation"] = "";
+        file << Json::writeString(wbuilder, root);
         RING_DBG("[Account %s] saved %lu messages to %s", account_.getAccountID().c_str(), messages_.size(), savePath_.c_str());
     } catch (const std::exception& e) {
         RING_ERR("[Account %s] couldn't save messages to %s: %s", account_.getAccountID().c_str(), savePath_.c_str(), e.what());
diff --git a/src/ringdht/accountarchive.cpp b/src/ringdht/accountarchive.cpp
index e6a11cc151..ce454d4a19 100644
--- a/src/ringdht/accountarchive.cpp
+++ b/src/ringdht/accountarchive.cpp
@@ -31,11 +31,13 @@ AccountArchive::deserialize(const std::vector<uint8_t>& dat)
     RING_DBG("Loading account archive (%lu bytes)", dat.size());
 
     // Decode string
-    std::string decoded {dat.begin(), dat.end()};
+    auto* char_data = reinterpret_cast<const char*>(&dat[0]);
+    std::string err;
     Json::Value value;
-    Json::Reader reader;
-    if (!reader.parse(decoded.c_str(),value)) {
-        RING_ERR("Archive JSON parsing error: %s", reader.getFormattedErrorMessages().c_str());
+    Json::CharReaderBuilder rbuilder;
+    auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
+    if (!reader->parse(char_data, char_data + dat.size(), &value, &err)) {
+        RING_ERR() << "Archive JSON parsing error: " << err;
         throw std::runtime_error("failed to parse JSON");
     }
 
@@ -100,8 +102,10 @@ AccountArchive::serialize() const
             jsonContacts[c.first.toString()] = c.second.toJson();
     }
 
-    Json::FastWriter fastWriter;
-    return fastWriter.write(root);
+    Json::StreamWriterBuilder wbuilder;
+    wbuilder["commentStyle"] = "None";
+    wbuilder["indentation"] = "";
+    return Json::writeString(wbuilder, root);
 }
 
 
diff --git a/src/ringdht/namedirectory.cpp b/src/ringdht/namedirectory.cpp
index ddf15b6cf6..4d7e2c1479 100644
--- a/src/ringdht/namedirectory.cpp
+++ b/src/ringdht/namedirectory.cpp
@@ -127,8 +127,9 @@ void NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb)
                 reply->get_body(body);
 
                 Json::Value json;
-                Json::Reader reader;
-                if (!reader.parse(body, json)) {
+                Json::CharReaderBuilder rbuilder;
+                auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
+                if (!reader->parse(&body[0], &body[body.size()], &json, nullptr)) {
                     RING_ERR("Address lookup for %s: can't parse server response: %s", addr.c_str(), body.c_str());
                     cb("", Response::error);
                     return;
@@ -199,8 +200,9 @@ void NameDirectory::lookupName(const std::string& n, LookupCallback cb)
                 reply->get_body(body);
 
                 Json::Value json;
-                Json::Reader reader;
-                if (!reader.parse(body, json)) {
+                Json::CharReaderBuilder rbuilder;
+                auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
+                if (!reader->parse(&body[0], &body[body.size()], &json, nullptr)) {
                     RING_ERR("Name lookup for %s: can't parse server response: %s", name.c_str(), body.c_str());
                     cb("", Response::error);
                     return;
@@ -291,8 +293,9 @@ void NameDirectory::registerName(const std::string& addr, const std::string& n,
                 reply->get_body(body);
 
                 Json::Value json;
-                Json::Reader reader;
-                if (!reader.parse(body, json)) {
+                Json::CharReaderBuilder rbuilder;
+                auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
+                if (!reader->parse(&body[0], &body[body.size()], &json, nullptr)) {
                     cb(RegistrationResponse::error);
                     return;
                 }
diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp
index 1759d303e1..1f328cc331 100644
--- a/src/ringdht/ringaccount.cpp
+++ b/src/ringdht/ringaccount.cpp
@@ -806,9 +806,12 @@ RingAccount::useIdentity(const dht::crypto::Identity& identity)
     }
 
     Json::Value root;
-    Json::Reader reader;
-    if (!reader.parse(receipt_, root))
+    Json::CharReaderBuilder rbuilder;
+    auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
+    if (!reader->parse(&receipt_[0], &receipt_[receipt_.size()], &root, nullptr)) {
+        RING_ERR() << this << " device receipt parsing error";
         return false;
+    }
 
     auto dev_id = root["dev"].asString();
     if (dev_id != identity.second->getId().toString()) {
-- 
GitLab