Skip to content

Commit

Permalink
More JSONCPP -> nlohmann
Browse files Browse the repository at this point in the history
  • Loading branch information
abellgithub committed Apr 18, 2019
1 parent 6555b9a commit 361227c
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 20,650 deletions.
6 changes: 3 additions & 3 deletions plugins/greyhound/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ PDAL_ADD_PLUGIN(reader_libname reader greyhound
io/GreyhoundCommon.cpp
io/bounds.cpp
LINK_WITH
${PDAL_JSONCPP_LIB_NAME}
${WINSOCK_LIBRARY}
INCLUDES
${PDAL_JSONCPP_INCLUDE_DIR}
${NLOHMANN_INCLUDE_DIR}
${PDAL_VENDOR_DIR}
)

Expand All @@ -32,10 +32,10 @@ PDAL_ADD_PLUGIN(writer_libname writer greyhound
io/GreyhoundCommon.cpp
io/bounds.cpp
LINK_WITH
${PDAL_JSONCPP_LIB_NAME}
${WINSOCK_LIBRARY}
INCLUDES
${PDAL_JSONCPP_INCLUDE_DIR}
${NLOHMANN_INCLUDE_DIR}
${PDAL_VENDOR_DIR}
)

Expand All @@ -47,9 +47,9 @@ if (WITH_TESTS)
LINK_WITH
${reader_libname}
${writer_libname}
${PDAL_JSONCPP_LIB_NAME}
INCLUDES
${PDAL_JSONCPP_INCLUDE_DIR}
${NLOHMANN_INCLUDE_DIR}
${PDAL_VENDOR_DIR}
)
endif()
47 changes: 24 additions & 23 deletions plugins/greyhound/io/GreyhoundCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ std::string GreyhoundParams::extractUrl(const GreyhoundArgs& args) const
return s;
}

Json::Value GreyhoundParams::extractParams(const GreyhoundArgs& args)
NL::json GreyhoundParams::extractParams(const GreyhoundArgs& args)
{
Json::Value json;
NL::json j;

const std::size_t m(args.url.find('?'));
std::string q(
Expand Down Expand Up @@ -132,9 +132,9 @@ Json::Value GreyhoundParams::extractParams(const GreyhoundArgs& args)
// stringify them. For other parameters, keep them as strings to
// pass them along untouched.
if (greyhoundParams.count(k))
json[k] = parse(v);
j[k] = parse(v);
else
json[k] = v;
j[k] = v;

a = b + 1;
b = q.find_first_of('&', a);
Expand Down Expand Up @@ -170,43 +170,43 @@ Json::Value GreyhoundParams::extractParams(const GreyhoundArgs& args)
gbounds = greyhound::Bounds(parse(args.sbounds));
}

json["bounds"] = gbounds.toJson();
j["bounds"] = gbounds.toJson();
}

if (args.buffer)
{
if (json["bounds"].isNull())
if (j["bounds"].is_null())
throw pdal_error("Cannot specify `buffer` without `bounds`");
m_obounds = json["bounds"];
m_obounds = j["bounds"];

json["bounds"] = greyhound::Bounds(json["bounds"])
.growBy(args.buffer).toJson();
j["bounds"] = greyhound::Bounds(j["bounds"]).
growBy(args.buffer).toJson();
}

if (args.depthBegin)
json["depthBegin"] = static_cast<Json::UInt64>(args.depthBegin);
j["depthBegin"] = args.depthBegin;
if (args.depthEnd)
json["depthEnd"] = static_cast<Json::UInt64>(args.depthEnd);
j["depthEnd"] = args.depthEnd;

Json::Value f(args.filter);
NL::json f(args.filter);

if (f.isString())
f = parse(f.asString());
if (f.is_string())
f = parse(f.get<std::string>());

if (args.tilePath.size())
f["Path"] = args.tilePath;

if (!f.isNull())
json["filter"] = f;
if (!f.is_null())
j["filter"] = f;

if (!args.schema.isNull())
json["schema"] = args.schema;
if (!args.schema.is_null())
j["schema"] = args.schema;

#ifdef PDAL_HAVE_LAZPERF
json["compress"] = true;
j["compress"] = true;
#endif

return json;
return j;
}

