LCOV - code coverage report
Current view: top level - port - cpl_json.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 585 651 89.9 %
Date: 2026-05-11 17:34:11 Functions: 90 92 97.8 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  * Project:  Common Portability Library
       3             :  * Purpose:  Function wrapper for libjson-c access.
       4             :  * Author:   Dmitry Baryshnikov, dmitry.baryshnikov@nextgis.com
       5             :  *
       6             :  ******************************************************************************
       7             :  * Copyright (c) 2017-2018 NextGIS, <info@nextgis.com>
       8             :  *
       9             :  * SPDX-License-Identifier: MIT
      10             :  ****************************************************************************/
      11             : 
      12             : #include "cpl_json.h"
      13             : 
      14             : #include "cpl_error.h"
      15             : #include "cpl_json_header.h"
      16             : #include "cpl_vsi.h"
      17             : 
      18             : #include "cpl_http.h"
      19             : #include "cpl_multiproc.h"
      20             : 
      21             : #define TO_JSONOBJ(x) static_cast<json_object *>(x)
      22             : 
      23             : static const char *JSON_PATH_DELIMITER = "/";
      24             : 
      25             : static const char *INVALID_OBJ_KEY = "__INVALID_OBJ_KEY__";
      26             : 
      27             : #define JSON_C_VER_014 (14 << 8)
      28             : 
      29             : // json_object_new_uint64() was added in libjson-c 0.14
      30             : #if (!defined(JSON_C_VERSION_NUM)) || (JSON_C_VERSION_NUM < JSON_C_VER_014)
      31             : 
      32             : static int CPLJSON_json_object_new_uint64_formatter(struct json_object *jso,
      33             :                                                     struct printbuf *pb,
      34             :                                                     int /* level */,
      35             :                                                     int /* flags */)
      36             : {
      37             :     const char *pszStr = json_object_get_string(jso);
      38             :     return printbuf_memappend(pb, pszStr, static_cast<int>(strlen(pszStr)));
      39             : }
      40             : 
      41             : static json_object *CPLJSON_json_object_new_uint64(uint64_t nVal)
      42             : {
      43             :     json_object *jso = json_object_new_string(
      44             :         CPLSPrintf(CPL_FRMT_GUIB, static_cast<GUIntBig>(nVal)));
      45             :     json_object_set_serializer(jso, CPLJSON_json_object_new_uint64_formatter,
      46             :                                nullptr, nullptr);
      47             :     return jso;
      48             : }
      49             : 
      50             : #define json_object_new_uint64 CPLJSON_json_object_new_uint64
      51             : 
      52             : #endif
      53             : 
      54             : //------------------------------------------------------------------------------
      55             : // JSONDocument
      56             : //------------------------------------------------------------------------------
      57             : /*! @cond Doxygen_Suppress */
      58       28077 : CPLJSONDocument::CPLJSONDocument() : m_poRootJsonObject(nullptr)
      59             : {
      60       28077 : }
      61             : 
      62       56168 : CPLJSONDocument::~CPLJSONDocument()
      63             : {
      64       28084 :     if (m_poRootJsonObject)
      65       22341 :         json_object_put(TO_JSONOBJ(m_poRootJsonObject));
      66       28084 : }
      67             : 
      68           6 : CPLJSONDocument::CPLJSONDocument(const CPLJSONDocument &other)
      69           6 :     : m_poRootJsonObject(json_object_get(TO_JSONOBJ(other.m_poRootJsonObject)))
      70             : {
      71           6 : }
      72             : 
      73           2 : CPLJSONDocument &CPLJSONDocument::operator=(const CPLJSONDocument &other)
      74             : {
      75           2 :     if (this == &other)
      76           1 :         return *this;
      77             : 
      78           1 :     if (m_poRootJsonObject)
      79           1 :         json_object_put(TO_JSONOBJ(m_poRootJsonObject));
      80           1 :     m_poRootJsonObject = json_object_get(TO_JSONOBJ(other.m_poRootJsonObject));
      81             : 
      82           1 :     return *this;
      83             : }
      84             : 
      85           1 : CPLJSONDocument::CPLJSONDocument(CPLJSONDocument &&other)
      86           1 :     : m_poRootJsonObject(other.m_poRootJsonObject)
      87             : {
      88           1 :     other.m_poRootJsonObject = nullptr;
      89           1 : }
      90             : 
      91         119 : CPLJSONDocument &CPLJSONDocument::operator=(CPLJSONDocument &&other)
      92             : {
      93         119 :     if (this == &other)
      94           0 :         return *this;
      95             : 
      96         119 :     if (m_poRootJsonObject)
      97          21 :         json_object_put(TO_JSONOBJ(m_poRootJsonObject));
      98         119 :     m_poRootJsonObject = other.m_poRootJsonObject;
      99         119 :     other.m_poRootJsonObject = nullptr;
     100             : 
     101         119 :     return *this;
     102             : }
     103             : 
     104             : /*! @endcond */
     105             : 
     106             : /**
     107             :  * Save json document at specified path
     108             :  * @param  osPath Path to save json document
     109             :  * @return         true on success. If error occurred it can be received using
     110             :  * CPLGetLastErrorMsg method.
     111             :  *
     112             :  */
     113        1673 : bool CPLJSONDocument::Save(const std::string &osPath) const
     114             : {
     115        1673 :     VSILFILE *fp = VSIFOpenL(osPath.c_str(), "wt");
     116        1673 :     if (nullptr == fp)
     117             :     {
     118          14 :         CPLError(CE_Failure, CPLE_NoWriteAccess,
     119             :                  "File %s cannot be opened for writing", osPath.c_str());
     120          14 :         return false;
     121             :     }
     122             : 
     123        3318 :     const char *pabyData = json_object_to_json_string_ext(
     124        1659 :         TO_JSONOBJ(m_poRootJsonObject), JSON_C_TO_STRING_PRETTY);
     125        1659 :     bool bRet = VSIFWriteL(pabyData, strlen(pabyData), 1, fp) == 1;
     126             : 
     127        1659 :     bRet = VSIFCloseL(fp) == 0 && bRet;
     128             : 
     129        1659 :     return bRet;
     130             : }
     131             : 
     132             : /**
     133             :  * Return the json document as a serialized string.
     134             :  * @return         serialized document.
     135             :  *
     136             :  */
     137        8929 : std::string CPLJSONDocument::SaveAsString() const
     138             : {
     139        8929 :     return json_object_to_json_string_ext(TO_JSONOBJ(m_poRootJsonObject),
     140        8929 :                                           JSON_C_TO_STRING_PRETTY);
     141             : }
     142             : 
     143             : /**
     144             :  * Get json document root object
     145             :  * @return CPLJSONObject class instance
     146             :  *
     147             :  * @since GDAL 3.1
     148             :  */
     149          93 : const CPLJSONObject CPLJSONDocument::GetRoot() const
     150             : {
     151          93 :     return const_cast<CPLJSONDocument *>(this)->GetRoot();
     152             : }
     153             : 
     154             : /**
     155             :  * Get json document root object
     156             :  * @return CPLJSONObject class instance
     157             :  *
     158             :  */
     159       28631 : CPLJSONObject CPLJSONDocument::GetRoot()
     160             : {
     161       28631 :     if (nullptr == m_poRootJsonObject)
     162             :     {
     163        1750 :         m_poRootJsonObject = json_object_new_object();
     164             :     }
     165             : 
     166       28631 :     if (json_object_get_type(TO_JSONOBJ(m_poRootJsonObject)) == json_type_array)
     167             :     {
     168        6094 :         return CPLJSONArray("", m_poRootJsonObject);
     169             :     }
     170             :     else
     171             :     {
     172       25584 :         return CPLJSONObject("", m_poRootJsonObject);
     173             :     }
     174             : }
     175             : 
     176             : /**
     177             :  * Set json document root object
     178             :  * @param oRoot CPLJSONObject root object
     179             :  *
     180             :  * @since GDAL 3.4
     181             :  */
     182        8871 : void CPLJSONDocument::SetRoot(const CPLJSONObject &oRoot)
     183             : {
     184        8871 :     if (m_poRootJsonObject)
     185           0 :         json_object_put(TO_JSONOBJ(m_poRootJsonObject));
     186        8871 :     m_poRootJsonObject = json_object_get(TO_JSONOBJ(oRoot.m_poJsonObject));
     187        8871 : }
     188             : 
     189             : /**
     190             :  * Load json document from file by provided path
     191             :  * @param  osPath Path to json file.
     192             :  * @return         true on success. If error occurred it can be received using
     193             :  * CPLGetLastErrorMsg method.
     194             :  *
     195             :  */
     196        7680 : bool CPLJSONDocument::Load(const std::string &osPath)
     197             : {
     198        7680 :     GByte *pabyOut = nullptr;
     199        7680 :     vsi_l_offset nSize = 0;
     200             : 
     201        7680 :     GIntBig nMaxSize = 0;
     202        7680 :     if (CPLParseMemorySize(CPLGetConfigOption("CPL_JSON_MAX_SIZE", "100MB"),
     203       15360 :                            &nMaxSize, nullptr) != CE_None ||
     204        7680 :         nMaxSize <= 0)
     205           0 :         return false;
     206             : 
     207        7680 :     if (!VSIIngestFile(nullptr, osPath.c_str(), &pabyOut, &nSize, nMaxSize))
     208             :     {
     209         756 :         CPLError(CE_Failure, CPLE_FileIO, "Load json file %s failed",
     210             :                  osPath.c_str());
     211         756 :         return false;
     212             :     }
     213             : 
     214        6924 :     bool bResult = LoadMemory(pabyOut, static_cast<int>(nSize));
     215        6924 :     VSIFree(pabyOut);
     216        6924 :     return bResult;
     217             : }
     218             : 
     219             : /**
     220             :  * Load json document from memory buffer.
     221             :  * @param  pabyData Buffer.data.
     222             :  * @param  nLength  Buffer size.
     223             :  * @return          true on success. If error occurred it can be received using
     224             :  * CPLGetLastErrorMsg method.
     225             :  *
     226             :  */
     227       20072 : bool CPLJSONDocument::LoadMemory(const GByte *pabyData, int nLength)
     228             : {
     229       20072 :     if (nullptr == pabyData)
     230             :     {
     231           1 :         return false;
     232             :     }
     233             : 
     234       20071 :     if (m_poRootJsonObject)
     235        8308 :         json_object_put(TO_JSONOBJ(m_poRootJsonObject));
     236             : 
     237       20071 :     if (nLength == 4 &&
     238           8 :         memcmp(reinterpret_cast<const char *>(pabyData), "true", nLength) == 0)
     239             :     {
     240           5 :         m_poRootJsonObject = json_object_new_boolean(true);
     241           5 :         return true;
     242             :     }
     243             : 
     244       20066 :     if (nLength == 5 &&
     245           6 :         memcmp(reinterpret_cast<const char *>(pabyData), "false", nLength) == 0)
     246             :     {
     247           3 :         m_poRootJsonObject = json_object_new_boolean(false);
     248           3 :         return true;
     249             :     }
     250             : 
     251       20063 :     json_tokener *jstok = json_tokener_new();
     252       20063 :     m_poRootJsonObject = json_tokener_parse_ex(
     253             :         jstok, reinterpret_cast<const char *>(pabyData), nLength);
     254       20063 :     bool bParsed = jstok->err == json_tokener_success;
     255       20063 :     if (!bParsed)
     256             :     {
     257          26 :         CPLError(CE_Failure, CPLE_AppDefined,
     258             :                  "JSON parsing error: %s (at offset %d)",
     259             :                  json_tokener_error_desc(jstok->err), jstok->char_offset);
     260          26 :         json_tokener_free(jstok);
     261          26 :         return false;
     262             :     }
     263       20037 :     json_tokener_free(jstok);
     264       20037 :     return bParsed;
     265             : }
     266             : 
     267             : /**
     268             :  * Load json document from memory buffer.
     269             :  * @param  osStr    String
     270             :  * @return          true on success. If error occurred it can be received using
     271             :  * CPLGetLastErrorMsg method.
     272             :  *
     273             :  */
     274       13043 : bool CPLJSONDocument::LoadMemory(const std::string &osStr)
     275             : {
     276       13043 :     if (osStr.empty())
     277           3 :         return false;
     278       13040 :     return LoadMemory(reinterpret_cast<const GByte *>(osStr.data()),
     279       26080 :                       static_cast<int>(osStr.size()));
     280             : }
     281             : 
     282             : /**
     283             :  * Load json document from file using small chunks of data.
     284             :  * @param  osPath      Path to json document file.
     285             :  * @param  nChunkSize   Chunk size.
     286             :  * @param  pfnProgress  a function to report progress of the json data loading.
     287             :  * @param  pProgressArg application data passed into progress function.
     288             :  * @return              true on success. If error occurred it can be received
     289             :  * using CPLGetLastErrorMsg method.
     290             :  *
     291             :  */
     292           3 : bool CPLJSONDocument::LoadChunks(const std::string &osPath, size_t nChunkSize,
     293             :                                  GDALProgressFunc pfnProgress,
     294             :                                  void *pProgressArg)
     295             : {
     296             :     VSIStatBufL sStatBuf;
     297           3 :     if (VSIStatL(osPath.c_str(), &sStatBuf) != 0)
     298             :     {
     299           2 :         CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s", osPath.c_str());
     300           2 :         return false;
     301             :     }
     302             : 
     303           1 :     VSILFILE *fp = VSIFOpenL(osPath.c_str(), "rb");
     304           1 :     if (fp == nullptr)
     305             :     {
     306           0 :         CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s", osPath.c_str());
     307           0 :         return false;
     308             :     }
     309             : 
     310           1 :     void *pBuffer = CPLMalloc(nChunkSize);
     311           1 :     json_tokener *tok = json_tokener_new();
     312           1 :     bool bSuccess = true;
     313           1 :     GUInt32 nFileSize = static_cast<GUInt32>(sStatBuf.st_size);
     314           1 :     double dfTotalRead = 0.0;
     315             : 
     316             :     while (true)
     317             :     {
     318           6 :         size_t nRead = VSIFReadL(pBuffer, 1, nChunkSize, fp);
     319           6 :         dfTotalRead += nRead;
     320             : 
     321           6 :         if (m_poRootJsonObject)
     322           1 :             json_object_put(TO_JSONOBJ(m_poRootJsonObject));
     323             : 
     324           6 :         m_poRootJsonObject = json_tokener_parse_ex(
     325             :             tok, static_cast<const char *>(pBuffer), static_cast<int>(nRead));
     326             : 
     327           6 :         enum json_tokener_error jerr = json_tokener_get_error(tok);
     328           6 :         if (jerr != json_tokener_continue && jerr != json_tokener_success)
     329             :         {
     330           0 :             CPLError(CE_Failure, CPLE_AppDefined, "JSON error: %s",
     331             :                      json_tokener_error_desc(jerr));
     332           0 :             bSuccess = false;
     333           0 :             break;
     334             :         }
     335             : 
     336           6 :         if (nRead < nChunkSize)
     337             :         {
     338           1 :             break;
     339             :         }
     340             : 
     341           5 :         if (nullptr != pfnProgress)
     342             :         {
     343           0 :             pfnProgress(dfTotalRead / nFileSize, "Loading ...", pProgressArg);
     344             :         }
     345           5 :     }
     346             : 
     347           1 :     json_tokener_free(tok);
     348           1 :     CPLFree(pBuffer);
     349           1 :     VSIFCloseL(fp);
     350             : 
     351           1 :     if (nullptr != pfnProgress)
     352             :     {
     353           0 :         pfnProgress(1.0, "Loading ...", pProgressArg);
     354             :     }
     355             : 
     356           1 :     return bSuccess;
     357             : }
     358             : 
     359             : /*! @cond Doxygen_Suppress */
     360             : #ifdef HAVE_CURL
     361             : 
     362             : typedef struct
     363             : {
     364             :     json_object *pObject;
     365             :     json_tokener *pTokener;
     366             : } JsonContext, *JsonContextL;
     367             : 
     368           0 : static size_t CPLJSONWriteFunction(void *pBuffer, size_t nSize, size_t nMemb,
     369             :                                    void *pUserData)
     370             : {
     371           0 :     size_t nLength = nSize * nMemb;
     372           0 :     JsonContextL ctx = static_cast<JsonContextL>(pUserData);
     373           0 :     if (ctx->pObject != nullptr)
     374             :     {
     375           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     376             :                  "A complete JSon object had already been parsed before new "
     377             :                  "content is appended to it");
     378           0 :         return 0;
     379             :     }
     380           0 :     ctx->pObject =
     381           0 :         json_tokener_parse_ex(ctx->pTokener, static_cast<const char *>(pBuffer),
     382             :                               static_cast<int>(nLength));
     383           0 :     switch (json_tokener_get_error(ctx->pTokener))
     384             :     {
     385           0 :         case json_tokener_continue:
     386             :         case json_tokener_success:
     387           0 :             return nLength;
     388           0 :         default:
     389           0 :             return 0; /* error: interrupt the transfer */
     390             :     }
     391             : }
     392             : 
     393             : #endif  // HAVE_CURL
     394             : /*! @endcond */
     395             : 
     396             : /**
     397             :  * Load json document from web.
     398             :  * @param  osUrl       Url to json document.
     399             :  * @param  papszOptions Option list as a NULL-terminated array of strings. May
     400             :  * be NULL. The available keys are same for CPLHTTPFetch method. Additional key
     401             :  * JSON_DEPTH define json parse depth. Default is 10.
     402             :  * @param  pfnProgress  a function to report progress of the json data loading.
     403             :  * @param  pProgressArg application data passed into progress function.
     404             :  * @return              true on success. If error occurred it can be received
     405             :  * using CPLGetLastErrorMsg method.
     406             :  *
     407             :  */
     408             : 
     409             : #ifdef HAVE_CURL
     410           2 : bool CPLJSONDocument::LoadUrl(const std::string &osUrl,
     411             :                               const char *const *papszOptions,
     412             :                               GDALProgressFunc pfnProgress, void *pProgressArg)
     413             : #else
     414             : bool CPLJSONDocument::LoadUrl(const std::string & /*osUrl*/,
     415             :                               const char *const * /*papszOptions*/,
     416             :                               GDALProgressFunc /*pfnProgress*/,
     417             :                               void * /*pProgressArg*/)
     418             : #endif  // HAVE_CURL
     419             : {
     420             : #ifdef HAVE_CURL
     421             :     int nDepth =
     422           2 :         atoi(CSLFetchNameValueDef(papszOptions, "JSON_DEPTH",
     423             :                                   "32"));  // Same as JSON_TOKENER_DEFAULT_DEPTH
     424           2 :     JsonContext ctx = {nullptr, json_tokener_new_ex(nDepth)};
     425             : 
     426           2 :     CPLHTTPFetchWriteFunc pWriteFunc = CPLJSONWriteFunction;
     427             :     CPLHTTPResult *psResult =
     428           2 :         CPLHTTPFetchEx(osUrl.c_str(), papszOptions, pfnProgress, pProgressArg,
     429             :                        pWriteFunc, &ctx);
     430             : 
     431           2 :     bool bResult =
     432           2 :         psResult->nStatus == 0 /*CURLE_OK*/ && psResult->pszErrBuf == nullptr;
     433             : 
     434           2 :     CPLHTTPDestroyResult(psResult);
     435             : 
     436             :     enum json_tokener_error jerr;
     437           2 :     if ((jerr = json_tokener_get_error(ctx.pTokener)) != json_tokener_success)
     438             :     {
     439           0 :         CPLError(CE_Failure, CPLE_AppDefined, "JSON error: %s\n",
     440             :                  json_tokener_error_desc(jerr));
     441           0 :         bResult = false;
     442             :     }
     443             :     else
     444             :     {
     445           2 :         if (m_poRootJsonObject)
     446           1 :             json_object_put(TO_JSONOBJ(m_poRootJsonObject));
     447             : 
     448           2 :         m_poRootJsonObject = ctx.pObject;
     449             :     }
     450           2 :     json_tokener_free(ctx.pTokener);
     451             : 
     452           2 :     return bResult;
     453             : #else
     454             :     CPLError(CE_Failure, CPLE_NotSupported,
     455             :              "LoadUrl() not supported in a build without Curl");
     456             :     return false;
     457             : #endif
     458             : }
     459             : 
     460             : //------------------------------------------------------------------------------
     461             : // JSONObject
     462             : //------------------------------------------------------------------------------
     463             : /*! @cond Doxygen_Suppress */
     464      242086 : CPLJSONObject::CPLJSONObject() : m_poJsonObject(json_object_new_object())
     465             : {
     466      242064 : }
     467             : 
     468           1 : CPLJSONObject::CPLJSONObject(std::nullptr_t) : m_poJsonObject(nullptr)
     469             : {
     470           1 : }
     471             : 
     472        4337 : CPLJSONObject::CPLJSONObject(const std::string &osVal)
     473        4337 :     : m_poJsonObject(json_object_new_string(osVal.c_str()))
     474             : {
     475        4337 : }
     476             : 
     477           1 : CPLJSONObject::CPLJSONObject(const char *pszValue)
     478           1 :     : m_poJsonObject(json_object_new_string(pszValue))
     479             : {
     480           1 : }
     481             : 
     482           1 : CPLJSONObject::CPLJSONObject(bool bVal)
     483           1 :     : m_poJsonObject(json_object_new_boolean(bVal))
     484             : {
     485           1 : }
     486             : 
     487        2969 : CPLJSONObject::CPLJSONObject(int nVal)
     488        2969 :     : m_poJsonObject(json_object_new_int(nVal))
     489             : {
     490        2969 : }
     491             : 
     492           1 : CPLJSONObject::CPLJSONObject(int64_t nVal)
     493           1 :     : m_poJsonObject(json_object_new_int64(nVal))
     494             : {
     495           1 : }
     496             : 
     497           1 : CPLJSONObject::CPLJSONObject(uint64_t nVal)
     498           1 :     : m_poJsonObject(json_object_new_uint64(nVal))
     499             : {
     500           1 : }
     501             : 
     502        1776 : CPLJSONObject::CPLJSONObject(double dfVal)
     503        1776 :     : m_poJsonObject(json_object_new_double(dfVal))
     504             : {
     505        1776 : }
     506             : 
     507         835 : CPLJSONObject::CPLJSONObject(const std::string &osName,
     508         835 :                              const CPLJSONObject &oParent)
     509         835 :     : m_poJsonObject(json_object_get(json_object_new_object())), m_osKey(osName)
     510             : {
     511         835 :     json_object_object_add(TO_JSONOBJ(oParent.m_poJsonObject), osName.c_str(),
     512         835 :                            TO_JSONOBJ(m_poJsonObject));
     513         835 : }
     514             : 
     515     1660170 : CPLJSONObject::CPLJSONObject(const std::string &osName,
     516     1660170 :                              JSONObjectH poJsonObject)
     517     1660170 :     : m_poJsonObject(json_object_get(TO_JSONOBJ(poJsonObject))), m_osKey(osName)
     518             : {
     519     1660170 : }
     520             : 
     521        8064 : CPLJSONObject CPLJSONObject::Clone() const
     522             : {
     523        8064 :     CPLJSONObject oRet;
     524        8064 :     if (IsValid())
     525             :     {
     526       14706 :         CPLJSONDocument oTmpDoc;
     527        7353 :         oTmpDoc.SetRoot(*this);
     528        7353 :         std::string osStr = oTmpDoc.SaveAsString();
     529        7353 :         CPL_IGNORE_RET_VAL(oTmpDoc.LoadMemory(osStr));
     530        7353 :         oRet = oTmpDoc.GetRoot();
     531             :     }
     532        8064 :     return oRet;
     533             : }
     534             : 
     535     3531940 : CPLJSONObject::~CPLJSONObject()
     536             : {
     537             :     // Should delete m_poJsonObject only if CPLJSONObject has no parent
     538     3531940 :     if (m_poJsonObject)
     539             :     {
     540     2918970 :         json_object_put(TO_JSONOBJ(m_poJsonObject));
     541     2918970 :         m_poJsonObject = nullptr;
     542             :     }
     543     3531940 : }
     544             : 
     545     1233640 : CPLJSONObject::CPLJSONObject(const CPLJSONObject &other)
     546     1233640 :     : m_poJsonObject(json_object_get(TO_JSONOBJ(other.m_poJsonObject))),
     547     1233640 :       m_osKey(other.m_osKey), m_osKeyForSet(other.m_osKeyForSet)
     548             : {
     549     1233640 : }
     550             : 
     551      386117 : CPLJSONObject::CPLJSONObject(CPLJSONObject &&other)
     552      386117 :     : m_poJsonObject(other.m_poJsonObject), m_osKey(std::move(other.m_osKey)),
     553      386117 :       m_osKeyForSet(std::move(other.m_osKeyForSet))
     554             : {
     555      386117 :     other.m_poJsonObject = nullptr;
     556      386117 : }
     557             : 
     558      122038 : CPLJSONObject &CPLJSONObject::operator=(const CPLJSONObject &other)
     559             : {
     560      122038 :     if (this == &other)
     561           1 :         return *this;
     562             : 
     563      122037 :     if (!m_osKeyForSet.empty())
     564             :     {
     565         336 :         std::string osKeyForSet = m_osKeyForSet;
     566         168 :         m_osKeyForSet.clear();
     567         168 :         Set(osKeyForSet, other);
     568             :     }
     569             :     else
     570             :     {
     571      121869 :         m_osKey = other.m_osKey;
     572      121869 :         if (m_poJsonObject)
     573      120764 :             json_object_put(TO_JSONOBJ(m_poJsonObject));
     574      121869 :         m_poJsonObject = json_object_get(TO_JSONOBJ(other.m_poJsonObject));
     575             :     }
     576      122037 :     return *this;
     577             : }
     578             : 
     579       36957 : CPLJSONObject &CPLJSONObject::operator=(CPLJSONObject &&other)
     580             : {
     581       36957 :     if (this == &other)
     582           0 :         return *this;
     583             : 
     584       36957 :     if (!m_osKeyForSet.empty())
     585             :     {
     586         937 :         if (other.m_poJsonObject)
     587             :         {
     588         937 :             json_object_object_add(TO_JSONOBJ(GetInternalHandle()),
     589             :                                    m_osKeyForSet.c_str(),
     590         937 :                                    TO_JSONOBJ(other.m_poJsonObject));
     591         937 :             other.m_poJsonObject = nullptr;
     592             :         }
     593         937 :         other.m_osKey = INVALID_OBJ_KEY;
     594         937 :         m_osKeyForSet.clear();
     595             :     }
     596             :     else
     597             :     {
     598       36020 :         m_osKey = std::move(other.m_osKey);
     599       36020 :         if (m_poJsonObject)
     600       30908 :             json_object_put(TO_JSONOBJ(m_poJsonObject));
     601       36020 :         m_poJsonObject = other.m_poJsonObject;
     602       36020 :         other.m_poJsonObject = nullptr;
     603             :     }
     604       36957 :     return *this;
     605             : }
     606             : 
     607         459 : CPLJSONObject &CPLJSONObject::operator=(CPLJSONArray &&other)
     608             : {
     609         459 :     return operator=(static_cast<CPLJSONObject &&>(other));
     610             : }
     611             : 
     612             : /*! @endcond */
     613             : 
     614             : /**
     615             :  * Add new key - value pair to json object.
     616             :  * @param osName Key name.
     617             :  * @param osValue String value.
     618             :  *
     619             :  */
     620       29228 : void CPLJSONObject::Add(const std::string &osName, const std::string &osValue)
     621             : {
     622       58456 :     std::string objectName;
     623       29228 :     if (m_osKey == INVALID_OBJ_KEY)
     624           0 :         m_osKey.clear();
     625       58456 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     626       29228 :     if (object.IsValid() && json_object_get_type(TO_JSONOBJ(
     627             :                                 object.m_poJsonObject)) == json_type_object)
     628             :     {
     629       29228 :         json_object *poVal = json_object_new_string(osValue.c_str());
     630       29228 :         json_object_object_add(TO_JSONOBJ(object.GetInternalHandle()),
     631             :                                objectName.c_str(), poVal);
     632             :     }
     633       29228 : }
     634             : 
     635             : /** Add new key - value pair to json object.
     636             :  *
     637             :  * @param osName Key name.
     638             :  * @param svValue String value.
     639             :  * @since 3.13
     640             :  */
     641          16 : void CPLJSONObject::Add(const std::string &osName, std::string_view svValue)
     642             : {
     643          16 :     std::string objectName;
     644          16 :     if (m_osKey == INVALID_OBJ_KEY)
     645           0 :         m_osKey.clear();
     646          16 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     647          16 :     if (object.IsValid() && json_object_get_type(TO_JSONOBJ(
     648             :                                 object.m_poJsonObject)) == json_type_object)
     649             :     {
     650          16 :         if (svValue.size() > static_cast<size_t>(INT_MAX - 1))
     651             :         {
     652           0 :             CPLError(CE_Failure, CPLE_AppDefined, "Too long string view");
     653           0 :             return;
     654             :         }
     655          16 :         json_object *poVal = json_object_new_string_len(
     656          16 :             svValue.data(), static_cast<int>(svValue.size()));
     657          16 :         json_object_object_add(TO_JSONOBJ(object.GetInternalHandle()),
     658             :                                objectName.c_str(), poVal);
     659             :     }
     660             : }
     661             : 
     662             : /**
     663             :  * Add new key - value pair to json object.
     664             :  * @param osName Key name.
     665             :  * @param pszValue String value.
     666             :  *
     667             :  */
     668       23701 : void CPLJSONObject::Add(const std::string &osName, const char *pszValue)
     669             : {
     670       23701 :     if (nullptr == pszValue)
     671             :     {
     672           2 :         return;
     673             :     }
     674       23699 :     if (m_osKey == INVALID_OBJ_KEY)
     675           0 :         m_osKey.clear();
     676       47398 :     std::string objectName;
     677       47398 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     678       23699 :     if (object.IsValid() && json_object_get_type(TO_JSONOBJ(
     679             :                                 object.m_poJsonObject)) == json_type_object)
     680             :     {
     681       23699 :         json_object *poVal = json_object_new_string(pszValue);
     682       23699 :         json_object_object_add(TO_JSONOBJ(object.GetInternalHandle()),
     683             :                                objectName.c_str(), poVal);
     684             :     }
     685             : }
     686             : 
     687             : // defined in ogr/ogrsf_frmts/geojson/ogrgeojsonwriter.cpp
     688             : CPL_C_START
     689             : /* %.XXXg formatting */
     690             : json_object CPL_DLL *
     691             : json_object_new_double_with_significant_figures(double dfVal,
     692             :                                                 int nSignificantFigures);
     693             : CPL_C_END
     694             : 
     695             : /**
     696             :  * Add new key - value pair to json object.
     697             :  * @param osName  Key name.
     698             :  * @param dfValue Double value.
     699             :  *
     700             :  */
     701       10383 : void CPLJSONObject::Add(const std::string &osName, double dfValue)
     702             : {
     703       20766 :     std::string objectName;
     704       10383 :     if (m_osKey == INVALID_OBJ_KEY)
     705           0 :         m_osKey.clear();
     706       20766 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     707       10383 :     if (object.IsValid() && json_object_get_type(TO_JSONOBJ(
     708             :                                 object.m_poJsonObject)) == json_type_object)
     709             :     {
     710             :         json_object *poVal =
     711       10383 :             json_object_new_double_with_significant_figures(dfValue, -1);
     712       10383 :         json_object_object_add(TO_JSONOBJ(object.GetInternalHandle()),
     713             :                                objectName.c_str(), poVal);
     714             :     }
     715       10383 : }
     716             : 
     717             : /**
     718             :  * Add new key - value pair to json object.
     719             :  * @param osName  Key name.
     720             :  * @param nValue Integer value.
     721             :  *
     722             :  */
     723       14734 : void CPLJSONObject::Add(const std::string &osName, int nValue)
     724             : {
     725       29468 :     std::string objectName;
     726       14734 :     if (m_osKey == INVALID_OBJ_KEY)
     727           0 :         m_osKey.clear();
     728       29468 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     729       14734 :     if (object.IsValid() && json_object_get_type(TO_JSONOBJ(
     730             :                                 object.m_poJsonObject)) == json_type_object)
     731             :     {
     732       14734 :         json_object *poVal = json_object_new_int(nValue);
     733       14734 :         json_object_object_add(TO_JSONOBJ(object.GetInternalHandle()),
     734             :                                objectName.c_str(), poVal);
     735             :     }
     736       14734 : }
     737             : 
     738             : /**
     739             :  * Add new key - value pair to json object.
     740             :  * @param osName  Key name.
     741             :  * @param nValue Long value.
     742             :  *
     743             :  */
     744        5778 : void CPLJSONObject::Add(const std::string &osName, GInt64 nValue)
     745             : {
     746       11556 :     std::string objectName;
     747        5778 :     if (m_osKey == INVALID_OBJ_KEY)
     748           0 :         m_osKey.clear();
     749       11556 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     750        5778 :     if (object.IsValid() && json_object_get_type(TO_JSONOBJ(
     751             :                                 object.m_poJsonObject)) == json_type_object)
     752             :     {
     753             :         json_object *poVal =
     754        5778 :             json_object_new_int64(static_cast<int64_t>(nValue));
     755        5778 :         json_object_object_add(TO_JSONOBJ(object.GetInternalHandle()),
     756             :                                objectName.c_str(), poVal);
     757             :     }
     758        5778 : }
     759             : 
     760             : /**
     761             :  * Add new key - value pair to json object.
     762             :  * @param osName  Key name.
     763             :  * @param nValue uint64_t value.
     764             :  *
     765             :  * @since GDAL 3.8
     766             :  */
     767         840 : void CPLJSONObject::Add(const std::string &osName, uint64_t nValue)
     768             : {
     769        1680 :     std::string objectName;
     770         840 :     if (m_osKey == INVALID_OBJ_KEY)
     771           0 :         m_osKey.clear();
     772        1680 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     773         840 :     if (object.IsValid() && json_object_get_type(TO_JSONOBJ(
     774             :                                 object.m_poJsonObject)) == json_type_object)
     775             :     {
     776         840 :         json_object *poVal = json_object_new_uint64(nValue);
     777         840 :         json_object_object_add(TO_JSONOBJ(object.GetInternalHandle()),
     778             :                                objectName.c_str(), poVal);
     779             :     }
     780         840 : }
     781             : 
     782             : /**
     783             :  * Add new key - value pair to json object.
     784             :  * @param osName  Key name.
     785             :  * @param oValue   Array value.
     786             :  *
     787             :  */
     788       12260 : void CPLJSONObject::Add(const std::string &osName, const CPLJSONArray &oValue)
     789             : {
     790       24520 :     std::string objectName;
     791       12260 :     if (m_osKey == INVALID_OBJ_KEY)
     792           0 :         m_osKey.clear();
     793       24520 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     794       12260 :     if (object.IsValid() && json_object_get_type(TO_JSONOBJ(
     795             :                                 object.m_poJsonObject)) == json_type_object)
     796             :     {
     797       24520 :         json_object_object_add(
     798       12260 :             TO_JSONOBJ(object.GetInternalHandle()), objectName.c_str(),
     799       12260 :             json_object_get(TO_JSONOBJ(oValue.GetInternalHandle())));
     800             :     }
     801       12260 : }
     802             : 
     803             : /**
     804             :  * Add new key - value pair to json object.
     805             :  * @param osName  Key name.
     806             :  * @param oValue   Json object value.
     807             :  *
     808             :  */
     809       12203 : void CPLJSONObject::Add(const std::string &osName, const CPLJSONObject &oValue)
     810             : {
     811       12203 :     std::string objectName;
     812       12203 :     if (m_osKey == INVALID_OBJ_KEY)
     813           0 :         m_osKey.clear();
     814       12203 :     if (osName.empty())
     815             :     {
     816          40 :         json_object_object_add(
     817          20 :             TO_JSONOBJ(GetInternalHandle()), "",
     818          20 :             json_object_get(TO_JSONOBJ(oValue.GetInternalHandle())));
     819          20 :         return;
     820             :     }
     821       24366 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     822       12183 :     if (object.IsValid() && json_object_get_type(TO_JSONOBJ(
     823             :                                 object.m_poJsonObject)) == json_type_object)
     824             :     {
     825       24366 :         json_object_object_add(
     826       12183 :             TO_JSONOBJ(object.GetInternalHandle()), objectName.c_str(),
     827       12183 :             json_object_get(TO_JSONOBJ(oValue.GetInternalHandle())));
     828             :     }
     829             : }
     830             : 
     831             : /**
     832             :  * Add new key - value pair to json object.
     833             :  * @param osName  Key name (do not split it on '/')
     834             :  * @param oValue   Json object value.
     835             :  *
     836             :  * @since GDAL 3.2
     837             :  */
     838       13902 : void CPLJSONObject::AddNoSplitName(const std::string &osName,
     839             :                                    const CPLJSONObject &oValue)
     840             : {
     841       13902 :     if (m_osKey == INVALID_OBJ_KEY)
     842           0 :         m_osKey.clear();
     843       27804 :     if (IsValid() &&
     844       13902 :         json_object_get_type(TO_JSONOBJ(m_poJsonObject)) == json_type_object)
     845             :     {
     846       27804 :         json_object_object_add(
     847       13902 :             TO_JSONOBJ(GetInternalHandle()), osName.c_str(),
     848       13902 :             json_object_get(TO_JSONOBJ(oValue.GetInternalHandle())));
     849             :     }
     850       13902 : }
     851             : 
     852             : /**
     853             :  * Add new key - value pair to json object.
     854             :  * @param osName  Key name.
     855             :  * @param bValue   Boolean value.
     856             :  *
     857             :  */
     858       12660 : void CPLJSONObject::Add(const std::string &osName, bool bValue)
     859             : {
     860       25320 :     std::string objectName;
     861       12660 :     if (m_osKey == INVALID_OBJ_KEY)
     862           0 :         m_osKey.clear();
     863       25320 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     864       12660 :     if (object.IsValid() && json_object_get_type(TO_JSONOBJ(
     865             :                                 object.m_poJsonObject)) == json_type_object)
     866             :     {
     867       12660 :         json_object *poVal = json_object_new_boolean(bValue);
     868       12660 :         json_object_object_add(TO_JSONOBJ(object.GetInternalHandle()),
     869             :                                objectName.c_str(), poVal);
     870             :     }
     871       12660 : }
     872             : 
     873             : /**
     874             :  * Add new key - null pair to json object.
     875             :  * @param osName  Key name.
     876             :  *
     877             :  */
     878       12086 : void CPLJSONObject::AddNull(const std::string &osName)
     879             : {
     880       24172 :     std::string objectName;
     881       12086 :     if (m_osKey == INVALID_OBJ_KEY)
     882           0 :         m_osKey.clear();
     883       24172 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     884       12086 :     if (object.IsValid() && json_object_get_type(TO_JSONOBJ(
     885             :                                 object.m_poJsonObject)) == json_type_object)
     886             :     {
     887       12086 :         json_object_object_add(TO_JSONOBJ(object.GetInternalHandle()),
     888             :                                objectName.c_str(), nullptr);
     889             :     }
     890       12086 : }
     891             : 
     892             : /**
     893             :  * Change value by key.
     894             :  * @param osName  Key name.
     895             :  *
     896             :  */
     897          18 : void CPLJSONObject::SetNull(const std::string &osName)
     898             : {
     899          18 :     Delete(osName);
     900          18 :     AddNull(osName);
     901          18 : }
     902             : 
     903             : /**
     904             :  * Get value by key.
     905             :  * @param  osName Key name.
     906             :  * @return         Json array object.
     907             :  *
     908             :  */
     909      138266 : CPLJSONArray CPLJSONObject::GetArray(const std::string &osName) const
     910             : {
     911      276532 :     std::string objectName;
     912      276532 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     913      138266 :     if (object.IsValid())
     914             :     {
     915      138266 :         json_object *poVal = nullptr;
     916      138266 :         if (json_object_object_get_ex(TO_JSONOBJ(object.GetInternalHandle()),
     917      138266 :                                       objectName.c_str(), &poVal))
     918             :         {
     919       76562 :             if (poVal && json_object_get_type(poVal) == json_type_array)
     920             :             {
     921       76519 :                 return CPLJSONArray(objectName, poVal);
     922             :             }
     923             :         }
     924             :     }
     925       61747 :     return CPLJSONArray(INVALID_OBJ_KEY, nullptr);
     926             : }
     927             : 
     928             : /**
     929             :  * Get value by key.
     930             :  * @param  osName Key name.
     931             :  * @return         Json object.
     932             :  *
     933             :  */
     934      845909 : CPLJSONObject CPLJSONObject::GetObj(const std::string &osName) const
     935             : {
     936     1691820 :     std::string objectName;
     937     1691820 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
     938      845909 :     if (object.IsValid())
     939             :     {
     940      844794 :         json_object *poVal = nullptr;
     941      844794 :         if (json_object_object_get_ex(TO_JSONOBJ(object.GetInternalHandle()),
     942      844794 :                                       objectName.c_str(), &poVal))
     943             :         {
     944      773005 :             return CPLJSONObject(objectName, poVal);
     945             :         }
     946             :     }
     947       72904 :     return CPLJSONObject(INVALID_OBJ_KEY, nullptr);
     948             : }
     949             : 
     950             : /**
     951             :  * Get value by key (without splitting on /).
     952             :  * @param  osName Key name.
     953             :  * @return         Json object.
     954             :  * @since 3.13
     955             :  *
     956             :  */
     957       10309 : CPLJSONObject CPLJSONObject::GetObjNoSplitName(const std::string &osName) const
     958             : {
     959       10309 :     json_object *poVal = nullptr;
     960             : 
     961             :     // Typically for keys that contain / character
     962       10309 :     if (json_object_object_get_ex(TO_JSONOBJ(GetInternalHandle()),
     963       10309 :                                   osName.c_str(), &poVal))
     964             :     {
     965          10 :         return CPLJSONObject(osName, poVal);
     966             :     }
     967       10299 :     return CPLJSONObject(INVALID_OBJ_KEY, nullptr);
     968             : }
     969             : 
     970             : /**
     971             :  * Get value by key.
     972             :  * @param  osName Key name.
     973             :  * @return         Json object.
     974             :  *
     975             :  */
     976      118784 : CPLJSONObject CPLJSONObject::operator[](const std::string &osName) const
     977             : {
     978      118784 :     return GetObj(osName);
     979             : }
     980             : 
     981             : /** Change value by key.
     982             :  *
     983             :  * e.g.: ``oObj["type"] = "MyType"``
     984             :  *
     985             :  * @since 3.12
     986             :  */
     987       28829 : CPLJSONObject CPLJSONObject::operator[](const std::string &osName)
     988             : {
     989       57658 :     auto oObj = GetObj(osName);
     990       28829 :     if (oObj.IsValid())
     991        6642 :         return oObj;
     992       44374 :     CPLJSONObject oClone(*this);
     993       22187 :     oClone.m_osKey = INVALID_OBJ_KEY;
     994       22187 :     oClone.m_osKeyForSet = osName;
     995       22187 :     return oClone;
     996             : }
     997             : 
     998             : /**
     999             :  * Delete json object by key.
    1000             :  * @param  osName Key name.
    1001             :  *
    1002             :  */
    1003       14635 : void CPLJSONObject::Delete(const std::string &osName)
    1004             : {
    1005       29270 :     std::string objectName;
    1006       14635 :     if (m_osKey == INVALID_OBJ_KEY)
    1007        2845 :         m_osKey.clear();
    1008       29270 :     CPLJSONObject object = GetObjectByPath(osName, objectName);
    1009       14635 :     if (object.IsValid())
    1010             :     {
    1011       14635 :         json_object_object_del(TO_JSONOBJ(object.GetInternalHandle()),
    1012             :                                objectName.c_str());
    1013             :     }
    1014       14635 : }
    1015             : 
    1016             : /**
    1017             :  * Delete json object by key (without splitting on /)
    1018             :  * @param  osName Key name.
    1019             :  *
    1020             :  * @since GDAL 3.4
    1021             :  */
    1022        1135 : void CPLJSONObject::DeleteNoSplitName(const std::string &osName)
    1023             : {
    1024        1135 :     if (m_osKey == INVALID_OBJ_KEY)
    1025           0 :         m_osKey.clear();
    1026        1135 :     if (m_poJsonObject)
    1027             :     {
    1028        1135 :         json_object_object_del(TO_JSONOBJ(m_poJsonObject), osName.c_str());
    1029             :     }
    1030        1135 : }
    1031             : 
    1032             : /**
    1033             :  * Get value by key.
    1034             :  * @param  osName    Key name.
    1035             :  * @param  osDefault Default value.
    1036             :  * @return            String value.
    1037             :  *
    1038             :  */
    1039      324178 : std::string CPLJSONObject::GetString(const std::string &osName,
    1040             :                                      const std::string &osDefault) const
    1041             : {
    1042      324178 :     if (!m_osKeyForSet.empty())
    1043           0 :         return osDefault;
    1044      648356 :     CPLJSONObject object = GetObj(osName);
    1045      324178 :     return object.ToString(osDefault);
    1046             : }
    1047             : 
    1048             : /**
    1049             :  * Get value.
    1050             :  * @param  osDefault Default value.
    1051             :  * @return            String value.
    1052             :  *
    1053             :  */
    1054      500541 : std::string CPLJSONObject::ToString(const std::string &osDefault) const
    1055             : {
    1056      500541 :     if (!m_osKeyForSet.empty())
    1057         239 :         return osDefault;
    1058      500302 :     if( m_poJsonObject /*&& json_object_get_type( TO_JSONOBJ(m_poJsonObject) ) ==
    1059             :             json_type_string*/ )
    1060             :     {
    1061             :         const char *pszString =
    1062      479485 :             json_object_get_string(TO_JSONOBJ(m_poJsonObject));
    1063      479485 :         if (nullptr != pszString)
    1064             :         {
    1065      479485 :             return pszString;
    1066             :         }
    1067             :     }
    1068       20817 :     return osDefault;
    1069             : }
    1070             : 
    1071             : /**
    1072             :  * Get value by key.
    1073             :  * @param  osName    Key name.
    1074             :  * @param  dfDefault  Default value.
    1075             :  * @return            Double value.
    1076             :  *
    1077             :  */
    1078       61646 : double CPLJSONObject::GetDouble(const std::string &osName,
    1079             :                                 double dfDefault) const
    1080             : {
    1081       61646 :     if (!m_osKeyForSet.empty())
    1082           0 :         return dfDefault;
    1083      123292 :     CPLJSONObject object = GetObj(osName);
    1084       61646 :     return object.ToDouble(dfDefault);
    1085             : }
    1086             : 
    1087             : /**
    1088             :  * Get value
    1089             :  * @param  dfDefault  Default value.
    1090             :  * @return            Double value.
    1091             :  *
    1092             :  */
    1093      193924 : double CPLJSONObject::ToDouble(double dfDefault) const
    1094             : {
    1095      193924 :     if (!m_osKeyForSet.empty())
    1096           0 :         return dfDefault;
    1097      193924 :     if( m_poJsonObject /*&& json_object_get_type( TO_JSONOBJ(m_poJsonObject) ) ==
    1098             :             json_type_double*/ )
    1099      193207 :         return json_object_get_double(TO_JSONOBJ(m_poJsonObject));
    1100         717 :     return dfDefault;
    1101             : }
    1102             : 
    1103             : /**
    1104             :  * Get value by key.
    1105             :  * @param  osName    Key name.
    1106             :  * @param  nDefault   Default value.
    1107             :  * @return            Integer value.
    1108             :  *
    1109             :  */
    1110      246401 : int CPLJSONObject::GetInteger(const std::string &osName, int nDefault) const
    1111             : {
    1112      246401 :     if (!m_osKeyForSet.empty())
    1113           0 :         return nDefault;
    1114      492802 :     CPLJSONObject object = GetObj(osName);
    1115      246401 :     return object.ToInteger(nDefault);
    1116             : }
    1117             : 
    1118             : /**
    1119             :  * Get value.
    1120             :  * @param  nDefault   Default value.
    1121             :  * @return            Integer value.
    1122             :  *
    1123             :  */
    1124      249296 : int CPLJSONObject::ToInteger(int nDefault) const
    1125             : {
    1126      249296 :     if (!m_osKeyForSet.empty())
    1127           0 :         return nDefault;
    1128      249296 :     if( m_poJsonObject /*&& json_object_get_type( TO_JSONOBJ(m_poJsonObject) ) ==
    1129             :             json_type_int*/ )
    1130      248326 :         return json_object_get_int(TO_JSONOBJ(m_poJsonObject));
    1131         970 :     return nDefault;
    1132             : }
    1133             : 
    1134             : /**
    1135             :  * Get value by key.
    1136             :  * @param  osName    Key name.
    1137             :  * @param  nDefault   Default value.
    1138             :  * @return            Long value.
    1139             :  *
    1140             :  */
    1141         126 : GInt64 CPLJSONObject::GetLong(const std::string &osName, GInt64 nDefault) const
    1142             : {
    1143         126 :     if (!m_osKeyForSet.empty())
    1144           0 :         return nDefault;
    1145         252 :     CPLJSONObject object = GetObj(osName);
    1146         126 :     return object.ToLong(nDefault);
    1147             : }
    1148             : 
    1149             : /**
    1150             :  * Get value.
    1151             :  * @param  nDefault   Default value.
    1152             :  * @return            Long value.
    1153             :  *
    1154             :  */
    1155        8796 : GInt64 CPLJSONObject::ToLong(GInt64 nDefault) const
    1156             : {
    1157        8796 :     if (!m_osKeyForSet.empty())
    1158           0 :         return nDefault;
    1159        8796 :     if( m_poJsonObject /*&& json_object_get_type( TO_JSONOBJ(m_poJsonObject) ) ==
    1160             :             json_type_int*/ )
    1161             :         return static_cast<GInt64>(
    1162        8723 :             json_object_get_int64(TO_JSONOBJ(m_poJsonObject)));
    1163          73 :     return nDefault;
    1164             : }
    1165             : 
    1166             : /**
    1167             :  * Get value by key.
    1168             :  * @param  osName    Key name.
    1169             :  * @param  bDefault   Default value.
    1170             :  * @return            Boolean value.
    1171             :  *
    1172             :  */
    1173         957 : bool CPLJSONObject::GetBool(const std::string &osName, bool bDefault) const
    1174             : {
    1175         957 :     if (!m_osKeyForSet.empty())
    1176           0 :         return bDefault;
    1177        1914 :     CPLJSONObject object = GetObj(osName);
    1178         957 :     return object.ToBool(bDefault);
    1179             : }
    1180             : 
    1181             : /**
    1182             :  * \brief Get json object children.
    1183             :  *
    1184             :  * This function is useful when keys is not know and need to
    1185             :  * iterate over json object items and get keys and values.
    1186             :  *
    1187             :  * @return Array of CPLJSONObject class instance.
    1188             :  *
    1189             :  */
    1190       20760 : std::vector<CPLJSONObject> CPLJSONObject::GetChildren() const
    1191             : {
    1192       20760 :     std::vector<CPLJSONObject> aoChildren;
    1193       20760 :     if (!m_osKeyForSet.empty())
    1194           0 :         return aoChildren;
    1195       41520 :     if (nullptr == m_poJsonObject ||
    1196       20760 :         json_object_get_type(TO_JSONOBJ(m_poJsonObject)) != json_type_object)
    1197             :     {
    1198           1 :         return aoChildren;
    1199             :     }
    1200             : 
    1201             :     json_object_iter it;
    1202       20759 :     it.key = nullptr;
    1203       20759 :     it.val = nullptr;
    1204       20759 :     it.entry = nullptr;
    1205             :     // cppcheck-suppress cstyleCast
    1206       87713 :     json_object_object_foreachC(TO_JSONOBJ(m_poJsonObject), it)
    1207             :     {
    1208       66954 :         aoChildren.push_back(CPLJSONObject(it.key, it.val));
    1209             :     }
    1210             : 
    1211       20759 :     return aoChildren;
    1212             : }
    1213             : 
    1214             : /**
    1215             :  * Get value.
    1216             :  * @param  bDefault   Default value.
    1217             :  * @return            Boolean value.
    1218             :  *
    1219             :  */
    1220        1496 : bool CPLJSONObject::ToBool(bool bDefault) const
    1221             : {
    1222        1496 :     if (!m_osKeyForSet.empty())
    1223           0 :         return bDefault;
    1224        1496 :     if( m_poJsonObject /*&& json_object_get_type( TO_JSONOBJ(m_poJsonObject) ) ==
    1225             :             json_type_boolean*/ )
    1226         740 :         return json_object_get_boolean(TO_JSONOBJ(m_poJsonObject)) == 1;
    1227         756 :     return bDefault;
    1228             : }
    1229             : 
    1230             : /**
    1231             :  * Get value.
    1232             :  * @return            Array
    1233             :  *
    1234             :  */
    1235       27300 : CPLJSONArray CPLJSONObject::ToArray() const
    1236             : {
    1237       53404 :     if (m_osKeyForSet.empty() && m_poJsonObject &&
    1238       26104 :         json_object_get_type(TO_JSONOBJ(m_poJsonObject)) == json_type_array)
    1239       26099 :         return CPLJSONArray("", TO_JSONOBJ(m_poJsonObject));
    1240        1201 :     return CPLJSONArray(INVALID_OBJ_KEY, nullptr);
    1241             : }
    1242             : 
    1243             : /**
    1244             :  * Stringify object to json format.
    1245             :  * @param  eFormat Format type,
    1246             :  * @return         A string in JSON format.
    1247             :  *
    1248             :  */
    1249       29194 : std::string CPLJSONObject::Format(PrettyFormat eFormat) const
    1250             : {
    1251       29194 :     if (m_poJsonObject)
    1252             :     {
    1253       29194 :         const char *pszFormatString = nullptr;
    1254       29194 :         switch (eFormat)
    1255             :         {
    1256           1 :             case PrettyFormat::Spaced:
    1257           2 :                 pszFormatString = json_object_to_json_string_ext(
    1258           1 :                     TO_JSONOBJ(m_poJsonObject), JSON_C_TO_STRING_SPACED);
    1259           1 :                 break;
    1260         314 :             case PrettyFormat::Pretty:
    1261         628 :                 pszFormatString = json_object_to_json_string_ext(
    1262         314 :                     TO_JSONOBJ(m_poJsonObject), JSON_C_TO_STRING_PRETTY);
    1263         314 :                 break;
    1264       28879 :             default:
    1265       28879 :                 pszFormatString = json_object_to_json_string_ext(
    1266       28879 :                     TO_JSONOBJ(m_poJsonObject), JSON_C_TO_STRING_PLAIN);
    1267             :         }
    1268       29194 :         if (nullptr != pszFormatString)
    1269             :         {
    1270       29194 :             return pszFormatString;
    1271             :         }
    1272             :     }
    1273           0 :     return "";
    1274             : }
    1275             : 
    1276             : /*! @cond Doxygen_Suppress */
    1277     1132680 : CPLJSONObject CPLJSONObject::GetObjectByPath(const std::string &osPath,
    1278             :                                              std::string &osName) const
    1279             : {
    1280     1132680 :     json_object *poVal = nullptr;
    1281             : 
    1282             :     // Typically for keys that contain / character
    1283     1132680 :     if (json_object_object_get_ex(TO_JSONOBJ(GetInternalHandle()),
    1284     1132680 :                                   osPath.c_str(), &poVal))
    1285             :     {
    1286      850105 :         osName = osPath;
    1287      850105 :         return *this;
    1288             :     }
    1289             : 
    1290             :     CPLStringList pathPortions(
    1291      565144 :         CSLTokenizeString2(osPath.c_str(), JSON_PATH_DELIMITER, 0));
    1292      282572 :     int portionsCount = pathPortions.size();
    1293      282572 :     if (portionsCount > 100)
    1294             :     {
    1295           0 :         CPLError(CE_Failure, CPLE_NotSupported, "Too many components in path");
    1296           0 :         return CPLJSONObject(INVALID_OBJ_KEY, nullptr);
    1297             :     }
    1298      282572 :     if (0 == portionsCount)
    1299           0 :         return CPLJSONObject(INVALID_OBJ_KEY, nullptr);
    1300      565144 :     CPLJSONObject object = *this;
    1301      284741 :     for (int i = 0; i < portionsCount - 1; ++i)
    1302             :     {
    1303             :         // TODO: check array index in path - i.e.
    1304             :         // settings/catalog/root/id:1/name if EQUALN(pathPortions[i+1], "id:",
    1305             :         // 3) -> getArray
    1306        2169 :         if (json_object_object_get_ex(TO_JSONOBJ(object.GetInternalHandle()),
    1307        4338 :                                       pathPortions[i], &poVal))
    1308             :         {
    1309        1334 :             object = CPLJSONObject(pathPortions[i], poVal);
    1310             :         }
    1311             :         else
    1312             :         {
    1313         835 :             if (json_object_get_type(TO_JSONOBJ(object.m_poJsonObject)) !=
    1314             :                 json_type_object)
    1315             :             {
    1316           0 :                 return CPLJSONObject(INVALID_OBJ_KEY, nullptr);
    1317             :             }
    1318         835 :             object = CPLJSONObject(pathPortions[i], object);
    1319             :         }
    1320             :     }
    1321             : 
    1322             :     //    // Check if such object already  exists
    1323             :     //    if(json_object_object_get_ex(object.m_jsonObject,
    1324             :     //                                 pathPortions[portionsCount - 1], &poVal))
    1325             :     //        return JSONObject(nullptr);
    1326             :     //
    1327      282572 :     osName = pathPortions[portionsCount - 1];
    1328      282572 :     return object;
    1329             : }
    1330             : 
    1331             : /*! @endcond */
    1332             : 
    1333             : /**
    1334             :  * Get json object type.
    1335             :  * @return Json object type.
    1336             :  *
    1337             :  */
    1338      178179 : CPLJSONObject::Type CPLJSONObject::GetType() const
    1339             : {
    1340      178179 :     if (!m_osKeyForSet.empty())
    1341       13206 :         return CPLJSONObject::Type::Unknown;
    1342             : 
    1343      164973 :     if (nullptr == m_poJsonObject)
    1344             :     {
    1345       11996 :         if (m_osKey == INVALID_OBJ_KEY)
    1346        7195 :             return CPLJSONObject::Type::Unknown;
    1347        4801 :         return CPLJSONObject::Type::Null;
    1348             :     }
    1349      152977 :     auto jsonObj(TO_JSONOBJ(m_poJsonObject));
    1350      152977 :     switch (json_object_get_type(jsonObj))
    1351             :     {
    1352         547 :         case json_type_boolean:
    1353         547 :             return CPLJSONObject::Type::Boolean;
    1354        4048 :         case json_type_double:
    1355        4048 :             return CPLJSONObject::Type::Double;
    1356       43405 :         case json_type_int:
    1357             :         {
    1358       43405 :             if (CPL_INT64_FITS_ON_INT32(json_object_get_int64(jsonObj)))
    1359       43206 :                 return CPLJSONObject::Type::Integer;
    1360             :             else
    1361         199 :                 return CPLJSONObject::Type::Long;
    1362             :         }
    1363       26504 :         case json_type_object:
    1364       26504 :             return CPLJSONObject::Type::Object;
    1365       19888 :         case json_type_array:
    1366       19888 :             return CPLJSONObject::Type::Array;
    1367       58585 :         case json_type_string:
    1368       58585 :             return CPLJSONObject::Type::String;
    1369           0 :         default:
    1370           0 :             break;
    1371             :     }
    1372           0 :     return CPLJSONObject::Type::Unknown;
    1373             : }
    1374             : 
    1375             : /**
    1376             :  * Check if json object valid.
    1377             :  * @return true if json object valid.
    1378             :  *
    1379             :  */
    1380     1399860 : bool CPLJSONObject::IsValid() const
    1381             : {
    1382     1399860 :     return m_osKeyForSet.empty() && m_osKey != INVALID_OBJ_KEY;
    1383             : }
    1384             : 
    1385             : /**
    1386             :  * Decrement reference counter and make pointer NULL.
    1387             :  * A json object will become invalid.
    1388             :  *
    1389             :  */
    1390       45308 : void CPLJSONObject::Deinit()
    1391             : {
    1392       45308 :     if (m_poJsonObject)
    1393             :     {
    1394       43047 :         json_object_put(TO_JSONOBJ(m_poJsonObject));
    1395       43012 :         m_poJsonObject = nullptr;
    1396             :     }
    1397       45273 :     m_osKey = INVALID_OBJ_KEY;
    1398       45286 : }
    1399             : 
    1400             : //------------------------------------------------------------------------------
    1401             : // JSONArray
    1402             : //------------------------------------------------------------------------------
    1403             : /*! @cond Doxygen_Suppress */
    1404       49823 : CPLJSONArray::CPLJSONArray()
    1405             : {
    1406       49823 :     json_object_put(TO_JSONOBJ(m_poJsonObject));
    1407       49823 :     m_poJsonObject = json_object_new_array();
    1408       49823 : }
    1409             : 
    1410           1 : CPLJSONArray::CPLJSONArray(const std::string &osName)
    1411           1 :     : CPLJSONObject(osName, json_object_new_array())
    1412             : {
    1413           1 :     json_object_put(TO_JSONOBJ(m_poJsonObject));
    1414           1 : }
    1415             : 
    1416      168613 : CPLJSONArray::CPLJSONArray(const std::string &osName, JSONObjectH poJsonObject)
    1417      168613 :     : CPLJSONObject(osName, poJsonObject)
    1418             : {
    1419      168613 : }
    1420             : 
    1421          12 : CPLJSONArray::CPLJSONArray(const CPLJSONObject &other) : CPLJSONObject(other)
    1422             : {
    1423          12 : }
    1424             : 
    1425             : /*! @endcond */
    1426             : 
    1427             : /**
    1428             :  * Get array size.
    1429             :  * @return Array size.
    1430             :  *
    1431             :  */
    1432      408958 : int CPLJSONArray::Size() const
    1433             : {
    1434      408958 :     if (m_poJsonObject)
    1435             :         return static_cast<int>(
    1436      406431 :             json_object_array_length(TO_JSONOBJ(m_poJsonObject)));
    1437        2527 :     return 0;
    1438             : }
    1439             : 
    1440             : /**
    1441             :  * Add null object to array.
    1442             :  *
    1443             :  * @since GDAL 3.8
    1444             :  */
    1445        2459 : void CPLJSONArray::AddNull()
    1446             : {
    1447        2459 :     if (m_poJsonObject)
    1448        2459 :         json_object_array_add(TO_JSONOBJ(m_poJsonObject), nullptr);
    1449        2459 : }
    1450             : 
    1451             : /**
    1452             :  * Add json object to array.
    1453             :  * @param oValue Json array.
    1454             :  *
    1455             :  */
    1456       12777 : void CPLJSONArray::Add(const CPLJSONObject &oValue)
    1457             : {
    1458       12777 :     if (m_poJsonObject && oValue.m_poJsonObject)
    1459       25554 :         json_object_array_add(
    1460       12777 :             TO_JSONOBJ(m_poJsonObject),
    1461       12777 :             json_object_get(TO_JSONOBJ(oValue.m_poJsonObject)));
    1462       12777 : }
    1463             : 
    1464             : /**
    1465             :  * Add value to array
    1466             :  * @param osValue Value to add.
    1467             :  *
    1468             :  */
    1469       14415 : void CPLJSONArray::Add(const std::string &osValue)
    1470             : {
    1471       14415 :     if (m_poJsonObject)
    1472       14415 :         json_object_array_add(TO_JSONOBJ(m_poJsonObject),
    1473             :                               json_object_new_string(osValue.c_str()));
    1474       14415 : }
    1475             : 
    1476             : /**
    1477             :  * Add value to array
    1478             :  * @param svValue Value to add.
    1479             :  * @since 3.13
    1480             :  *
    1481             :  */
    1482          17 : void CPLJSONArray::Add(std::string_view svValue)
    1483             : {
    1484          17 :     if (svValue.size() > static_cast<size_t>(INT_MAX - 1))
    1485             :     {
    1486           0 :         CPLError(CE_Failure, CPLE_AppDefined, "Too long string view");
    1487           0 :         return;
    1488             :     }
    1489          17 :     if (m_poJsonObject)
    1490             :     {
    1491          17 :         json_object_array_add(
    1492          17 :             TO_JSONOBJ(m_poJsonObject),
    1493             :             json_object_new_string_len(svValue.data(),
    1494          17 :                                        static_cast<int>(svValue.size())));
    1495             :     }
    1496             : }
    1497             : 
    1498             : /**
    1499             :  * Add value to array
    1500             :  * @param pszValue Value to add.
    1501             :  *
    1502             :  */
    1503        6680 : void CPLJSONArray::Add(const char *pszValue)
    1504             : {
    1505        6680 :     if (nullptr == pszValue)
    1506           0 :         return;
    1507        6680 :     if (m_poJsonObject)
    1508        6680 :         json_object_array_add(TO_JSONOBJ(m_poJsonObject),
    1509             :                               json_object_new_string(pszValue));
    1510             : }
    1511             : 
    1512             : /**
    1513             :  * Add value to array
    1514             :  * @param dfValue Value to add.
    1515             :  *
    1516             :  */
    1517        4627 : void CPLJSONArray::Add(double dfValue)
    1518             : {
    1519        4627 :     if (m_poJsonObject)
    1520        4627 :         json_object_array_add(TO_JSONOBJ(m_poJsonObject),
    1521             :                               json_object_new_double(dfValue));
    1522        4627 : }
    1523             : 
    1524             : /**
    1525             :  * Add value to array
    1526             :  * @param nValue Value to add.
    1527             :  *
    1528             :  */
    1529         759 : void CPLJSONArray::Add(int nValue)
    1530             : {
    1531         759 :     if (m_poJsonObject)
    1532         759 :         json_object_array_add(TO_JSONOBJ(m_poJsonObject),
    1533             :                               json_object_new_int(nValue));
    1534         759 : }
    1535             : 
    1536             : /**
    1537             :  * Add value to array
    1538             :  * @param nValue Value to add.
    1539             :  *
    1540             :  */
    1541        1965 : void CPLJSONArray::Add(GInt64 nValue)
    1542             : {
    1543        1965 :     if (m_poJsonObject)
    1544        1965 :         json_object_array_add(TO_JSONOBJ(m_poJsonObject),
    1545             :                               json_object_new_int64(nValue));
    1546        1965 : }
    1547             : 
    1548             : /**
    1549             :  * Add value to array
    1550             :  * @param nValue Value to add.
    1551             :  *
    1552             :  * @since GDAL 3.8
    1553             :  */
    1554          91 : void CPLJSONArray::Add(uint64_t nValue)
    1555             : {
    1556          91 :     if (m_poJsonObject)
    1557             :     {
    1558          91 :         json_object_array_add(TO_JSONOBJ(m_poJsonObject),
    1559             :                               json_object_new_uint64(nValue));
    1560             :     }
    1561          91 : }
    1562             : 
    1563             : /**
    1564             :  * Add value to array
    1565             :  * @param bValue Value to add.
    1566             :  *
    1567             :  */
    1568          38 : void CPLJSONArray::Add(bool bValue)
    1569             : {
    1570          38 :     if (m_poJsonObject)
    1571          38 :         json_object_array_add(TO_JSONOBJ(m_poJsonObject),
    1572             :                               json_object_new_boolean(bValue));
    1573          38 : }
    1574             : 
    1575             : /**
    1576             :  * Get array item by index.
    1577             :  * @param  nIndex Item index.
    1578             :  * @return        Json object.
    1579             :  *
    1580             :  */
    1581        3364 : CPLJSONObject CPLJSONArray::operator[](int nIndex)
    1582             : {
    1583             :     return CPLJSONObject(
    1584             :         CPLSPrintf("id:%d", nIndex),
    1585        3364 :         json_object_array_get_idx(TO_JSONOBJ(m_poJsonObject), nIndex));
    1586             : }
    1587             : 
    1588             : /**
    1589             :  * Get array const item by index.
    1590             :  * @param  nIndex Item index.
    1591             :  * @return        Json object.
    1592             :  *
    1593             :  */
    1594      538066 : const CPLJSONObject CPLJSONArray::operator[](int nIndex) const
    1595             : {
    1596             :     return CPLJSONObject(
    1597             :         CPLSPrintf("id:%d", nIndex),
    1598      538066 :         json_object_array_get_idx(TO_JSONOBJ(m_poJsonObject), nIndex));
    1599             : }
    1600             : 
    1601             : /**
    1602             :  * Get array item by index.
    1603             :  * @param  nIndex Item index.
    1604             :  * @return        Json object.
    1605             :  *
    1606             :  */
    1607           0 : CPLJSONObject CPLJSONArray::operator[](size_t nIndex)
    1608             : {
    1609             :     return CPLJSONObject(CPLSPrintf("id:%d", static_cast<int>(nIndex)),
    1610           0 :                          json_object_array_get_idx(TO_JSONOBJ(m_poJsonObject),
    1611           0 :                                                    static_cast<int>(nIndex)));
    1612             : }
    1613             : 
    1614             : /**
    1615             :  * Get array const item by index.
    1616             :  * @param  nIndex Item index.
    1617             :  * @return        Json object.
    1618             :  *
    1619             :  */
    1620          34 : const CPLJSONObject CPLJSONArray::operator[](size_t nIndex) const
    1621             : {
    1622             :     return CPLJSONObject(CPLSPrintf("id:%d", static_cast<int>(nIndex)),
    1623          34 :                          json_object_array_get_idx(TO_JSONOBJ(m_poJsonObject),
    1624          34 :                                                    static_cast<int>(nIndex)));
    1625             : }
    1626             : 
    1627             : /************************************************************************/
    1628             : /*                        CPLParseKeyValueJson()                        */
    1629             : /************************************************************************/
    1630             : 
    1631             : /** Return a string list of key/value pairs extracted from a JSON doc.
    1632             : 
    1633             :     We are expecting simple documents with key:value pairs, like the
    1634             :     following with no hierarchy or complex structure.
    1635             :     \verbatim
    1636             :     {
    1637             :       "Code" : "Success",
    1638             :       "LastUpdated" : "2017-07-03T16:20:17Z",
    1639             :       "Type" : "AWS-HMAC",
    1640             :       "AccessKeyId" : "bla",
    1641             :       "SecretAccessKey" : "bla",
    1642             :       "Token" : "bla",
    1643             :       "Expiration" : "2017-07-03T22:42:58Z"
    1644             :     }
    1645             :     \endverbatim
    1646             :     @since GDAL 3.7
    1647             :  */
    1648          40 : CPLStringList CPLParseKeyValueJson(const char *pszJson)
    1649             : {
    1650          80 :     CPLJSONDocument oDoc;
    1651          40 :     CPLStringList oNameValue;
    1652          40 :     if (pszJson != nullptr && oDoc.LoadMemory(pszJson))
    1653             :     {
    1654         146 :         for (const auto &obj : oDoc.GetRoot().GetChildren())
    1655             :         {
    1656         106 :             const auto eType = obj.GetType();
    1657         106 :             if (eType == CPLJSONObject::Type::String ||
    1658           0 :                 eType == CPLJSONObject::Type::Integer ||
    1659             :                 eType == CPLJSONObject::Type::Double)
    1660             :             {
    1661         212 :                 oNameValue.SetNameValue(obj.GetName().c_str(),
    1662         318 :                                         obj.ToString().c_str());
    1663             :             }
    1664             :         }
    1665             :     }
    1666          80 :     return oNameValue;
    1667             : }

Generated by: LCOV version 1.14