Exemple #1
0
void
BlackboardGraph::to_json_value(rapidjson::Document& d, rapidjson::Value& v) const
{
	rapidjson::Document::AllocatorType& allocator = d.GetAllocator();
	v.SetObject();
	// Avoid unused variable warnings
	(void)allocator;

	if (kind_) {
		rapidjson::Value v_kind;
		v_kind.SetString(*kind_, allocator);
		v.AddMember("kind", v_kind, allocator);
	}
	if (apiVersion_) {
		rapidjson::Value v_apiVersion;
		v_apiVersion.SetString(*apiVersion_, allocator);
		v.AddMember("apiVersion", v_apiVersion, allocator);
	}
	if (dotgraph_) {
		rapidjson::Value v_dotgraph;
		v_dotgraph.SetString(*dotgraph_, allocator);
		v.AddMember("dotgraph", v_dotgraph, allocator);
	}

}
Exemple #2
0
    /*
    * generate from "sync_conf.json": "jos_lib::NetworkConf"
    */
    bool Serialize(const jos_lib::NetworkConf& obj_val, jos_lib::Allocator& alloc, rapidjson::Value& json_val)
    {
        do
        {
            json_val.SetObject();
            // fields from "jos_lib::NetworkConf"
            if (!Serialize(obj_val.port, "port", alloc, json_val)) break; // @(field_type : int)
            if (!Serialize(obj_val.backlog, "backlog", alloc, json_val)) break; // @(field_type : int)
            if (!Serialize(obj_val.enable_reuseport, "enable_reuseport", alloc, json_val)) break; // @(field_type : bool)
            if (!Serialize(obj_val.enable_nodelay, "enable_nodelay", alloc, json_val)) break; // @(field_type : bool)
            if (!Serialize(obj_val.enable_defer_accept, "enable_defer_accept", alloc, json_val)) break; // @(field_type : bool)
            if (!Serialize(obj_val.max_body_size, "max_body_size", alloc, json_val)) break; // @(field_type : int)
            if (!Serialize(obj_val.max_keepalive_requests, "max_keepalive_requests", alloc, json_val)) break; // @(field_type : int)
            if (!Serialize(obj_val.recv_timeout, "recv_timeout", alloc, json_val)) break; // @(field_type : int)
            if (!Serialize(obj_val.send_timeout, "send_timeout", alloc, json_val)) break; // @(field_type : int)
            if (!Serialize(obj_val.threads, "threads", alloc, json_val)) break; // @(field_type : int)
            if (!Serialize(obj_val.user, "user", alloc, json_val)) break; // @(field_type : std::string)
            if (!Serialize(obj_val.passwd, "passwd", alloc, json_val)) break; // @(field_type : std::string)
            if (!Serialize(obj_val.access_allow, "access_allow", alloc, json_val)) break; // @(field_type : std::string)

            return true;
        } while (0);

        return false;
    }