std::string GreyhoundParams::qs() const
Expand All @@ -218,12 +218,13 @@ std::string GreyhoundParams::qs() const
s += (s.size() ? '&' : '?') + k + '=' + v;
});

for (const std::string key : m_params.getMemberNames())
for (auto it : m_params.items())
{
const std::string& key = it.key();
if (greyhoundParams.count(key))
add(key, dense(m_params[key]));
add(key, it.value().dump());
else
add(key, m_params[key].asString());
add(key, it.value().get<std::string>());
}

return s;
Expand Down
94 changes: 52 additions & 42 deletions plugins/greyhound/io/GreyhoundCommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <cstddef>
#include <string>

#include <json/json.h>
#include <nlohmann/json.hpp>

#include <pdal/pdal_types.hpp>
#include <pdal/PointLayout.hpp>
Expand All @@ -49,48 +49,47 @@ namespace pdal

namespace greyhound = entwine;

static inline Json::Value parse(const std::string& data)
static inline NL::json parse(const std::string& data)
{
Json::Value json;
Json::Reader reader;
NL::json j;

if (data.size())
try
{
if (!reader.parse(data, json, false))
{
const std::string jsonError(reader.getFormattedErrorMessages());
if (!jsonError.empty())
{
throw pdal_error("Error during parsing: " + jsonError);
}
}
j= NL::json::parse(data);
}

return json;
catch (const NL::json::parse_error& err)
{
throw pdal_error(std::string("Error during parsing: ") + err.what());
}
return j;
}

//ABELL
/**
static inline std::string dense(const Json::Value& json)
{
Json::StreamWriterBuilder builder;
builder.settings_["indentation"] = "";
return Json::writeString(builder, json);
}
**/

static inline Json::Value layoutToSchema(PointLayout& layout)
static inline NL::json layoutToSchema(PointLayout& layout)
{
Json::Value schema;
NL::json schema;

layout.finalize();
for (const Dimension::Id id : layout.dims())
{
const Dimension::Detail& d(*layout.dimDetail(id));
const std::string name(layout.dimName(id));

Json::Value j;
j["name"] = name;
j["type"] = Dimension::toName(base(d.type()));
j["size"] = static_cast<int>(Dimension::size(d.type()));
schema.append(j);
NL::json j {
{ "name", name },
{ "type", Dimension::toName(base(d.type())) },
{ "size", Dimension::size(d.type()) }
};
schema.push_back(j);
}

return schema;
Expand All @@ -104,9 +103,9 @@ struct GreyhoundArgs
std::size_t depthBegin = 0;
std::size_t depthEnd = 0;
std::string tilePath;
Json::Value filter;
Json::Value dims;
Json::Value schema;
NL::json filter;
NL::json dims;
NL::json schema;
double buffer = 0;
};

Expand All @@ -115,40 +114,51 @@ class GreyhoundParams
public:
GreyhoundParams() { }
GreyhoundParams(const GreyhoundArgs& args);
GreyhoundParams(std::string resourceRoot, Json::Value params)
GreyhoundParams(std::string resourceRoot, NL::json params)
: m_url(resourceRoot)
, m_params(params)
{
if (m_params.isMember("obounds"))
auto it = m_params.find("obounds");
if (it != m_params.end())
{
m_obounds = m_params["obounds"];
m_params.removeMember("obounds");
m_obounds = *it;
m_params.erase(it);
}
}

std::string root() const { return m_url; }
std::string root() const
{ return m_url; }
std::string qs() const;

Json::Value& operator[](std::string key) { return m_params[key]; }
Json::Value toJson() const
NL::json& operator[](std::string key)
{ return m_params[key]; }
NL::json toJson() const
{
Json::Value json(m_params);
if (!m_obounds.isNull())
json["obounds"] = m_obounds;
return json;
NL::json j(m_params);
if (!m_obounds.is_null())
j["obounds"] = m_obounds;
return j;
}

Json::Value obounds() const { return m_obounds; }
void removeMember(std::string key) { m_params.removeMember(key); }
NL::json obounds() const
{ return m_obounds; }
void removeMember(std::string key)
{
try
{
m_params.erase(key);
}
catch (...)
{}
}

private:
std::string extractUrl(const GreyhoundArgs& args) const;
Json::Value extractParams(const GreyhoundArgs& args);

