VirtualBox

Ignore:
Timestamp:
Sep 21, 2018 6:23:01 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
125249
Message:

IPRT/rest: Reworked the JSON output classes a little, adding a pretty printing version. bugref:9167

Location:
trunk/src/VBox/Runtime/common/rest
Files:
2 added
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/rest/RTCRestAnyObject.cpp

    r74402 r74414  
    3434#include <iprt/assert.h>
    3535#include <iprt/err.h>
     36#include <iprt/cpp/restoutput.h>
    3637
    3738
     
    419420    if (m_pData)
    420421        return m_pData->serializeAsJson(a_rDst);
    421     a_rDst.printf("null");
     422    a_rDst.nullValue();
    422423    return a_rDst;
    423424}
  • trunk/src/VBox/Runtime/common/rest/RTCRestArrayBase.cpp

    r74402 r74414  
    3434#include <iprt/err.h>
    3535#include <iprt/string.h>
     36#include <iprt/cpp/restoutput.h>
    3637
    3738
     
    117118    if (!m_fNullIndicator)
    118119    {
    119         a_rDst.printf("[\n");
    120         unsigned const uOldIndent = a_rDst.incrementIndent();
    121 
     120        uint32_t const uOldState = a_rDst.beginArray();
    122121        for (size_t i = 0; i < m_cElements; i++)
    123122        {
     123            a_rDst.valueSeparator();
    124124            m_papElements[i]->serializeAsJson(a_rDst);
    125             if (i < m_cElements - 1)
    126                 a_rDst.printf(",\n");
    127             else
    128                 a_rDst.printf("\n");
    129         }
    130 
    131         a_rDst.setIndent(uOldIndent);
    132         a_rDst.printf("]");
     125        }
     126        a_rDst.endArray(uOldState);
    133127    }
    134128    else
    135         a_rDst.printf("null");
     129        a_rDst.nullValue();
    136130    return a_rDst;
    137131}
  • trunk/src/VBox/Runtime/common/rest/RTCRestOutputPrettyToString.cpp

    r74403 r74414  
    11/* $Id$ */
    22/** @file
    3  * IPRT - C++ REST, RTCRestOutputToString implementation.
     3 * IPRT - C++ REST, RTCRestOutputPrettyToString implementation.
    44 */
    55
     
    3030*********************************************************************************************************************************/
    3131#define LOG_GROUP RTLOGGROUP_REST
    32 #include <iprt/cpp/restbase.h>
     32#include <iprt/cpp/restoutput.h>
    3333
    3434#include <iprt/err.h>
     
    3636
    3737
    38 RTCRestOutputToString::RTCRestOutputToString(RTCString *a_pDst, bool a_fAppend /*= false*/)
    39     : m_pDst(a_pDst)
     38RTCRestOutputPrettyToString::RTCRestOutputPrettyToString(RTCString *a_pDst, bool a_fAppend /*= false*/)
     39    : RTCRestOutputPrettyBase()
     40    , m_pDst(a_pDst)
    4041    , m_fOutOfMemory(false)
    4142{
     
    4546
    4647
    47 RTCRestOutputToString::~RTCRestOutputToString()
     48RTCRestOutputPrettyToString::~RTCRestOutputPrettyToString()
    4849{
    4950    /* We don't own the string, so we don't delete it! */
     
    5253
    5354
    54 /**
    55  * @callback_method_impl{FNRTSTROUTPUT}
    56  */
    57 /*static*/ DECLCALLBACK(size_t)
    58 RTCRestOutputToString::strOutput(void *pvArg, const char *pachChars, size_t cbChars)
     55size_t RTCRestOutputPrettyToString::output(const char *a_pchString, size_t a_cchToWrite)
    5956{
    60     RTCRestOutputToString *pThis = (RTCRestOutputToString *)pvArg;
    61     if (cbChars)
     57    if (a_cchToWrite)
    6258    {
    63         RTCString *pDst = pThis->m_pDst;
    64         if (pDst && !pThis->m_fOutOfMemory)
     59        RTCString *pDst = m_pDst;
     60        if (pDst && !m_fOutOfMemory)
    6561        {
    6662            /*
     
    6965            size_t cchCurrent = pDst->length();
    7066            size_t cbCapacity = pDst->capacity();
    71             size_t cbNeeded   = cchCurrent + cbChars + 1;
     67            size_t cbNeeded   = cchCurrent + a_cchToWrite + 1;
    7268            if (cbNeeded <= cbCapacity)
    7369            { /* likely */ }
     
    9692                    if (RT_FAILURE(rc))
    9793                    {
    98                         pThis->m_fOutOfMemory = true;
    99                         return cbChars;
     94                        m_fOutOfMemory = true;
     95                        return a_cchToWrite;
    10096                    }
    10197                }
     
    105101             * Do the appending.
    106102             */
    107             pDst->append(pachChars, cbChars);
     103            pDst->append(a_pchString, a_cchToWrite);
    108104        }
    109105    }
    110     return cbChars;
     106    return a_cchToWrite;
    111107}
    112108
    113109
    114 size_t RTCRestOutputToString::vprintf(const char *pszFormat, va_list va)
    115 {
    116     return RTStrFormatV(strOutput, this, NULL, NULL, pszFormat, va);
    117 }
    118 
    119 
    120 RTCString *RTCRestOutputToString::finalize()
     110RTCString *RTCRestOutputPrettyToString::finalize()
    121111{
    122112    RTCString *pRet;
  • trunk/src/VBox/Runtime/common/rest/RTCRestOutputToString.cpp

    r73977 r74414  
    3030*********************************************************************************************************************************/
    3131#define LOG_GROUP RTLOGGROUP_REST
    32 #include <iprt/cpp/restbase.h>
     32#include <iprt/cpp/restoutput.h>
    3333
    3434#include <iprt/err.h>
     
    3737
    3838RTCRestOutputToString::RTCRestOutputToString(RTCString *a_pDst, bool a_fAppend /*= false*/)
    39     : m_pDst(a_pDst)
     39    : RTCRestOutputBase()
     40    , m_pDst(a_pDst)
    4041    , m_fOutOfMemory(false)
    4142{
     
    5253
    5354
    54 /**
    55  * @callback_method_impl{FNRTSTROUTPUT}
    56  */
    57 /*static*/ DECLCALLBACK(size_t)
    58 RTCRestOutputToString::strOutput(void *pvArg, const char *pachChars, size_t cbChars)
     55size_t RTCRestOutputToString::output(const char *a_pchString, size_t a_cchToWrite)
    5956{
    60     RTCRestOutputToString *pThis = (RTCRestOutputToString *)pvArg;
    61     if (cbChars)
     57    if (a_cchToWrite)
    6258    {
    63         RTCString *pDst = pThis->m_pDst;
    64         if (pDst && !pThis->m_fOutOfMemory)
     59        RTCString *pDst = m_pDst;
     60        if (pDst && !m_fOutOfMemory)
    6561        {
    6662            /*
     
    6965            size_t cchCurrent = pDst->length();
    7066            size_t cbCapacity = pDst->capacity();
    71             size_t cbNeeded   = cchCurrent + cbChars + 1;
     67            size_t cbNeeded   = cchCurrent + a_cchToWrite + 1;
    7268            if (cbNeeded <= cbCapacity)
    7369            { /* likely */ }
     
    9692                    if (RT_FAILURE(rc))
    9793                    {
    98                         pThis->m_fOutOfMemory = true;
    99                         return cbChars;
     94                        m_fOutOfMemory = true;
     95                        return a_cchToWrite;
    10096                    }
    10197                }
     
    105101             * Do the appending.
    106102             */
    107             pDst->append(pachChars, cbChars);
     103            pDst->append(a_pchString, a_cchToWrite);
    108104        }
    109105    }
    110     return cbChars;
    111 }
    112 
    113 
    114 size_t RTCRestOutputToString::vprintf(const char *pszFormat, va_list va)
    115 {
    116     return RTStrFormatV(strOutput, this, NULL, NULL, pszFormat, va);
     106    return a_cchToWrite;
    117107}
    118108
  • trunk/src/VBox/Runtime/common/rest/RTCRestStringMapBase.cpp

    r74402 r74414  
    3434#include <iprt/err.h>
    3535#include <iprt/string.h>
     36#include <iprt/cpp/restoutput.h>
    3637
    3738
     
    101102    if (!m_fNullIndicator)
    102103    {
    103         a_rDst.printf("{\n");
    104         unsigned const uOldIndent = a_rDst.incrementIndent();
    105 
    106         MapEntry const * const pLast = RTListGetLastCpp(&m_ListHead, MapEntry, ListEntry);
    107         MapEntry const * pCur;
     104        uint32_t const uOldState = a_rDst.beginObject();
     105        MapEntry const *pCur;
    108106        RTListForEachCpp(&m_ListHead, pCur, MapEntry, ListEntry)
    109107        {
    110             a_rDst.printf("%RJs: ", pCur->strKey.c_str());
     108            a_rDst.valueSeparatorAndName(pCur->strKey.c_str(), pCur->strKey.length());
    111109            pCur->pValue->serializeAsJson(a_rDst);
    112 
    113             if (pCur != pLast)
    114                 a_rDst.printf(",\n");
    115             else
    116                 a_rDst.printf("\n");
    117110        }
    118 
    119         a_rDst.setIndent(uOldIndent);
    120         a_rDst.printf("}");
     111        a_rDst.endArray(uOldState);
    121112    }
    122113    else
    123         a_rDst.printf("null");
     114        a_rDst.nullValue();
    124115    return a_rDst;
    125116}
  • trunk/src/VBox/Runtime/common/rest/rest-binary.cpp

    r74402 r74414  
    3535#include <iprt/assert.h>
    3636#include <iprt/err.h>
     37#include <iprt/cpp/restoutput.h>
    3738
    3839
     
    209210{
    210211    AssertMsgFailed(("We should never get here!\n"));
    211     a_rDst.printf("null");
     212    a_rDst.nullValue();
    212213    return a_rDst;
    213214}
  • trunk/src/VBox/Runtime/common/rest/rest-primary-object-types.cpp

    r74402 r74414  
    3535#include <iprt/err.h>
    3636#include <iprt/string.h>
     37#include <iprt/cpp/restoutput.h>
    3738
    3839#include <stdio.h>
     
    409410        a_rDst.printf("%RI64", m_iValue);
    410411    else
    411         a_rDst.printf("null");
     412        a_rDst.nullValue();
    412413    return a_rDst;
    413414}
     
    593594        a_rDst.printf("%RI32", m_iValue);
    594595    else
    595         a_rDst.printf("null");
     596        a_rDst.nullValue();
    596597    return a_rDst;
    597598}
     
    783784        a_rDst.printf("%RI16", m_iValue);
    784785    else
    785         a_rDst.printf("null");
     786        a_rDst.nullValue();
    786787    return a_rDst;
    787788}
     
    989990    }
    990991    else
    991         a_rDst.printf("null");
     992        a_rDst.nullValue();
    992993    return a_rDst;
    993994}
     
    12381239        a_rDst.printf("%RMjs", m_psz ? m_psz : "");
    12391240    else
    1240         a_rDst.printf("null");
     1241        a_rDst.nullValue();
    12411242    return a_rDst;
    12421243}
     
    15461547{
    15471548    if (m_fNullIndicator)
    1548         a_rDst.printf("null");
     1549        a_rDst.nullValue();
    15491550    else
    15501551        a_rDst.printf("%RMjs", m_strFormatted.c_str());
     
    19581959        a_rDst.printf("%RMjs", getString());
    19591960    else
    1960         a_rDst.printf("null");
     1961        a_rDst.nullValue();
    19611962    return a_rDst;
    19621963}
     
    22282229
    22292230
    2230 const char *RTCRestDataObject::serializeMembersAsJson(RTCRestOutputBase &a_rDst, const char *a_pszSep) const
     2231RTCRestOutputBase &RTCRestDataObject::serializeMembersAsJson(RTCRestOutputBase &a_rDst) const
    22312232{
    22322233    RT_NOREF(a_rDst);
    2233     return a_pszSep;
     2234    return a_rDst;
    22342235}
    22352236
     
    22392240    if (!m_fNullIndicator)
    22402241    {
    2241         a_rDst.printf("{");
    2242         unsigned const uOldIndent = a_rDst.incrementIndent();
    2243 
    2244         const char *pszSep = serializeMembersAsJson(a_rDst, "");
    2245 
    2246         a_rDst.setIndent(uOldIndent);
    2247         a_rDst.printf("%s}", *pszSep != '\0' ? "\n" : "");
     2242        uint32_t const uOldState = a_rDst.beginObject();
     2243        serializeMembersAsJson(a_rDst);
     2244        a_rDst.endObject(uOldState);
    22482245    }
    22492246    else
    2250         a_rDst.printf("null");
     2247        a_rDst.nullValue();
    22512248    return a_rDst;
    22522249}
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette