-
Notifications
You must be signed in to change notification settings - Fork 192
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 a swig out typemap for Json::Value + openstudio::path typemap in/out for Python + add toJSON() for Workfow files and BCLMeasure #4917
Conversation
%fragment("JsonToDict","header", fragment="SWIG_FromCharPtrAndSize") { | ||
PyObject* SWIG_From_JsonValue(const Json::Value& value) { | ||
|
||
if (value.isBool()) { | ||
return value.asBool() ? Py_True : Py_False; | ||
} else if (value.isIntegral()) { | ||
return PyLong_FromLongLong(value.asInt64()); | ||
} else if (value.isNumeric()) { | ||
return PyFloat_FromDouble(value.asDouble()); | ||
} else if (value.isString()) { | ||
// return PyUnicode_FromString(value.asCString()); | ||
const auto str = value.asString(); | ||
return SWIG_FromCharPtrAndSize(str.data(), str.size()); | ||
} else if (value.isArray()) { | ||
PyObject* result = PyList_New(value.size()); | ||
Py_ssize_t idx = 0; | ||
for( const auto& arrayElement : value) { | ||
auto val = SWIG_From_JsonValue(arrayElement); | ||
PyList_SetItem(result, idx++, val); // This steals the reference so no decref | ||
} | ||
return result; | ||
|
||
} else if (value.isObject()) { | ||
PyObject* result = PyDict_New(); | ||
for( const auto& id : value.getMemberNames()) { | ||
auto val = SWIG_From_JsonValue(value[id]); | ||
PyDict_SetItemString(result, id.c_str(), val); // Does **not** steal so decref | ||
Py_DECREF(val); | ||
} | ||
return result; | ||
} | ||
|
||
return PyDict_New(); | ||
} | ||
} | ||
%typemap(out, fragment="JsonToDict") Json::Value { | ||
$result = SWIG_From_JsonValue($1); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fragment and typemap for python
%fragment("JsonToDict", "header", fragment="SWIG_FromCharPtrAndSize") { | ||
VALUE SWIG_From_JsonValue(const Json::Value& value) { | ||
if (value.isBool()) { | ||
return value.asBool() ? Qtrue : Qfalse; | ||
} else if (value.isIntegral()) { | ||
return INT2NUM(value.asInt64()); | ||
} else if (value.isNumeric()) { | ||
return DOUBLE2NUM(value.asDouble()); | ||
} else if (value.isString()) { | ||
const auto str = value.asString(); | ||
return SWIG_FromCharPtrAndSize(str.data(), str.size()); | ||
} else if (value.isArray()) { | ||
VALUE result = rb_ary_new2(value.size()); | ||
for( const auto& arrayElement : value) { | ||
rb_ary_push(result, SWIG_From_JsonValue(arrayElement)); | ||
} | ||
return result; | ||
|
||
} else if (value.isObject()) { | ||
VALUE result = rb_hash_new(); | ||
for( const auto& id : value.getMemberNames()) { | ||
rb_hash_aset(result, ID2SYM(rb_intern(id.data())), SWIG_From_JsonValue(value[id])); | ||
} | ||
return result; | ||
} | ||
|
||
return rb_hash_new(); | ||
} | ||
} | ||
%typemap(out, fragment="JsonToDict") Json::Value { | ||
$result = SWIG_From_JsonValue($1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fragment and typemap for ruby
} else if (value.isObject()) { | ||
VALUE result = rb_hash_new(); | ||
for( const auto& id : value.getMemberNames()) { | ||
rb_hash_aset(result, ID2SYM(rb_intern(id.data())), SWIG_From_JsonValue(value[id])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the symbolization of hash keys, could replace with
rb_hash_aset(result, ID2SYM(rb_intern(id.data())), SWIG_From_JsonValue(value[id])); | |
rb_hash_aset(result, SWIG_FromCharPtrAndSize(id.data(), id.size()), SWIG_From_JsonValue(value[id])); |
|
||
#if defined SWIGPYTHON | ||
%fragment("JsonToDict","header", fragment="SWIG_FromCharPtrAndSize") { | ||
inline PyObject* SWIG_From_JsonValue(const Json::Value& value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up moving the fragments defined in xxx/LanguageSpecific.i to a single jsoncpp.i file... Because I was getting a "multiple defined symbol SWIG_From_JsonValue" when linking the ruby libs into a single lib. I'm not 100% sure but really I think the only think that fixed it is inline
here. I'm fine leaving it in jsoncpp.i though
#ifdef SWIGCSHARP | ||
%ignore openstudio::epJSON::toJSON; | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want that global?
fd2f7b3
to
6051ea8
Compare
1f7b61d
to
b11a6b7
Compare
…ializing/deserializing WorkflowStepResults we construct a Json::Value, serialize that. We parse that string in WorkflowStep to a Json::Value, then we serialize to a string. In WOrkflowJSON we parse that string... etc
…to openstudio::path
…h to pathlib.Path in Python
b11a6b7
to
e7b984a
Compare
CI Results for e7b984a:
|
e7b984a
to
6a31f1a
Compare
Pull request overview
Context: As part of the rewrite of Measure Manager to C++, I'm going to be using Json::Value outputs a lot more than currently. I'm just trying to isolate changes for easier review.
Test code:
ruby:
Products/openstudio labs -e "m = OpenStudio::Model::Model.new; ft = OpenStudio::EnergyPlus::ForwardTranslator.new; ft.setExcludeLCCObjects(true); w = ft.translateModel(m); d = OpenStudio::EPJSON::toJSON(w); puts d.class; require 'json'; puts JSON.pretty_generate(d)"
python:
Products/openstudio labs -c "m = openstudio.model.Model(); z = openstudio.model.ThermalZone(m); ft = openstudio.energyplus.ForwardTranslator(); ft.setExcludeLCCObjects(True); w = ft.translateModel(m); d = openstudio.epjson.toJSON(w); print(type(d)); import json; print(json.dumps(d, indent=2))"
Pull Request Author
src/model/test
)src/energyplus/Test
)src/osversion/VersionTranslator.cpp
)Labels:
IDDChange
APIChange
Pull Request - Ready for CI
so that CI builds your PRReview Checklist
This will not be exhaustively relevant to every PR.