| 182 | } |
| 183 | |
| 184 | int VariantUtilities::ConvertVariantToJsonValue(IElementManager* element_manager, |
| 185 | VARIANT variant_value, |
| 186 | Json::Value* value) { |
| 187 | int status_code = WD_SUCCESS; |
| 188 | if (VariantIsString(variant_value)) { |
| 189 | std::string string_value = ""; |
| 190 | if (variant_value.bstrVal) { |
| 191 | std::wstring bstr_value = variant_value.bstrVal; |
| 192 | string_value = StringUtilities::ToString(bstr_value); |
| 193 | } |
| 194 | *value = string_value; |
| 195 | } else if (VariantIsInteger(variant_value)) { |
| 196 | *value = variant_value.lVal; |
| 197 | } else if (VariantIsDouble(variant_value)) { |
| 198 | double int_part; |
| 199 | if (std::modf(variant_value.dblVal, &int_part) == 0.0) { |
| 200 | // This bears some explaining. Due to inconsistencies between versions |
| 201 | // of the JSON serializer we use, if the value is floating-point, but |
| 202 | // has no fractional part, convert it to a 64-bit integer so that it |
| 203 | // will be serialized in a way consistent with language bindings' |
| 204 | // expectations. |
| 205 | *value = static_cast<long long>(int_part); |
| 206 | } else { |
| 207 | *value = variant_value.dblVal; |
| 208 | } |
| 209 | } else if (VariantIsBoolean(variant_value)) { |
| 210 | *value = variant_value.boolVal == VARIANT_TRUE; |
| 211 | } else if (VariantIsEmpty(variant_value)) { |
| 212 | *value = Json::Value::null; |
| 213 | } else if (variant_value.vt == VT_NULL) { |
| 214 | *value = Json::Value::null; |
| 215 | } else if (VariantIsIDispatch(variant_value)) { |
| 216 | if (VariantIsArray(variant_value) || |
| 217 | VariantIsElementCollection(variant_value)) { |
| 218 | Json::Value result_array(Json::arrayValue); |
| 219 | |
| 220 | long length = 0; |
| 221 | status_code = GetArrayLength(variant_value.pdispVal, &length); |
| 222 | if (status_code != WD_SUCCESS) { |
| 223 | LOG(WARN) << "Did not successfully get array length."; |
| 224 | return EUNEXPECTEDJSERROR; |
| 225 | } |
| 226 | |
| 227 | for (long i = 0; i < length; ++i) { |
| 228 | CComVariant array_item; |
| 229 | int array_item_status = GetArrayItem(variant_value.pdispVal, |
| 230 | i, |
| 231 | &array_item); |
| 232 | if (array_item_status != WD_SUCCESS) { |
| 233 | LOG(WARN) << "Did not successfully get item with index " |
| 234 | << i << " from array."; |
| 235 | return EUNEXPECTEDJSERROR; |
| 236 | } |
| 237 | Json::Value array_item_result; |
| 238 | ConvertVariantToJsonValue(element_manager, |
| 239 | array_item, |
| 240 | &array_item_result); |
| 241 | result_array[i] = array_item_result; |
nothing calls this directly
no test coverage detected