Json::Value m_obounds;
NL::json extractParams(const GreyhoundArgs& args);

NL::json m_obounds;
std::string m_url;
Json::Value m_params;
NL::json m_params;
};

} // namespace pdal
Expand Down
42 changes: 21 additions & 21 deletions plugins/greyhound/io/GreyhoundReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,38 +98,38 @@ void GreyhoundReader::initialize(PointTableRef table)
throw pdal_error(std::string("Failed to fetch info: ") + e.what());
}

if (m_info.isMember("srs") && getSpatialReference().empty())
setSpatialReference(m_info["srs"].asString());
auto it = m_info.find("srs");
if (it != m_info.end() && getSpatialReference().empty())
setSpatialReference(it->get<std::string>());
}

void GreyhoundReader::addDimensions(PointLayoutPtr layout)
{
std::map<std::string, const Json::Value*> remote;
for (const auto& d : m_info["schema"])
remote[d["name"].asString()] = &d;
std::map<std::string, const NL::json *> remote;

for (auto it : m_info["schema"].items())
remote[it.key()] = &it.value();

// The dimensions we will query are determined in the following order, the
// first of which is present taking precedence:
// - The `dims` PDAL option on this stage.
// - A `schema` query parameter parsed from the `url` option.
// - Finally, fall back to the full `info.schema`.
if (m_args.dims.isNull())
if (m_args.dims.is_null())
{
// If no dimensions are explicitly listed, only include the indexed
// schema - omitting any appended dimensions. Those must be explicitly
// specified if desired.
Json::Value nativeSchema;
for (const Json::Value d : m_info["schema"])
if (!d["addon"].asBool())
nativeSchema.append(d);
NL::json nativeSchema;
for (auto el : m_info["schema"])
if (!el["addon"].get<bool>())
nativeSchema.push_back(el);

const Json::Value& readSchema = m_params["schema"].isNull() ?
const NL::json& readSchema = m_params["schema"].is_null() ?
nativeSchema : m_params["schema"];

for (const auto& d : readSchema)
{
m_args.dims.append(d["name"].asString());
}
m_args.dims.push_back(d["name"].get<std::string>());
}

auto isXyz([](const std::string& name)
Expand All @@ -140,17 +140,17 @@ void GreyhoundReader::addDimensions(PointLayoutPtr layout)
// Build the request layout from the specified `dims`.
for (const auto& n : m_args.dims)
{
const auto name(n.asString());
const std::string name(n.get<std::string>());

if (!remote.count(name))
throw pdal_error(name + " does not exist in the remote schema");

const auto& d(*remote.at(name));

const int baseType(
Utils::toNative(Dimension::fromName(d["type"].asString())));
Utils::toNative(Dimension::fromName(d["type"].get<std::string>())));

const int size(d["size"].asInt());
const int size(d["size"].get<int>());

const Dimension::Type type(
isXyz(name) ?
Expand All @@ -161,7 +161,7 @@ void GreyhoundReader::addDimensions(PointLayoutPtr layout)
m_readLayout.registerOrAssignDim(name, type);
}

if (!m_params.obounds().isNull())
if (!m_params.obounds().is_null())
{
layout->registerDim(Dimension::Id::Omit);
}
Expand All @@ -180,9 +180,9 @@ void GreyhoundReader::addDimensions(PointLayoutPtr layout)
void GreyhoundReader::prepared(PointTableRef table)
{
MetadataNode queryNode(table.privateMetadata("greyhound"));
queryNode.add("info", dense(m_info));
queryNode.add("info", m_info.dump());
queryNode.add("root", m_params.root());
queryNode.add("params", dense(m_params.toJson()));
queryNode.add("params", m_params.toJson().dump());
}

point_count_t GreyhoundReader::read(PointViewPtr view, point_count_t count)
Expand Down Expand Up @@ -225,7 +225,7 @@ point_count_t GreyhoundReader::read(PointViewPtr view, point_count_t count)
m_cb(*view, view->size() - 1);
}
#endif
if (!m_params.obounds().isNull())
if (!m_params.obounds().is_null())
{
greyhound::Bounds obounds(m_params.obounds());
greyhound::Point p;
Expand Down

0 comments on commit 361227c

Please sign in to comment.