Exemple #3
0
void
SkillInfo::to_json_value(rapidjson::Document& d, rapidjson::Value& v) const
{
	rapidjson::Document::AllocatorType& allocator = d.GetAllocator();
	v.SetObject();
	// Avoid unused variable warnings
	(void)allocator;

	if (kind_) {
		rapidjson::Value v_kind;
		v_kind.SetString(*kind_, allocator);
		v.AddMember("kind", v_kind, allocator);
	}
	if (apiVersion_) {
		rapidjson::Value v_apiVersion;
		v_apiVersion.SetString(*apiVersion_, allocator);
		v.AddMember("apiVersion", v_apiVersion, allocator);
	}
	if (name_) {
		rapidjson::Value v_name;
		v_name.SetString(*name_, allocator);
		v.AddMember("name", v_name, allocator);
	}

}
void jsonifyModule(const Module& module, rapidjson::Value& moduleValue, rapidjson::Document& document){
    moduleValue.SetObject();
    moduleValue.AddMember("name", module.getName().c_str(), document.GetAllocator());
    moduleValue.AddMember("description", module.getDescription().c_str(), document.GetAllocator());
    moduleValue.AddMember("type", module.getType().getName().c_str(), document.GetAllocator());

    //add inputs
    rapidjson::Value inputsValue;
    inputsValue.SetObject();
    for(unsigned int i=0; i<module.getInputSize(); ++i){
        auto inputLink = module.getInput(i);
        auto outputLink = inputLink->getOutputLink();
        //only add connected links
        if(outputLink){
            std::string sourceString = outputLink->getOwner().getName() + "." + outputLink->getModuleOutput().getName();
            rapidjson::Value source(sourceString.c_str(), document.GetAllocator());
            rapidjson::Value inputName(inputLink->getModuleInput().getName().c_str(), document.GetAllocator());
            inputsValue.AddMember(inputName, source, document.GetAllocator());
        } else {
            //add custom unlinked values
            rapidjson::Value vectorValue;
            vectorValue.SetArray();
            auto unlinkedValue = inputLink->getUnlinkedValue();
            for(int j=0; j<unlinkedValue.getSignalType().dimensionality; ++j){
                vectorValue.PushBack(unlinkedValue[j], document.GetAllocator());
            }
            rapidjson::Value inputName(inputLink->getModuleInput().getName().c_str(), document.GetAllocator());
            inputsValue.AddMember(inputName, vectorValue, document.GetAllocator());
        }
    }
    moduleValue.AddMember("inputs", inputsValue, document.GetAllocator());
}
Exemple #5
0
void jsonValueToRapidJSON(JSONValue* value, rapidjson::Value& rapidValue, rapidjson::Document& document) {
	JSONNull* nullValue = dynamic_cast<JSONNull*>(value);
	if (nullValue) {
		rapidValue.SetNull();
		return;
	}
	JSONNumber* numberValue = dynamic_cast<JSONNumber*>(value);
	if (numberValue) {
		if (numberValue->canBeUInt64()) {
			rapidValue.SetUint64(numberValue->getUInt64Value());
		}
		else if (numberValue->canBeInt64()) {
			rapidValue.SetInt64(numberValue->getInt64Value());
		}
		else  {
			rapidValue.SetDouble(numberValue->getDoubleValue());
		}
		return;
	}
	JSONString* stringValue = dynamic_cast<JSONString*>(value);
	if (stringValue) {
		rapidValue.SetString(stringValue->getValue().c_str(), stringValue->getValue().size(), document.GetAllocator());
		return;
	}
	JSONBool* boolValue = dynamic_cast<JSONBool*>(value);
	if (boolValue) {
		rapidValue.SetBool(boolValue->getValue());
		return;
	}
	JSONArray* arrayValue = dynamic_cast<JSONArray*>(value);
	if (arrayValue) {
		rapidValue.SetArray();
		std::vector<JSONValue::ref> values = arrayValue->getValues();
		for (auto & value : values) {
			rapidjson::Value obj;
			jsonValueToRapidJSON(value.get(), obj, document);
			rapidValue.PushBack(obj, document.GetAllocator());
		}
		return;
	}
	JSONObject* objectValue = dynamic_cast<JSONObject*>(value);
	if (objectValue) {
		rapidValue.SetObject();
		typedef std::map<std::string, JSONValue::ref> ValuesMap;
		ValuesMap values = objectValue->getValues();
		for (auto & value : values) {
			rapidjson::Value obj;
			jsonValueToRapidJSON(value.second.get(), obj, document);
			rapidjson::Value key;
			key.SetString(value.first.c_str(), value.first.size(), document.GetAllocator());
			rapidValue.AddMember(key, obj, document.GetAllocator());
		}
		return;
	}
	assert(false);
}
Exemple #6
0
    /*
    * generate from "sync_conf.json": "jos_lib::SyncServerConf"
    */
    bool Serialize(const jos_lib::SyncServerConf& obj_val, jos_lib::Allocator& alloc, rapidjson::Value& json_val)
    {
        do
        {
            json_val.SetObject();
            // fields from "jos_lib::SyncServerConf"
            if (!Serialize(obj_val.network, "network", alloc, json_val)) break; // @(field_type : NetworkConf)
            if (!Serialize(obj_val.sync, "sync", alloc, json_val)) break; // @(field_type : SyncConf)

            return true;
        } while (0);

        return false;
    }
Exemple #7
0
void
Backend::to_json_value(rapidjson::Document& d, rapidjson::Value& v) const
{
	rapidjson::Document::AllocatorType& allocator = d.GetAllocator();
	v.SetObject();
	// Avoid unused variable warnings
	(void)allocator;

	if (kind_) {
		rapidjson::Value v_kind;
		v_kind.SetString(*kind_, allocator);
		v.AddMember("kind", v_kind, allocator);
	}
	if (apiVersion_) {
		rapidjson::Value v_apiVersion;
		v_apiVersion.SetString(*apiVersion_, allocator);
		v.AddMember("apiVersion", v_apiVersion, allocator);
	}
	if (id_) {
		rapidjson::Value v_id;
		v_id.SetString(*id_, allocator);
		v.AddMember("id", v_id, allocator);
	}
	if (name_) {
		rapidjson::Value v_name;
		v_name.SetString(*name_, allocator);
		v.AddMember("name", v_name, allocator);
	}
	if (url_) {
		rapidjson::Value v_url;
		v_url.SetString(*url_, allocator);
		v.AddMember("url", v_url, allocator);
	}
	rapidjson::Value v_services(rapidjson::kArrayType);
	v_services.Reserve(services_.size(), allocator);
	for (const auto & e : services_) {
		rapidjson::Value v(rapidjson::kObjectType);
		e->to_json_value(d, v);
		v_services.PushBack(v, allocator);
	}
	v.AddMember("services", v_services, allocator);

}
Exemple #8
0
    /*
    * generate from "sync_conf.json": "jos_lib::SyncConf"
    */
    bool Serialize(const jos_lib::SyncConf& obj_val, jos_lib::Allocator& alloc, rapidjson::Value& json_val)
    {
        do
        {
            json_val.SetObject();
            // fields from "jos_lib::SyncConf"
            if (!Serialize(obj_val.logs_path, "logs_path", alloc, json_val)) break; // @(field_type : std::string)
            if (!Serialize(obj_val.ngx_path, "ngx_path", alloc, json_val)) break; // @(field_type : std::string)
            if (!Serialize(obj_val.auth_path, "auth_path", alloc, json_val)) break; // @(field_type : std::string)
            if (!Serialize(obj_val.threads, "threads", alloc, json_val)) break; // @(field_type : int)
            if (!Serialize(obj_val.release_interval, "release_interval", alloc, json_val)) break; // @(field_type : int)
            if (!Serialize(obj_val.checkdb_interval, "checkdb_interval", alloc, json_val)) break; // @(field_type : int)
            if (!Serialize(obj_val.checklog_interval, "checklog_interval", alloc, json_val)) break; // @(field_type : int)

            return true;
        } while (0);

        return false;
    }
