[74093] | 1 | /* $Id: RTCRestAnyObject.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * IPRT - C++ REST, RTCRestAnyObject implementation.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2018-2023 Oracle and/or its affiliates.
|
---|
[74093] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[74093] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[74093] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[74093] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[74093] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
| 41 | #define LOG_GROUP RTLOGGROUP_REST
|
---|
| 42 | #include <iprt/cpp/restanyobject.h>
|
---|
| 43 |
|
---|
| 44 | #include <iprt/assert.h>
|
---|
| 45 | #include <iprt/err.h>
|
---|
[74414] | 46 | #include <iprt/cpp/restoutput.h>
|
---|
[74093] | 47 |
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Default constructor.
|
---|
| 52 | */
|
---|
[74425] | 53 | RTCRestAnyObject::RTCRestAnyObject() RT_NOEXCEPT
|
---|
[74093] | 54 | : RTCRestObjectBase()
|
---|
| 55 | , m_pData(NULL)
|
---|
| 56 | {
|
---|
| 57 | m_fNullIndicator = true;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * Destructor.
|
---|
| 63 | */
|
---|
| 64 | RTCRestAnyObject::~RTCRestAnyObject()
|
---|
| 65 | {
|
---|
| 66 | if (m_pData)
|
---|
| 67 | {
|
---|
| 68 | delete m_pData;
|
---|
| 69 | m_pData = NULL;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * Copy constructor.
|
---|
| 76 | */
|
---|
| 77 | RTCRestAnyObject::RTCRestAnyObject(RTCRestAnyObject const &a_rThat)
|
---|
| 78 | : RTCRestObjectBase()
|
---|
| 79 | , m_pData(NULL)
|
---|
| 80 | {
|
---|
| 81 | int rc = assignCopy(a_rThat);
|
---|
| 82 | if (RT_FAILURE(rc))
|
---|
| 83 | throw std::bad_alloc();
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * Copy assignment operator.
|
---|
| 89 | */
|
---|
| 90 | RTCRestAnyObject &RTCRestAnyObject::operator=(RTCRestAnyObject const &a_rThat)
|
---|
| 91 | {
|
---|
| 92 | int rc = assignCopy(a_rThat);
|
---|
| 93 | if (RT_FAILURE(rc))
|
---|
| 94 | throw std::bad_alloc();
|
---|
| 95 | return *this;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * Safe copy assignment method.
|
---|
| 101 | */
|
---|
[74425] | 102 | int RTCRestAnyObject::assignCopy(RTCRestAnyObject const &a_rThat) RT_NOEXCEPT
|
---|
[74093] | 103 | {
|
---|
| 104 | setNull();
|
---|
| 105 | if ( !a_rThat.m_fNullIndicator
|
---|
| 106 | && a_rThat.m_pData != NULL)
|
---|
| 107 | {
|
---|
| 108 | kTypeClass enmType = a_rThat.m_pData->typeClass();
|
---|
| 109 | switch (enmType)
|
---|
| 110 | {
|
---|
| 111 | case kTypeClass_Bool: return assignCopy(*(RTCRestBool const *)a_rThat.m_pData);
|
---|
| 112 | case kTypeClass_Int64: return assignCopy(*(RTCRestInt64 const *)a_rThat.m_pData);
|
---|
| 113 | case kTypeClass_Int32: return assignCopy(*(RTCRestInt32 const *)a_rThat.m_pData);
|
---|
| 114 | case kTypeClass_Int16: return assignCopy(*(RTCRestInt16 const *)a_rThat.m_pData);
|
---|
| 115 | case kTypeClass_Double: return assignCopy(*(RTCRestDouble const *)a_rThat.m_pData);
|
---|
| 116 | case kTypeClass_String: return assignCopy(*(RTCRestString const *)a_rThat.m_pData);
|
---|
| 117 | case kTypeClass_Array: return assignCopy(*(RTCRestArray<RTCRestAnyObject> const *)a_rThat.m_pData);
|
---|
| 118 | case kTypeClass_StringMap: return assignCopy(*(RTCRestStringMap<RTCRestAnyObject> const *)a_rThat.m_pData);
|
---|
| 119 |
|
---|
| 120 | /* Currently unused of invalid: */
|
---|
[74149] | 121 | case kTypeClass_Date:
|
---|
| 122 | case kTypeClass_Uuid:
|
---|
| 123 | case kTypeClass_Binary:
|
---|
[74093] | 124 | case kTypeClass_StringEnum:
|
---|
[74347] | 125 | case kTypeClass_AnyObject:
|
---|
| 126 | case kTypeClass_DataObject:
|
---|
[74093] | 127 | case kTypeClass_Invalid:
|
---|
| 128 | AssertFailedReturn(VERR_REST_INTERNAL_ERROR_7);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | return VINF_SUCCESS;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * Safe copy assignment method, boolean variant.
|
---|
| 137 | */
|
---|
[74425] | 138 | int RTCRestAnyObject::assignCopy(RTCRestBool const &a_rThat) RT_NOEXCEPT
|
---|
[74093] | 139 | {
|
---|
| 140 | setNull();
|
---|
| 141 | RTCRestBool *pData = new (std::nothrow) RTCRestBool();
|
---|
| 142 | if (pData)
|
---|
| 143 | {
|
---|
| 144 | m_pData = pData;
|
---|
| 145 | m_fNullIndicator = false;
|
---|
| 146 | return pData->assignCopy(a_rThat);
|
---|
| 147 | }
|
---|
| 148 | return VERR_NO_MEMORY;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * Safe copy assignment method, int64_t variant.
|
---|
| 154 | */
|
---|
[74425] | 155 | int RTCRestAnyObject::assignCopy(RTCRestInt64 const &a_rThat) RT_NOEXCEPT
|
---|
[74093] | 156 | {
|
---|
| 157 | setNull();
|
---|
| 158 | RTCRestInt64 *pData = new (std::nothrow) RTCRestInt64();
|
---|
| 159 | if (pData)
|
---|
| 160 | {
|
---|
| 161 | m_pData = pData;
|
---|
| 162 | m_fNullIndicator = false;
|
---|
| 163 | return pData->assignCopy(a_rThat);
|
---|
| 164 | }
|
---|
| 165 | return VERR_NO_MEMORY;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | /**
|
---|
| 170 | * Safe copy assignment method, int32_t variant.
|
---|
| 171 | */
|
---|
[74425] | 172 | int RTCRestAnyObject::assignCopy(RTCRestInt32 const &a_rThat) RT_NOEXCEPT
|
---|
[74093] | 173 | {
|
---|
| 174 | setNull();
|
---|
| 175 | RTCRestInt32 *pData = new (std::nothrow) RTCRestInt32();
|
---|
| 176 | if (pData)
|
---|
| 177 | {
|
---|
| 178 | m_pData = pData;
|
---|
| 179 | m_fNullIndicator = false;
|
---|
| 180 | return pData->assignCopy(a_rThat);
|
---|
| 181 | }
|
---|
| 182 | return VERR_NO_MEMORY;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
| 187 | * Safe copy assignment method, int16_t variant.
|
---|
| 188 | */
|
---|
[74425] | 189 | int RTCRestAnyObject::assignCopy(RTCRestInt16 const &a_rThat) RT_NOEXCEPT
|
---|
[74093] | 190 | {
|
---|
| 191 | setNull();
|
---|
| 192 | RTCRestInt16 *pData = new (std::nothrow) RTCRestInt16();
|
---|
| 193 | if (pData)
|
---|
| 194 | {
|
---|
| 195 | m_pData = pData;
|
---|
| 196 | m_fNullIndicator = false;
|
---|
| 197 | return pData->assignCopy(a_rThat);
|
---|
| 198 | }
|
---|
| 199 | return VERR_NO_MEMORY;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | /**
|
---|
| 204 | * Safe copy assignment method, double variant.
|
---|
| 205 | */
|
---|
[74425] | 206 | int RTCRestAnyObject::assignCopy(RTCRestDouble const &a_rThat) RT_NOEXCEPT
|
---|
[74093] | 207 | {
|
---|
| 208 | setNull();
|
---|
| 209 | RTCRestDouble *pData = new (std::nothrow) RTCRestDouble();
|
---|
| 210 | if (pData)
|
---|
| 211 | {
|
---|
| 212 | m_pData = pData;
|
---|
| 213 | m_fNullIndicator = false;
|
---|
| 214 | return pData->assignCopy(a_rThat);
|
---|
| 215 | }
|
---|
| 216 | return VERR_NO_MEMORY;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | /**
|
---|
| 221 | * Safe copy assignment method, string variant.
|
---|
| 222 | */
|
---|
[74425] | 223 | int RTCRestAnyObject::assignCopy(RTCRestString const &a_rThat) RT_NOEXCEPT
|
---|
[74093] | 224 | {
|
---|
| 225 | setNull();
|
---|
| 226 | RTCRestString *pData = new (std::nothrow) RTCRestString();
|
---|
| 227 | if (pData)
|
---|
| 228 | {
|
---|
| 229 | m_pData = pData;
|
---|
| 230 | m_fNullIndicator = false;
|
---|
| 231 | return pData->assignCopy(a_rThat);
|
---|
| 232 | }
|
---|
| 233 | return VERR_NO_MEMORY;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 |
|
---|
| 237 | /**
|
---|
| 238 | * Safe copy assignment method, array variant.
|
---|
| 239 | */
|
---|
[74425] | 240 | int RTCRestAnyObject::assignCopy(RTCRestArray<RTCRestAnyObject> const &a_rThat) RT_NOEXCEPT
|
---|
[74093] | 241 | {
|
---|
| 242 | setNull();
|
---|
| 243 | RTCRestArray<RTCRestAnyObject> *pData = new (std::nothrow) RTCRestArray<RTCRestAnyObject>();
|
---|
| 244 | if (pData)
|
---|
| 245 | {
|
---|
| 246 | m_pData = pData;
|
---|
| 247 | m_fNullIndicator = false;
|
---|
| 248 | return pData->assignCopy(a_rThat);
|
---|
| 249 | }
|
---|
| 250 | return VERR_NO_MEMORY;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 |
|
---|
| 254 | /**
|
---|
| 255 | * Safe copy assignment method, string map variant.
|
---|
| 256 | */
|
---|
[74425] | 257 | int RTCRestAnyObject::assignCopy(RTCRestStringMap<RTCRestAnyObject> const &a_rThat) RT_NOEXCEPT
|
---|
[74093] | 258 | {
|
---|
| 259 | setNull();
|
---|
| 260 | RTCRestStringMap<RTCRestAnyObject> *pData = new (std::nothrow) RTCRestStringMap<RTCRestAnyObject>();
|
---|
| 261 | if (pData)
|
---|
| 262 | {
|
---|
| 263 | m_pData = pData;
|
---|
| 264 | m_fNullIndicator = false;
|
---|
| 265 | return pData->assignCopy(a_rThat);
|
---|
| 266 | }
|
---|
| 267 | return VERR_NO_MEMORY;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 |
|
---|
| 271 | /**
|
---|
| 272 | * Safe value assignment method, boolean variant.
|
---|
| 273 | */
|
---|
[74425] | 274 | int RTCRestAnyObject::assignValue(bool a_fValue) RT_NOEXCEPT
|
---|
[74093] | 275 | {
|
---|
| 276 | setNull();
|
---|
| 277 | RTCRestBool *pData = new (std::nothrow) RTCRestBool();
|
---|
| 278 | if (pData)
|
---|
| 279 | {
|
---|
| 280 | m_pData = pData;
|
---|
| 281 | pData->assignValue(a_fValue);
|
---|
| 282 | m_fNullIndicator = false;
|
---|
| 283 | return VINF_SUCCESS;
|
---|
| 284 | }
|
---|
| 285 | return VERR_NO_MEMORY;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 | /**
|
---|
| 290 | * Safe value assignment method, int64_t variant.
|
---|
| 291 | */
|
---|
[74425] | 292 | int RTCRestAnyObject::assignValue(int64_t a_iValue) RT_NOEXCEPT
|
---|
[74093] | 293 | {
|
---|
| 294 | setNull();
|
---|
| 295 | RTCRestInt64 *pData = new (std::nothrow) RTCRestInt64();
|
---|
| 296 | if (pData)
|
---|
| 297 | {
|
---|
| 298 | m_pData = pData;
|
---|
| 299 | pData->assignValue(a_iValue);
|
---|
| 300 | m_fNullIndicator = false;
|
---|
| 301 | return VINF_SUCCESS;
|
---|
| 302 | }
|
---|
| 303 | return VERR_NO_MEMORY;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 |
|
---|
| 307 | /**
|
---|
| 308 | * Safe value assignment method, int32_t variant.
|
---|
| 309 | */
|
---|
[74425] | 310 | int RTCRestAnyObject::assignValue(int32_t a_iValue) RT_NOEXCEPT
|
---|
[74093] | 311 | {
|
---|
| 312 | setNull();
|
---|
| 313 | RTCRestInt32 *pData = new (std::nothrow) RTCRestInt32();
|
---|
| 314 | if (pData)
|
---|
| 315 | {
|
---|
| 316 | m_pData = pData;
|
---|
| 317 | pData->assignValue(a_iValue);
|
---|
| 318 | m_fNullIndicator = false;
|
---|
| 319 | return VINF_SUCCESS;
|
---|
| 320 | }
|
---|
| 321 | return VERR_NO_MEMORY;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
| 325 | /**
|
---|
| 326 | * Safe value assignment method, int16_t variant.
|
---|
| 327 | */
|
---|
[74425] | 328 | int RTCRestAnyObject::assignValue(int16_t a_iValue) RT_NOEXCEPT
|
---|
[74093] | 329 | {
|
---|
| 330 | setNull();
|
---|
| 331 | RTCRestInt16 *pData = new (std::nothrow) RTCRestInt16();
|
---|
| 332 | if (pData)
|
---|
| 333 | {
|
---|
| 334 | m_pData = pData;
|
---|
| 335 | pData->assignValue(a_iValue);
|
---|
| 336 | m_fNullIndicator = false;
|
---|
| 337 | return VINF_SUCCESS;
|
---|
| 338 | }
|
---|
| 339 | return VERR_NO_MEMORY;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 |
|
---|
| 343 | /**
|
---|
| 344 | * Safe value assignment method, double variant.
|
---|
| 345 | */
|
---|
[74425] | 346 | int RTCRestAnyObject::assignValue(double a_iValue) RT_NOEXCEPT
|
---|
[74093] | 347 | {
|
---|
| 348 | setNull();
|
---|
| 349 | RTCRestDouble *pData = new (std::nothrow) RTCRestDouble();
|
---|
| 350 | if (pData)
|
---|
| 351 | {
|
---|
| 352 | m_pData = pData;
|
---|
| 353 | pData->assignValue(a_iValue);
|
---|
| 354 | m_fNullIndicator = false;
|
---|
| 355 | return VINF_SUCCESS;
|
---|
| 356 | }
|
---|
| 357 | return VERR_NO_MEMORY;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 |
|
---|
| 361 | /**
|
---|
| 362 | * Safe value assignment method, string variant.
|
---|
| 363 | */
|
---|
[74425] | 364 | int RTCRestAnyObject::assignValue(RTCString const &a_rValue) RT_NOEXCEPT
|
---|
[74093] | 365 | {
|
---|
| 366 | setNull();
|
---|
| 367 | RTCRestString *pData = new (std::nothrow) RTCRestString();
|
---|
| 368 | if (pData)
|
---|
| 369 | {
|
---|
| 370 | m_pData = pData;
|
---|
| 371 | m_fNullIndicator = false;
|
---|
| 372 | return pData->assignNoThrow(a_rValue);
|
---|
| 373 | }
|
---|
| 374 | return VERR_NO_MEMORY;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 |
|
---|
| 378 | /**
|
---|
| 379 | * Safe value assignment method, C-string variant.
|
---|
| 380 | */
|
---|
[74425] | 381 | int RTCRestAnyObject::assignValue(const char *a_pszValue) RT_NOEXCEPT
|
---|
[74093] | 382 | {
|
---|
| 383 | setNull();
|
---|
| 384 | RTCRestString *pData = new (std::nothrow) RTCRestString();
|
---|
| 385 | if (pData)
|
---|
| 386 | {
|
---|
| 387 | m_pData = pData;
|
---|
| 388 | m_fNullIndicator = false;
|
---|
| 389 | return pData->assignNoThrow(a_pszValue);
|
---|
| 390 | }
|
---|
| 391 | return VERR_NO_MEMORY;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 |
|
---|
[74425] | 395 | RTCRestObjectBase *RTCRestAnyObject::baseClone() const RT_NOEXCEPT
|
---|
[74386] | 396 | {
|
---|
| 397 | RTCRestAnyObject *pClone = new (std::nothrow) RTCRestAnyObject();
|
---|
| 398 | if (pClone)
|
---|
| 399 | {
|
---|
| 400 | int rc = pClone->assignCopy(*this);
|
---|
| 401 | if (RT_SUCCESS(rc))
|
---|
| 402 | return pClone;
|
---|
| 403 | delete pClone;
|
---|
| 404 | }
|
---|
| 405 | return NULL;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 |
|
---|
[74425] | 409 | int RTCRestAnyObject::setNull(void) RT_NOEXCEPT
|
---|
[74093] | 410 | {
|
---|
| 411 | if (m_pData)
|
---|
| 412 | {
|
---|
| 413 | delete m_pData;
|
---|
| 414 | m_pData = NULL;
|
---|
| 415 | }
|
---|
| 416 | return RTCRestObjectBase::setNull();
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 |
|
---|
[74425] | 420 | int RTCRestAnyObject::resetToDefault() RT_NOEXCEPT
|
---|
[74093] | 421 | {
|
---|
| 422 | if (m_pData)
|
---|
| 423 | return m_pData->resetToDefault();
|
---|
| 424 | return VINF_SUCCESS;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 |
|
---|
[74425] | 428 | RTCRestOutputBase &RTCRestAnyObject::serializeAsJson(RTCRestOutputBase &a_rDst) const RT_NOEXCEPT
|
---|
[74093] | 429 | {
|
---|
| 430 | if (m_pData)
|
---|
| 431 | return m_pData->serializeAsJson(a_rDst);
|
---|
[74414] | 432 | a_rDst.nullValue();
|
---|
[74093] | 433 | return a_rDst;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 |
|
---|
[74425] | 437 | int RTCRestAnyObject::deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT
|
---|
[74093] | 438 | {
|
---|
| 439 | setNull();
|
---|
| 440 |
|
---|
| 441 | RTJSONVALTYPE enmType = RTJsonValueGetType(a_rCursor.m_hValue);
|
---|
| 442 | switch (enmType)
|
---|
| 443 | {
|
---|
| 444 | case RTJSONVALTYPE_OBJECT:
|
---|
| 445 | {
|
---|
| 446 | RTCRestStringMap<RTCRestAnyObject> *pMap = new (std::nothrow) RTCRestStringMap<RTCRestAnyObject>();
|
---|
| 447 | if (pMap)
|
---|
| 448 | {
|
---|
| 449 | m_pData = pMap;
|
---|
| 450 | m_fNullIndicator = false;
|
---|
| 451 | return pMap->deserializeFromJson(a_rCursor);
|
---|
| 452 | }
|
---|
| 453 | break;
|
---|
| 454 | }
|
---|
| 455 |
|
---|
| 456 | case RTJSONVALTYPE_ARRAY:
|
---|
| 457 | {
|
---|
| 458 | RTCRestArray<RTCRestAnyObject> *pArray = new (std::nothrow) RTCRestArray<RTCRestAnyObject>();
|
---|
| 459 | if (pArray)
|
---|
| 460 | {
|
---|
| 461 | m_pData = pArray;
|
---|
| 462 | m_fNullIndicator = false;
|
---|
| 463 | return pArray->deserializeFromJson(a_rCursor);
|
---|
| 464 | }
|
---|
| 465 | break;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | case RTJSONVALTYPE_STRING:
|
---|
| 469 | {
|
---|
| 470 | RTCRestString *pString = new (std::nothrow) RTCRestString();
|
---|
| 471 | if (pString)
|
---|
| 472 | {
|
---|
| 473 | m_pData = pString;
|
---|
| 474 | m_fNullIndicator = false;
|
---|
| 475 | return pString->deserializeFromJson(a_rCursor);
|
---|
| 476 | }
|
---|
| 477 | break;
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | case RTJSONVALTYPE_INTEGER:
|
---|
| 481 | {
|
---|
| 482 | RTCRestInt64 *pInt64 = new (std::nothrow) RTCRestInt64();
|
---|
| 483 | if (pInt64)
|
---|
| 484 | {
|
---|
| 485 | m_pData = pInt64;
|
---|
| 486 | m_fNullIndicator = false;
|
---|
| 487 | return pInt64->deserializeFromJson(a_rCursor);
|
---|
| 488 | }
|
---|
| 489 | break;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | case RTJSONVALTYPE_NUMBER:
|
---|
| 493 | {
|
---|
| 494 | RTCRestDouble *pDouble = new (std::nothrow) RTCRestDouble();
|
---|
| 495 | if (pDouble)
|
---|
| 496 | {
|
---|
| 497 | m_pData = pDouble;
|
---|
| 498 | m_fNullIndicator = false;
|
---|
| 499 | return pDouble->deserializeFromJson(a_rCursor);
|
---|
| 500 | }
|
---|
| 501 | break;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | case RTJSONVALTYPE_NULL:
|
---|
| 505 | return VINF_SUCCESS;
|
---|
| 506 |
|
---|
| 507 | case RTJSONVALTYPE_TRUE:
|
---|
| 508 | case RTJSONVALTYPE_FALSE:
|
---|
| 509 | {
|
---|
| 510 | RTCRestBool *pBool = new (std::nothrow) RTCRestBool();
|
---|
| 511 | if (pBool)
|
---|
| 512 | {
|
---|
| 513 | m_pData = pBool;
|
---|
| 514 | m_fNullIndicator = false;
|
---|
| 515 | pBool->assignValue(enmType == RTJSONVALTYPE_TRUE);
|
---|
| 516 | return VINF_SUCCESS;
|
---|
| 517 | }
|
---|
| 518 | break;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | /* no break. */
|
---|
| 522 | case RTJSONVALTYPE_INVALID:
|
---|
| 523 | case RTJSONVALTYPE_32BIT_HACK:
|
---|
| 524 | break;
|
---|
| 525 | }
|
---|
| 526 | return a_rCursor.m_pPrimary->addError(a_rCursor, VERR_WRONG_TYPE, "RTCRestAnyObject found %d (%s)",
|
---|
| 527 | enmType, RTJsonValueTypeName(enmType));
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 |
|
---|
[74425] | 531 | int RTCRestAnyObject::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const RT_NOEXCEPT
|
---|
[74093] | 532 | {
|
---|
| 533 | if (m_pData)
|
---|
| 534 | return m_pData->toString(a_pDst, a_fFlags);
|
---|
| 535 | if (a_fFlags & kToString_Append)
|
---|
| 536 | return a_pDst->appendNoThrow(RT_STR_TUPLE("null"));
|
---|
| 537 | return a_pDst->assignNoThrow(RT_STR_TUPLE("null"));
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 |
|
---|
| 541 | int RTCRestAnyObject::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/,
|
---|
[74425] | 542 | uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) RT_NOEXCEPT
|
---|
[74093] | 543 | {
|
---|
| 544 | return RTCRestObjectBase::fromString(a_rValue, a_pszName, a_pErrInfo, a_fFlags);
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 |
|
---|
[74425] | 548 | RTCRestObjectBase::kTypeClass RTCRestAnyObject::typeClass(void) const RT_NOEXCEPT
|
---|
[74093] | 549 | {
|
---|
[74347] | 550 | return kTypeClass_AnyObject;
|
---|
[74093] | 551 | }
|
---|
| 552 |
|
---|
| 553 |
|
---|
[74425] | 554 | const char *RTCRestAnyObject::typeName(void) const RT_NOEXCEPT
|
---|
[74093] | 555 | {
|
---|
| 556 | if (m_pData)
|
---|
| 557 | {
|
---|
| 558 | kTypeClass enmType = m_pData->typeClass();
|
---|
| 559 | switch (enmType)
|
---|
| 560 | {
|
---|
| 561 | case kTypeClass_Bool: return "RTCRestAnyObject[Bool]";
|
---|
| 562 | case kTypeClass_Int64: return "RTCRestAnyObject[Int64]";
|
---|
| 563 | case kTypeClass_Int32: return "RTCRestAnyObject[Int32]";
|
---|
| 564 | case kTypeClass_Int16: return "RTCRestAnyObject[Int16]";
|
---|
| 565 | case kTypeClass_Double: return "RTCRestAnyObject[Double]";
|
---|
| 566 | case kTypeClass_String: return "RTCRestAnyObject[String]";
|
---|
| 567 | case kTypeClass_Array: return "RTCRestAnyObject[Array]";
|
---|
| 568 | case kTypeClass_StringMap: return "RTCRestAnyObject[StringMap]";
|
---|
| 569 |
|
---|
| 570 | /* Currently unused of invalid: */
|
---|
[74149] | 571 | case kTypeClass_Date:
|
---|
| 572 | case kTypeClass_Uuid:
|
---|
| 573 | case kTypeClass_Binary:
|
---|
[74093] | 574 | case kTypeClass_StringEnum:
|
---|
[74347] | 575 | case kTypeClass_DataObject:
|
---|
| 576 | case kTypeClass_AnyObject:
|
---|
[74093] | 577 | case kTypeClass_Invalid:
|
---|
| 578 | AssertFailed();
|
---|
| 579 | }
|
---|
| 580 | }
|
---|
| 581 | return "RTCRestAnyObject";
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 |
|
---|
| 585 | /**
|
---|
| 586 | * Factory method.
|
---|
| 587 | */
|
---|
[74425] | 588 | /*static*/ DECLCALLBACK(RTCRestObjectBase *) RTCRestAnyObject::createInstance(void) RT_NOEXCEPT
|
---|
[74093] | 589 | {
|
---|
| 590 | return new (std::nothrow) RTCRestAnyObject();
|
---|
| 591 | }
|
---|
| 592 |
|
---|
[74402] | 593 |
|
---|
| 594 | /**
|
---|
| 595 | * @copydoc RTCRestObjectBase::FNDESERIALIZEINSTANCEFROMJSON
|
---|
| 596 | */
|
---|
| 597 | /*static*/ DECLCALLBACK(int)
|
---|
| 598 | RTCRestAnyObject::deserializeInstanceFromJson(RTCRestJsonCursor const &a_rCursor, RTCRestObjectBase **a_ppInstance)
|
---|
| 599 | {
|
---|
| 600 | RTCRestObjectBase *pObj = createInstance();
|
---|
| 601 | *a_ppInstance = pObj;
|
---|
| 602 | if (pObj)
|
---|
| 603 | return pObj->deserializeFromJson(a_rCursor);
|
---|
| 604 | return a_rCursor.m_pPrimary->addError(a_rCursor, VERR_NO_MEMORY, "Out of memory");
|
---|
| 605 | }
|
---|
| 606 |
|
---|