merge branch JSON-102 to default

JSON-97
Claudio Luck 5 years ago
commit 06e6ac37f4

@ -1,43 +1,23 @@
#include "pep-types.hh"
#include "pep-utils.hh"
#include "pep-adapter.hh"
#include "json_spirit/json_spirit_utils.h"
#include "pep-utils-json.hh"
#include <pEp/pEp_string.h>
#include <iostream> // Just to print debug stuff to std::cerr
#include "base64.hh"
using pEp::utility::from_json_object;
using pEp::utility::to_json_object;
namespace
{
// fetch a member from the given object, if set. (return a sensible NULL/default value if not)
template<class T, js::Value_type VALUE_TYPE>
T from_json_object(const js::Object& obj, const std::string& key)
{
const auto v = find_value(obj, key);
if(v.type() == js::null_type) return T{};
if(v.type() == VALUE_TYPE) return from_json<T>(v);
throw std::runtime_error("JSON object has a member for key \"" + key + "\""
" with incompatible type " + js::value_type_to_string( v.type())
+ " instead of expected type " + js::value_type_to_string(VALUE_TYPE)
);
}
std::string base64_from_json_object(const js::Object& obj, const std::string& key)
{
const std::string b64String = from_json_object<std::string, js::str_type> (obj, key);
return base64_decode(b64String);
}
template<class T>
void to_json_object(js::Object& obj, const std::string& key, const T& value)
{
if(value!=T{})
{
obj.emplace_back( key, js::Value( to_json<T>(value) ));
}
}
void to_base64_json_object(js::Object& obj, const std::string& key, char *value, size_t size)
{
if(value != nullptr && size>0)

@ -0,0 +1,39 @@
// some helper functions to deal with JSON data
#ifndef PEP_UTILS_JSON_HH
#define PEP_UTILS_JSON_HH
#include "json_spirit/json_spirit_utils.h"
namespace pEp {
namespace utility {
// fetch a member from the given object, if set. (return a sensible NULL/default value if not)
template<class T, js::Value_type VALUE_TYPE>
T from_json_object(const js::Object& obj, const std::string& key)
{
const auto v = find_value(obj, key);
if(v.type() == js::null_type) return T{};
if(v.type() == VALUE_TYPE) return from_json<T>(v);
throw std::runtime_error("JSON object has a member for key \"" + key + "\""
" with incompatible type " + js::value_type_to_string( v.type())
+ " instead of expected type " + js::value_type_to_string(VALUE_TYPE)
);
}
template<class T>
void to_json_object(js::Object& obj, const std::string& key, const T& value)
{
if(value!=T{})
{
obj.emplace_back( key, js::Value( to_json<T>(value) ));
}
}
} // end of namespace pEp::utility
} // end of namespace pEp
#endif // PEP_UTILS_JSON_HH

@ -1,14 +1,21 @@
#include "server_version.hh"
#include "inout.hh"
#include <fstream>
#include <sstream>
#include "pep-utils.hh"
#include "pep-utils-json.hh"
#include <pEp/pEpEngine.h> // for PEP_VERSION and get_engine_version()
#include <boost/algorithm/string/trim.hpp>
#include "json_spirit/json_spirit_reader.h"
namespace js = json_spirit;
namespace {
#ifdef PACKAGE_VERSION
const char* const PackageVersion = PACKAGE_VERSION;
char* PackageVersion = PACKAGE_VERSION;
#else
const char* const PackageVersion = nullptr;
char* PackageVersion = nullptr;
#endif
@ -78,7 +85,27 @@ ServerVersion::ServerVersion(unsigned maj, unsigned min, unsigned p)
, patch{p}
, name {VersionName}
, package_version{PackageVersion}
{}
{
if (!PackageVersion)
try{
const std::string file_content =
boost::algorithm::trim_copy(
pEp::utility::slurp("PackageVersion")
);
js::Value v;
js::read_or_throw(file_content, v);
const js::Object obj = v.get_obj();
PackageVersion = pEp::utility::from_json_object<char*, js::str_type>(obj, "package_version");
PackageVersion = strdup(file_content.c_str());
this->package_version = PackageVersion;
}
catch(std::runtime_error&)
{
// slurp() throws when it cannot read the file.
}
}
const ServerVersion& server_version()
{

@ -17,7 +17,7 @@ struct ServerVersion
// The version name is of the form "(##) name" where ## is a monotonic increasing number.
std::string name;
const char* const package_version; // must be set via -D, e.g. -D'PACKAGE_VERSION="deb9-0.1"'
const char* package_version; // must be set via -D, e.g. -D'PACKAGE_VERSION="deb9-0.1"'
// returns "major.minor.patch"
std::string major_minor_patch() const;

Loading…
Cancel
Save