void NsSmartDeviceLink::NsJSONHandler::Formatters::CFormatterJsonBase::
    objToJsonValue(const NsSmartDeviceLink::NsSmartObjects::SmartObject& obj,
                   utils::json::JsonValueRef item) {
  using namespace utils::json;
  try {
    if (NsSmartDeviceLink::NsSmartObjects::SmartType_Array == obj.getType()) {
      item = ValueType::ARRAY_VALUE;

      for (uint32_t i = 0; i < obj.length(); i++) {
        JsonValue value;

        objToJsonValue(obj.getElement(i), value);

        item.Append(value);
      }
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Map ==
               obj.getType()) {
      item = ValueType::OBJECT_VALUE;
      std::set<std::string> keys = obj.enumerate();

      for (std::set<std::string>::const_iterator key_itr = keys.begin(),
                                                 end = keys.end();
           key_itr != end;
           ++key_itr) {
        JsonValue value;

        objToJsonValue(obj.getElement(*key_itr), value);

        item[*key_itr] = value;
      }
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Boolean ==
               obj.getType()) {
      item = obj.asBool();
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Integer ==
               obj.getType()) {
      item = obj.asInt();
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Double ==
               obj.getType()) {
      item = obj.asDouble();
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Null ==
               obj.getType()) {
      item = ValueType::NULL_VALUE;
    } else {
      item = obj.asString();
    }
  } catch (...) {
  }
}
Example #2
0
Formatters::CFormatterJsonSDLRPCv2::tMetaFormatterErrorCode
  Formatters::CFormatterJsonSDLRPCv2::MetaFormatToString(
            const NsSmartDeviceLink::NsSmartObjects::SmartObject& object,
            const NsSmartDeviceLink::NsSmartObjects::CSmartSchema& schema,
            std::string& outStr) {

  meta_formatter_error_code::tMetaFormatterErrorCode result_code
                                    = meta_formatter_error_code::kErrorOk;

  NsSmartDeviceLink::NsSmartObjects::SmartObject tmp_object;

  if (false == CMetaFormatter::CreateObjectByPattern(object, schema, tmp_object)) {
      result_code |= meta_formatter_error_code::kErrorFailedCreateObjectBySchema;
      return result_code;
  }

  // determine whether smart objects are functions
  // (in terms of SDLRPC communication)
  bool is_root_object_created_by_schema = (
    (tmp_object.getType() == smart_objects_ns::SmartType_Map)
      && tmp_object.keyExists(jsonhandler_ns::strings::S_PARAMS)
      && tmp_object.keyExists(jsonhandler_ns::strings::S_MSG_PARAMS));

  bool is_root_object = (
    (object.getType() == smart_objects_ns::SmartType_Map)
      && object.keyExists(jsonhandler_ns::strings::S_PARAMS)
      && object.keyExists(jsonhandler_ns::strings::S_MSG_PARAMS));

  if (false == is_root_object) {
    result_code |= meta_formatter_error_code::kErrorObjectIsNotFunction;
  }
  if (false == is_root_object_created_by_schema) {
    result_code |= meta_formatter_error_code::kErrorSchemaIsNotFunction;
  }

  CFormatterJsonSDLRPCv2::toString(tmp_object, outStr);

  return result_code;
}