Exemple #9
0
void jsonValueToRapidJSON(JSONValue* value, rapidjson::Value& rapidValue, rapidjson::Document& document) {
	JSONInt* intValue = dynamic_cast<JSONInt*>(value);
	if (intValue) {
		rapidValue.SetInt(intValue->getValue());
		return;
	}
	JSONString* stringValue = dynamic_cast<JSONString*>(value);
	if (stringValue) {
		rapidValue.SetString(stringValue->getValue().c_str(), stringValue->getValue().size(), document.GetAllocator());
		return;
	}
	JSONBool* boolValue = dynamic_cast<JSONBool*>(value);
	if (boolValue) {
		rapidValue.SetBool(boolValue->getValue());
		return;
	}
	JSONArray* arrayValue = dynamic_cast<JSONArray*>(value);
	if (arrayValue) {
		rapidValue.SetArray();
		std::vector<JSONValue::ref> values = arrayValue->getValues();
		for (size_t i = 0; i < values.size(); i++) {
			rapidjson::Value obj;
			jsonValueToRapidJSON(values[i].get(), obj, document);
			rapidValue.PushBack(obj, document.GetAllocator());
		}
		return;
	}
	JSONObject* objectValue = dynamic_cast<JSONObject*>(value);
	if (objectValue) {
		rapidValue.SetObject();
		typedef std::map<std::string, JSONValue::ref> ValuesMap;
		ValuesMap values = objectValue->getValues();
		for (ValuesMap::iterator it = values.begin(); it != values.end(); it++) {
			rapidjson::Value obj;
			jsonValueToRapidJSON(it->second.get(), obj, document);
			rapidjson::Value key;
			key.SetString(it->first.c_str(), it->first.size(), document.GetAllocator());
			rapidValue.AddMember(key, obj, document.GetAllocator());
		}
		return;
	}
	assert(false);
}
Exemple #10
0
void
Plan::to_json_value(rapidjson::Document& d, rapidjson::Value& v) const
{
	rapidjson::Document::AllocatorType& allocator = d.GetAllocator();
	v.SetObject();
	// Avoid unused variable warnings
	(void)allocator;

	if (kind_) {
		rapidjson::Value v_kind;
		v_kind.SetString(*kind_, allocator);
		v.AddMember("kind", v_kind, allocator);
	}
	if (apiVersion_) {
		rapidjson::Value v_apiVersion;
		v_apiVersion.SetString(*apiVersion_, allocator);
		v.AddMember("apiVersion", v_apiVersion, allocator);
	}
	if (id_) {
		rapidjson::Value v_id;
		v_id.SetString(*id_, allocator);
		v.AddMember("id", v_id, allocator);
	}
	if (goal_id_) {
		rapidjson::Value v_goal_id;
		v_goal_id.SetString(*goal_id_, allocator);
		v.AddMember("goal-id", v_goal_id, allocator);
	}
	if (cost_) {
		rapidjson::Value v_cost;
		v_cost.SetFloat(*cost_);
		v.AddMember("cost", v_cost, allocator);
	}
	rapidjson::Value v_actions(rapidjson::kArrayType);
	v_actions.Reserve(actions_.size(), allocator);
	for (const auto & e : actions_) {
		rapidjson::Value v(rapidjson::kObjectType);
		e->to_json_value(d, v);
		v_actions.PushBack(v, allocator);
	}
	v.AddMember("actions", v_actions, allocator);

}
Exemple #11
0
void xml2json_traverse_node(rapidxml::xml_node<> *xmlnode, rapidjson::Value &jsvalue, rapidjson::Document::AllocatorType& allocator)
{
    //cout << "this: " << xmlnode->type() << " name: " << xmlnode->name() << " value: " << xmlnode->value() << endl;
    rapidjson::Value jsvalue_chd;

    jsvalue.SetObject();
    jsvalue_chd.SetObject();
    rapidxml::xml_node<> *xmlnode_chd;

    // classified discussion:
    if((xmlnode->type() == rapidxml::node_data || xmlnode->type() == rapidxml::node_cdata) && xmlnode->value())
    {
        // case: pure_text
        jsvalue.SetString(xmlnode->value(), allocator);  // then addmember("#text" , jsvalue, allocator)
    }
    else if(xmlnode->type() == rapidxml::node_element)
    {
        if(xmlnode->first_attribute())
        {
            if(xmlnode->first_node() && xmlnode->first_node()->type() == rapidxml::node_data && count_children(xmlnode) == 1)
            {
                // case: <e attr="xxx">text</e>
                rapidjson::Value jn, jv;
                jn.SetString(xml2json_text_additional_name, allocator);
                jv.SetString(xmlnode->first_node()->value(), allocator);
                jsvalue.AddMember(jn, jv, allocator);
                xml2json_add_attributes(xmlnode, jsvalue, allocator);
                return;
            }
            else
            {
                // case: <e attr="xxx">...</e>
                xml2json_add_attributes(xmlnode, jsvalue, allocator);
            }
        }
        else
        {
            if(!xmlnode->first_node())
            {
                // case: <e />
                jsvalue.SetNull();
                return;
            }
            else if(xmlnode->first_node()->type() == rapidxml::node_data && count_children(xmlnode) == 1)
            {
                // case: <e>text</e>
                if (xml2json_numeric_support == false)
                {
                    jsvalue.SetString(rapidjson::StringRef(xmlnode->first_node()->value()), allocator);
                }
                else
                {
                    bool hasDecimal;
                    if (xml2json_has_digits_only(xmlnode->first_node()->value(), &hasDecimal) == false)
                    {
                        jsvalue.SetString(rapidjson::StringRef(xmlnode->first_node()->value()), allocator);
                    }
                    else
                    {
                        char *bgnptr = xmlnode->first_node()->value();
                        char *endptr = bgnptr;
                        if (hasDecimal)
                        {
                            double value = std::strtod(bgnptr, &endptr);
                            if (bgnptr != endptr)
                              jsvalue.SetDouble(value);
                            else
                              jsvalue.SetString(rapidjson::StringRef(bgnptr), allocator);
                        }
                        else
                        {
                            long int value = std::strtol(bgnptr, &endptr, 0);
                            if (bgnptr != endptr)
                              jsvalue.SetInt(value);
                            else
                              jsvalue.SetString(rapidjson::StringRef(bgnptr), allocator);
                        }
                    }
                }
                return;
            }
        }
        if(xmlnode->first_node())
        {
            // case: complex else...
            std::map<std::string, int> name_count;
            for(xmlnode_chd = xmlnode->first_node(); xmlnode_chd; xmlnode_chd = xmlnode_chd->next_sibling())
            {
                std::string current_name;
                const char *name_ptr = NULL;
                rapidjson::Value jn, jv;
                if(xmlnode_chd->type() == rapidxml::node_data || xmlnode_chd->type() == rapidxml::node_cdata)
                {
                    current_name = xml2json_text_additional_name;
                    name_count[current_name]++;
                    jv.SetString(xml2json_text_additional_name, allocator);
                    name_ptr = jv.GetString();
                }
                else if(xmlnode_chd->type() == rapidxml::node_element)
                {
                    current_name = xmlnode_chd->name();
                    name_count[current_name]++;
                    name_ptr = xmlnode_chd->name();
                }
                xml2json_traverse_node(xmlnode_chd, jsvalue_chd, allocator);
                if(name_count[current_name] > 1 && name_ptr)
                    xml2json_to_array_form(name_ptr, jsvalue, jsvalue_chd, allocator);
                else
                {
                    jn.SetString(name_ptr, allocator);
                    jsvalue.AddMember(jn, jsvalue_chd, allocator);
                }
            }
        }
    }
    else
    {
        std::cerr << "err data!!" << std::endl;
    }
}
void ConvertToJSON(const msgpack::object& object, rapidjson::Value& value, rapidjson::MemoryPoolAllocator<>& allocator)
{
	switch (object.type)
	{
		case msgpack::type::BOOLEAN:
			value.SetBool(object.as<bool>());
			break;

		case msgpack::type::POSITIVE_INTEGER:
		case msgpack::type::NEGATIVE_INTEGER:
			value.SetInt(object.as<int>());
			break;

		case msgpack::type::FLOAT:
			value.SetDouble(object.as<double>());
			break;

		case msgpack::type::STR:
		{
			std::string string = object.as<std::string>();
			value.SetString(string.c_str(), string.size(), allocator);
			break;
		}

		case msgpack::type::ARRAY:
		{
			auto list = object.as<std::vector<msgpack::object>>();
			value.SetArray();

			for (auto& entry : list)
			{
				rapidjson::Value inValue;
				ConvertToJSON(entry, inValue, allocator);

				value.PushBack(inValue, allocator);
			}

			break;
		}

		case msgpack::type::MAP:
		{
			auto list = object.as<std::map<std::string, msgpack::object>>();
			value.SetObject();

			for (auto& entry : list)
			{
				rapidjson::Value inValue;
				ConvertToJSON(entry.second, inValue, allocator);

				rapidjson::Value name;
				name.SetString(entry.first.c_str(), entry.first.size(), allocator);

				value.AddMember(name, inValue, allocator);
			}

			break;
		}

		default:
			value.SetNull();
			break;
	}
}
Exemple #13
0
void traverse_node(rapidxml::xml_node<> *xmlnode, rapidjson::Value &jsvalue, rapidjson::Document::AllocatorType& allocator)
{
    //cout << "this: " << xmlnode->type() << " name: " << xmlnode->name() << " value: " << xmlnode->value() << endl;
    rapidjson::Value jsvalue_chd;

    jsvalue.SetObject();
    jsvalue_chd.SetObject();
    rapidxml::xml_node<> *xmlnode_chd;

    // classified discussion:
    if ((xmlnode->type() == rapidxml::node_data || xmlnode->type() == rapidxml::node_cdata) && xmlnode->value())
    {
        // case: pure_text
        jsvalue.SetString(xmlnode->value(), allocator); // then addmember("#text" , jsvalue, allocator)
    }
    else if (xmlnode->type() == rapidxml::node_element)
    {
        if (xmlnode->first_attribute())
        {
            if (xmlnode->first_node() && xmlnode->first_node()->type() == rapidxml::node_data && count_children(xmlnode) == 1)
            {
                // case: <e attr="xxx">text</e>
                rapidjson::Value jn, jv;
                jn.SetString("#text", allocator);
                jv.SetString(xmlnode->first_node()->value(), allocator);
                jsvalue.AddMember(jn, jv, allocator);
                add_attributes(xmlnode, jsvalue, allocator);
                return;
            }
            else
            {
                // case: <e attr="xxx">...</e>
                add_attributes(xmlnode, jsvalue, allocator);
            }
        }
        else
        {
            if (!xmlnode->first_node())
            {
                // case: <e />
                jsvalue.SetNull();
                return;
            }
            else if (xmlnode->first_node()->type() == rapidxml::node_data && count_children(xmlnode) == 1)
            {
                // case: <e>text</e>
                jsvalue.SetString(rapidjson::StringRef(xmlnode->first_node()->value()), allocator);
                return;
            }
        }
        if (xmlnode->first_node())
        {
            // case: complex else...
            std::map<std::string, int> name_count;
            for (xmlnode_chd = xmlnode->first_node(); xmlnode_chd; xmlnode_chd = xmlnode_chd->next_sibling())
            {
                std::string current_name;
                const char *name_ptr = NULL;
                rapidjson::Value jn, jv;
                if (xmlnode_chd->type() == rapidxml::node_data || xmlnode_chd->type() == rapidxml::node_cdata)
                {
                    current_name = "#text";
                    name_count[current_name]++;
                    jv.SetString("#text", allocator);
                    name_ptr = jv.GetString();
                }
                else if (xmlnode_chd->type() == rapidxml::node_element)
                {
                    current_name = xmlnode_chd->name();
                    name_count[current_name]++;
                    name_ptr = xmlnode_chd->name();
                }
                traverse_node(xmlnode_chd, jsvalue_chd, allocator);
                if (name_count[current_name] > 1 && name_ptr)
                    to_array_form(name_ptr, jsvalue, jsvalue_chd, allocator);
                else
                {
                    jn.SetString(name_ptr, allocator);
                    jsvalue.AddMember(jn, jsvalue_chd, allocator);
                }
            }
        }
    }
    else
    {
        std::cerr << "err data!!" << std::endl;
    }
}
static void ToRapidjsonValue(rapidjson::Value& rapidjsonValue, const JSONValue& jsonValue, rapidjson::MemoryPoolAllocator<>& allocator)
{
    switch (jsonValue.GetValueType())
    {
    case JSON_NULL:
        rapidjsonValue.SetNull();
        break;

    case JSON_BOOL:
        rapidjsonValue.SetBool(jsonValue.GetBool());
        break;

    case JSON_NUMBER:
        {
            switch (jsonValue.GetNumberType())
            {
            case JSONNT_INT:
                rapidjsonValue.SetInt(jsonValue.GetInt());
                break;

            case JSONNT_UINT:
                rapidjsonValue.SetUint(jsonValue.GetUInt());
                break;

            default:
                rapidjsonValue.SetDouble(jsonValue.GetDouble());
                break;
            }
        }
        break;

    case JSON_STRING:
        rapidjsonValue.SetString(jsonValue.GetCString(), allocator);
        break;

    case JSON_ARRAY:
        {
            const JSONArray& jsonArray = jsonValue.GetArray();

            rapidjsonValue.SetArray();
            rapidjsonValue.Reserve(jsonArray.Size(), allocator);

            for (unsigned i = 0; i < jsonArray.Size(); ++i)
            {
                rapidjson::Value value;
                rapidjsonValue.PushBack(value, allocator);
                ToRapidjsonValue(rapidjsonValue[i], jsonArray[i], allocator);
            }
        }
        break;

    case JSON_OBJECT:
        {
            const JSONObject& jsonObject = jsonValue.GetObject();

            rapidjsonValue.SetObject();
            for (JSONObject::ConstIterator i = jsonObject.Begin(); i != jsonObject.End(); ++i)
            {
                const char* name = i->first_.CString();
                rapidjson::Value value;
                rapidjsonValue.AddMember(name, value, allocator);
                ToRapidjsonValue(rapidjsonValue[name], i->second_, allocator);
            }
        }
        break;

    default:
        break;
    }
}
void jsonifyModuleType(const ModuleType& moduleType, rapidjson::Value& moduleTypeValue, rapidjson::Document& document){
    //TODO assert moduleType.isComposite

    moduleTypeValue.SetObject();
    moduleTypeValue.AddMember("name", moduleType.getName().c_str(), document.GetAllocator());
    moduleTypeValue.AddMember("description", moduleType.getDescription().c_str(), document.GetAllocator());

    //handle inputs
    rapidjson::Value inputs;
    inputs.SetArray();
    for(unsigned int i=0; i<moduleType.numInputs(); ++i){
        const ModuleInput* input = moduleType.getInput(i);
        rapidjson::Value inputValue;
        inputValue.SetObject();

        inputValue.AddMember("name", input->getName().c_str(), document.GetAllocator());

        int dimensionality = input->getSignalType().dimensionality;
        std::stringstream ss;
        ss << dimensionality << "f";
        rapidjson::Value typeValue(ss.str().c_str(), document.GetAllocator());
        inputValue.AddMember("type", typeValue, document.GetAllocator());
        inputs.PushBack(inputValue, document.GetAllocator());
    }
    moduleTypeValue.AddMember("inputs", inputs, document.GetAllocator());

    //handle outputs
    rapidjson::Value outputs;
    outputs.SetArray();
    for(unsigned int i=0; i<moduleType.numOutputs(); ++i){
        const ModuleOutput* output = moduleType.getOutput(i);
        const Module* outputsModule = moduleType.getGraph()->getModule("outputs");
        rapidjson::Value outputValue;
        outputValue.SetObject();
        outputValue.AddMember("name", output->getName().c_str(), document.GetAllocator());
        auto inputLink = outputsModule->getInput(i);
        auto outputLink = outputsModule->getInput(i)->getOutputLink();
        if(outputLink!=nullptr){
            std::string sourceString = outputLink->getOwner().getName() + "." + outputLink->getModuleOutput().getName();
            rapidjson::Value sourceStringValue(sourceString.c_str(),document.GetAllocator());
            outputValue.AddMember("source", sourceStringValue, document.GetAllocator());
        } else {
            rapidjson::Value vectorValue;
            vectorValue.SetArray();
            auto unlinkedValue = inputLink->getUnlinkedValue();
            for(int j=0; j<unlinkedValue.getSignalType().dimensionality; ++j){
                vectorValue.PushBack(unlinkedValue[j], document.GetAllocator());
            }
            outputValue.AddMember("source", vectorValue, document.GetAllocator());
        }
        outputs.PushBack(outputValue, document.GetAllocator());
    }
    moduleTypeValue.AddMember("outputs", outputs, document.GetAllocator());

    //handle internal modules
    rapidjson::Value modulesValue;
    modulesValue.SetArray();
    auto graph = moduleType.getGraph();
    graph->traverseModulesTopological([&](const Module& module){
        //skip input/output modules
        if(module.getType().isGraphInput() || module.getType().isGraphOutput())return;
        rapidjson::Value moduleValue;
        jsonifyModule(module, moduleValue, document);
        modulesValue.PushBack(moduleValue, document.GetAllocator());
    });
    moduleTypeValue.AddMember("modules", modulesValue, document.GetAllocator());
}
Exemple #16
0
static bool
pyobj2doc(PyObject *object, rapidjson::Value& doc, rapidjson::Document& root)
{
    if (PyBool_Check(object)) {
        if (Py_True == object) {
	        doc.SetBool(true);
        }
        else {
	        doc.SetBool(false);
        }
    }
    else if (Py_None == object) {
        doc.SetNull();
    }
    else if (PyFloat_Check(object)) {
        doc.SetDouble(PyFloat_AsDouble(object));
    }
    else if (PyInt_Check(object)) {
        doc.SetInt64(PyLong_AsLong(object));
    }
    else if (PyString_Check(object)) {
        doc.SetString(PyString_AsString(object), PyString_GET_SIZE(object));
    }
    else if (PyUnicode_Check(object)) {
        PyObject *utf8_item = PyUnicode_AsUTF8String(object);
        if (!utf8_item) {
            PyErr_SetString(PyExc_RuntimeError, "codec error.");
            return false;
        }
#ifdef PY3
        doc.SetString(PyBytes_AsString(utf8_item), PyBytes_GET_SIZE(utf8_item), root.GetAllocator());
#else
        doc.SetString(PyString_AsString(utf8_item), PyString_GET_SIZE(utf8_item), root.GetAllocator());
#endif
        Py_XDECREF(utf8_item);
    }
    else if (PyTuple_Check(object)) {
        int len = PyTuple_Size(object), i;

        doc.SetArray();
        rapidjson::Value _v;
        for (i = 0; i < len; ++i) {
            PyObject *elm = PyTuple_GetItem(object, i);
            if (false == pyobj2doc(elm, _v, root)) {
                return false;
            }
            doc.PushBack(_v, root.GetAllocator());
        }
    }
    else if (PyList_Check(object)) {
        int len = PyList_Size(object), i;

        doc.SetArray();
        rapidjson::Value _v;
        for (i = 0; i < len; ++i) {
            PyObject *elm = PyList_GetItem(object, i);
            if (false == pyobj2doc(elm, _v, root)) {
                return false;
            }
            doc.PushBack(_v, root.GetAllocator());
        }
    }
    else if (PyDict_Check(object)) {
        doc.SetObject();
        PyObject *key, *value;
        Py_ssize_t pos = 0;
        while (PyDict_Next(object, &pos, &key, &value)) {
            if (false == pyobj2doc_pair(key, value, doc, root)) {
                return false;
            }
        }
    }
    else {
        PyErr_SetString(PyExc_RuntimeError, "invalid python object");
        return false;
    }

    return true;
}
 bool clone_value(rapidjson::Value & output, const rapidjson::Value & input, rapidjson::Value::AllocatorType & allocator)
 {
     if(&output == &input)
     {
         return false;
     }
     
     if(input.IsNull())
     {
         output.SetNull();
     }
     else if(input.IsInt64())
     {
         output.SetInt(input.GetInt64());
     }
     else if(input.IsInt())
     {
         output.SetInt(input.GetInt());
     }
     else if(input.IsDouble())
     {
         output.SetDouble(input.GetDouble());
     }
     else if(input.IsBool())
     {
         output.SetBool(input.GetBool());
     }
     else if(input.IsString())
     {
         output.SetString(input.GetString(), input.GetStringLength(), allocator);
     }
     else if(input.IsArray())
     {
         rapidjson::Value temp;
         output.SetArray();
         output.Reserve(input.Size(), allocator);
         for(rapidjson::SizeType i = 0; i < input.Size(); ++i)
         {
             clone_value(temp, input[i], allocator);
             output.PushBack(temp, allocator);
         }
     }
     else if(input.IsObject())
     {
         output.SetObject();
         
         rapidjson::Value key, val;
         for(rapidjson::Value::ConstMemberIterator it = input.MemberBegin();
             it != input.MemberEnd(); ++it)
         {
             clone_value(key, it->name, allocator);
             clone_value(val, it->value, allocator);
             
             output.AddMember(key, val, allocator);
         }
     }
     else
     {
         assert(false && "shuldn't execute to here.");
         return false;
     }
     return true;
 }
