Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macro to check for required json keys #112

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions include/mujincontrollerclient/mujinjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
#include <rapidjson/error/en.h>
#include <rapidjson/prettywriter.h>

#ifndef MUJINCLIENTJSON_LOAD_REQUIRED_JSON_VALUE_BY_KEY
#define MUJINCLIENTJSON_LOAD_REQUIRED_JSON_VALUE_BY_KEY(rValue, key, param) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this is a macro instead of a function LoadRequiredJsonValueByKey?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya, actually I also think function is better, then we can put it in a namespace...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we talk in controllercommon!489 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, Ziyan brought up the same concern here. Let's keep discussion here for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes you, me and @ziyan arguing for a function over a macro then

{ \
if (!(mujinclient::mujinjson_external::LoadJsonValueByKey(rValue, key, param))) \
{ \
throw mujinclient::mujinjson_external::MujinJSONException(boost::str(boost::format(("[%s, %u] assert(mujinclient::mujinjson_external::LoadJsonValueByKey(%s, %s, %s))"))%__FILE__%__LINE__%#rValue%key%#param), mujinclient::mujinjson_external::MJE_Failed); \
} \
}
#endif // MUJINCLIENTJSON_LOAD_REQUIRED_JSON_VALUE_BY_KEY
namespace mujinclient {
namespace mujinjson_external {

Expand Down Expand Up @@ -567,13 +576,16 @@ template<class T> inline void SaveJsonValue(rapidjson::Document& v, const T& t)
template<class T, class U> inline void SetJsonValueByKey(rapidjson::Value& v, const U& key, const T& t, rapidjson::Document::AllocatorType& alloc);

//get one json value by key, and store it in local data structures
template<class T> void inline LoadJsonValueByKey(const rapidjson::Value& v, const char* key, T& t) {
//returns false if the key does not exist or is null, else returns true
template<class T> bool inline LoadJsonValueByKey(const rapidjson::Value& v, const char* key, T& t) {
if (!v.IsObject()) {
throw MujinJSONException("Cannot load value of non-object.", MJE_Failed);
}
if (v.HasMember(key)) {
if (v.HasMember(key) && !v.FindMember(key)->value.IsNull()) {
cielavenir marked this conversation as resolved.
Show resolved Hide resolved
LoadJsonValue(v[key], t);
return true;
}
return false;
}
template<class T, class U> inline void LoadJsonValueByKey(const rapidjson::Value& v, const char* key, T& t, const U& d) {
if (!v.IsObject()) {
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ build_test(settaskparameters)
build_test(showresults)
build_test(uploadregister)
build_test(uploadregistercec)
build_test(testjsonutils)
64 changes: 64 additions & 0 deletions test/testjsonutils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <string>
#include <sstream>
#include <mujincontrollerclient/mujinjson.h>

// The tests will expect json documents containing these keys
using namespace mujinclient::mujinjson_external;
const std::string expectedJsonKey = "key1";

void TestExistingKey()
{
rapidjson::Document document;
document.SetObject();
int expectedResult = 1;
// Expected key and an extra key are in the document
SetJsonValueByKey(document, expectedJsonKey.c_str(), expectedResult, document.GetAllocator());
SetJsonValueByKey(document, "extraKey", expectedResult, document.GetAllocator());
int result = -1;
MUJINCLIENTJSON_LOAD_REQUIRED_JSON_VALUE_BY_KEY(document, expectedJsonKey.c_str(), result);
if(result == expectedResult) {
std::cout << "OK: For existing key test, we got result = " << result;
}
else {
std::cout << "FAILED: For existing key test, we got result = " << result;
}
std::cout << " when expected result was " << expectedResult << "\n";
}

void TestNonExistentKey()
{
rapidjson::Document document;
document.SetObject();
// Expected key is not in the document
SetJsonValueByKey(document, "extraKey", 0, document.GetAllocator());
int result = -1;
int exceptionLine = -1;
try
{
exceptionLine = __LINE__ + 1;
MUJINCLIENTJSON_LOAD_REQUIRED_JSON_VALUE_BY_KEY(document, expectedJsonKey.c_str(), result);
}
catch(const MujinJSONException& e)
{
std::stringstream ss;
ss << "mujin (): [" << __FILE__<<", "<< exceptionLine <<"] assert(mujinclient::mujinjson_external::LoadJsonValueByKey(document, key1, result))";
if(ss.str() == std::string(e.what())) {
std::cout << "OK: ";
}
else{
std::cout << "FAILED: \n";
std::cout << "Expected = " << ss.str() << "\n";
}
std::cout << "Got exception message = " << e.what() << "\n";
}
catch(const std::exception& e)
{
std::cout << "FAILED: Caught an exception, but not the right type.\n" << "Exception message = " << e.what() << "\n";
}
}

int main()
{
TestExistingKey();
TestNonExistentKey();
}