mirror of
https://github.com/Kitware/CMake.git
synced 2026-08-09 09:12:00 +00:00
Merge topic 'jsonstate-fileapi'
d3ccd49eeb cmFileAPI: Use cmJSONState to read client queries
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !12317
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
#include "cmFileAPIConfigureLog.h"
|
||||
#include "cmFileAPIToolchains.h"
|
||||
#include "cmGlobalGenerator.h"
|
||||
#include "cmJSONState.h"
|
||||
#include "cmStringAlgorithms.h"
|
||||
#include "cmSystemTools.h"
|
||||
#include "cmTimestamp.h"
|
||||
@@ -54,14 +55,6 @@ cmFileAPI::cmFileAPI(cmake* cm)
|
||||
this->UserAPIv1 = cmStrCat(std::move(*cmakeConfigDir), "/api/v1"_s);
|
||||
}
|
||||
|
||||
Json::CharReaderBuilder rbuilder;
|
||||
rbuilder["collectComments"] = false;
|
||||
rbuilder["failIfExtra"] = true;
|
||||
rbuilder["rejectDupKeys"] = false;
|
||||
rbuilder["strictRoot"] = true;
|
||||
this->JsonReader =
|
||||
std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
|
||||
|
||||
Json::StreamWriterBuilder wbuilder;
|
||||
wbuilder["indentation"] = "\t";
|
||||
this->JsonWriter =
|
||||
@@ -174,37 +167,19 @@ void cmFileAPI::RemoveOldReplyFiles()
|
||||
bool cmFileAPI::ReadJsonFile(std::string const& file, Json::Value& value,
|
||||
std::string& error)
|
||||
{
|
||||
std::vector<char> content;
|
||||
|
||||
cmsys::ifstream fin;
|
||||
if (!cmSystemTools::FileIsDirectory(file)) {
|
||||
fin.open(file.c_str(), std::ios::binary);
|
||||
}
|
||||
auto finEnd = fin.rdbuf()->pubseekoff(0, std::ios::end);
|
||||
if (finEnd > 0) {
|
||||
size_t finSize = finEnd;
|
||||
try {
|
||||
// Allocate a buffer to read the whole file.
|
||||
content.resize(finSize);
|
||||
|
||||
// Now read the file from the beginning.
|
||||
fin.seekg(0, std::ios::beg);
|
||||
fin.read(content.data(), finSize);
|
||||
} catch (...) {
|
||||
fin.setstate(std::ios::failbit);
|
||||
}
|
||||
}
|
||||
fin.close();
|
||||
if (!fin) {
|
||||
// Verify the file exists.
|
||||
if (!cmSystemTools::FileExists(file) ||
|
||||
cmSystemTools::FileIsDirectory(file)) {
|
||||
value = Json::Value();
|
||||
error = "failed to read from file";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse our buffer as json.
|
||||
if (!this->JsonReader->parse(content.data(), content.data() + content.size(),
|
||||
&value, &error)) {
|
||||
cmJSONState parseState(file, &value);
|
||||
if (!parseState.errors.empty()) {
|
||||
value = Json::Value();
|
||||
error = parseState.GetErrorMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#include <cm3p/json/reader.h>
|
||||
#include <cm3p/json/value.h>
|
||||
#include <cm3p/json/writer.h>
|
||||
|
||||
@@ -169,7 +168,6 @@ private:
|
||||
/** Identify the situation in which WriteReplies was called. */
|
||||
IndexFor ReplyIndexFor = IndexFor::Success;
|
||||
|
||||
std::unique_ptr<Json::CharReader> JsonReader;
|
||||
std::unique_ptr<Json::StreamWriter> JsonWriter;
|
||||
|
||||
bool ReadJsonFile(std::string const& file, Json::Value& value,
|
||||
|
||||
@@ -105,7 +105,7 @@ def check_query_json_bad_root(q):
|
||||
check_error_re(q, "A valid JSON document must be either an array or an object value")
|
||||
|
||||
def check_query_json_empty(q):
|
||||
check_error_re(q, "value, object or array expected")
|
||||
check_error_re(q, "A JSON document cannot be empty")
|
||||
|
||||
def check_query_json_extra(q):
|
||||
if bool(os.environ.get("CMake_JSONCPP_PRE_1_7_5", "")) and is_dict(q) and sorted(q.keys()) == ["responses"]:
|
||||
|
||||
Reference in New Issue
Block a user