Exemple #18
0
static void
pyobj2doc(PyObject *object, rapidjson::Value& doc, rapidjson::Document& root)
{
    if (PyBool_Check(object)) {
        if (Py_True == object) {
	        doc.SetBool(true);
        }
        else {
	        doc.SetBool(false);
        }
    }
    else if (Py_None == object) {
        doc.SetNull();
    }
    else if (PyFloat_Check(object)) {
        doc.SetDouble(PyFloat_AsDouble(object));
    }
    else if (PyInt_Check(object)) {
        doc.SetInt(PyLong_AsLong(object));
    }
    else if (PyString_Check(object)) {
        doc.SetString(PyString_AsString(object), PyString_GET_SIZE(object));
    }
    else if (PyUnicode_Check(object)) {
#ifdef PY3
        PyObject *utf8_item;
        utf8_item = PyUnicode_AsUTF8String(object);
        if (!utf8_item) {
            // TODO: error handling
            printf("error\n");
        }

        doc.SetString(PyBytes_AsString(utf8_item),
                      PyBytes_Size(utf8_item), root.GetAllocator());

        Py_XDECREF(utf8_item);
#else
        doc.SetString(PyBytes_AsString(object), PyBytes_GET_SIZE(object));
#endif
    }
    else if (PyTuple_Check(object)) {
        int len = PyTuple_Size(object), i;

        doc.SetArray();
        rapidjson::Value _v;
        for (i = 0; i < len; ++i) {
            PyObject *elm = PyList_GetItem(object, i);
            pyobj2doc(elm, _v, root);
            doc.PushBack(_v, root.GetAllocator());
        }
    }
    else if (PyList_Check(object)) {
        int len = PyList_Size(object), i;

        doc.SetArray();
        rapidjson::Value _v;
        for (i = 0; i < len; ++i) {
            PyObject *elm = PyList_GetItem(object, i);
            pyobj2doc(elm, _v, root);
            doc.PushBack(_v, root.GetAllocator());
        }
    }
    else if (PyDict_Check(object)) {
        doc.SetObject();
        PyObject *key, *value;
        Py_ssize_t pos = 0;
        while (PyDict_Next(object, &pos, &key, &value)) {
            pyobj2doc_pair(key, value, doc, root);
        }
    }
    else {
        // TODO: error handle
    }
}
    void tvalue2json(rapidjson::Value & output, const QVariant & input, rapidjson::Value::AllocatorType & allocator)
    {
        switch(input.type())
        {
        case QVariant::Invalid:
        {
            output.SetNull();
            break;
        }
        case QVariant::Bool:
        {
            output.SetBool(input.toBool());
            break;
        }
        case QVariant::Int:
        {
            output.SetInt64(input.toInt());
            break;
        }
        case QVariant::LongLong:
        {
            output.SetInt64(input.toLongLong());
            break;
        }
        case QVariant::Double:
        {
            output.SetDouble(input.toDouble());
            break;
        }
        case QVariant::String:
        {
            QByteArray str = input.toString().toUtf8();
            output.SetString(str.data(), str.size(), allocator);
            break;
        }
        case QVariant::StringList:
        {
            QStringList list = input.toStringList();

            output.SetArray();
            output.Reserve(list.size(), allocator);

            rapidjson::Value temp;
            for(QStringList::const_iterator it = list.begin(); it != list.end(); ++it)
            {
                QByteArray str = it->toUtf8();
                temp.SetString(str.data(), str.size(), allocator);
                output.PushBack(temp, allocator);
            }
            break;
        }
        case QVariant::List:
        {
            QList<QVariant> list = input.toList();

            output.SetArray();
            output.Reserve(list.size(), allocator);

            rapidjson::Value temp;
            for(QList<QVariant>::const_iterator it = list.begin(); it != list.end(); ++it)
            {
                tvalue2json(temp, *it, allocator);
                output.PushBack(temp, allocator);
            }
            break;
        }
        case QVariant::Map:
        {
            output.SetObject();
            rapidjson::Value tempK, tempV;

            QMap<QString, QVariant> qmap = input.toMap();
            for(QMap<QString, QVariant>::const_iterator it = qmap.begin(); it != qmap.end(); ++it)
            {
                tvalue2json(tempK, it.key(), allocator);
                tvalue2json(tempV, it.value(), allocator);
                output.AddMember(tempK, tempV, allocator);
            }
            break;
        }
        case QVariant::Point:
        {
            QPoint pt = input.toPoint();

            output.SetArray();
            output.Reserve(2, allocator);

            output.PushBack(pt.x(), allocator);
            output.PushBack(pt.y(), allocator);
            break;
        }
        case QVariant::PointF:
        {
            QPointF pt = input.toPointF();

            output.SetArray();
            output.Reserve(2, allocator);

            output.PushBack(pt.x(), allocator);
            output.PushBack(pt.y(), allocator);
            break;
        }
        case QVariant::Size:
        {
            QSize pt = input.toSize();

            output.SetArray();
            output.Reserve(2, allocator);

            output.PushBack(pt.width(), allocator);
            output.PushBack(pt.height(), allocator);
            break;
        }
        case QVariant::SizeF:
        {
            QSizeF pt = input.toSizeF();

            output.SetArray();
            output.Reserve(2, allocator);

            output.PushBack(pt.width(), allocator);
            output.PushBack(pt.height(), allocator);
            break;
        }
        case QVariant::Rect:
        {
            QRect pt = input.toRect();

            output.SetArray();
            output.Reserve(4, allocator);

            output.PushBack(pt.x(), allocator);
            output.PushBack(pt.y(), allocator);
            output.PushBack(pt.width(), allocator);
            output.PushBack(pt.height(), allocator);
            break;
        }
        case QVariant::RectF:
        {
            QRectF pt = input.toRectF();

            output.SetArray();
            output.Reserve(4, allocator);

            output.PushBack(pt.x(), allocator);
            output.PushBack(pt.y(), allocator);
            output.PushBack(pt.width(), allocator);
            output.PushBack(pt.height(), allocator);
            break;
        }
        case QVariant::Vector2D:
        {
            QVector2D pt = input.value<QVector2D>();

            output.SetArray();
            output.Reserve(2, allocator);

            output.PushBack(pt.x(), allocator);
            output.PushBack(pt.y(), allocator);
            break;
        }
        case QVariant::Vector3D:
        {
            QVector3D pt = input.value<QVector3D>();

            output.SetArray();
            output.Reserve(3, allocator);

            output.PushBack(pt.x(), allocator);
            output.PushBack(pt.y(), allocator);
            output.PushBack(pt.z(), allocator);
            break;
        }
        case QVariant::Vector4D:
        {
            QVector4D pt = input.value<QVector4D>();

            output.SetArray();
            output.Reserve(4, allocator);

            output.PushBack(pt.x(), allocator);
            output.PushBack(pt.y(), allocator);
            output.PushBack(pt.z(), allocator);
            output.PushBack(pt.w(), allocator);
            break;
        }
        case QVariant::Color:
        {
            QColor pt = input.value<QColor>();

            output.SetArray();
            output.Reserve(4, allocator);

            output.PushBack(pt.red(), allocator);
            output.PushBack(pt.green(), allocator);
            output.PushBack(pt.blue(), allocator);
            output.PushBack(pt.alpha(), allocator);
            break;
        }
        default:
        {
            output.SetNull();
            assert(false && "shuldn't execute to here.");
        }
        }
    }