Function preprocess_json

Summary

#include <src/c4/yml/preprocess.hpp>

(1) size_t preprocess_json(csubstr s, substr buf)

(2) template <class CharContainer>
    substr preprocess_json(csubstr json, CharContainer *out)

(3) template <class CharContainer>
    CharContainer preprocess_json(csubstr json)

Function overload

Synopsis

#include <src/c4/yml/preprocess.hpp>

size_t preprocess_json(csubstr s, substr buf)

Description

Write into a given output buffer. This function is safe to call with empty or small buffers; it won't write beyond the end of the buffer.

Returns
the number of characters required for output

Mentioned in

Source

Lines 66-142 in src/c4/yml/preprocess.cpp. Line 69 in src/c4/yml/preprocess.hpp.

size_t preprocess_json(csubstr s, substr buf)
{
    _SubstrWriter _append(buf);
    size_t last = 0; // the index of the last character in s that was copied to buf

    // append everything that was not written yet
    #define _apfromlast() { csubstr _s_ = s.range(last, i); _append(_s_); last += _s_.len; }
    // append element from the buffer
    #define _apelm(c) { _append(c); ++last; }
    #define _adv(nsrc, ndst) { _append.advance(ndst); i += nsrc; last += nsrc; }

    for(size_t i = 0; i < s.len; ++i)
    {
        const char curr = s[i];
        const char next = i+1 < s.len ? s[i+1] : '\0';
        if(curr == ':')  // if a space is missing after a semicolon, add it
        {
            bool insert = false;
            if(next == '"' || next == '\'' || next == '{' || next == '[' || (next >= '0' && next <= '9'))
            {
                insert = true;
            }
            else if(i+1 < s.len)
            {
                csubstr rem = s.sub(i+1);
                if(rem.begins_with("true") || rem.begins_with("false") || rem.begins_with("null"))
                {
                    insert = true;
                }
            }
            if(insert)
            {
                _apfromlast();
                _apelm(curr);
                _append(' ');
            }
        }
        else if((curr == '{' || curr == '[') && next != '\0') // recurse into substructures
        {
            // get the close-character maching the open-character.
            // In ascii: {=123,}=125 and [=91,]=93. So just add 2!
            const char close = static_cast<char>(curr + 2);
            // get the contents inside the brackets
            csubstr ss = s.sub(i).pair_range_nested(curr, close);
            RYML_ASSERT(ss.size() >= 2);
            RYML_ASSERT(ss.ends_with(close));
            ss = ss.offs(1, 1); // skip the open-close bracket characters
            _apfromlast();
            _apelm(curr);
            if(!ss.empty())  // recurse into the substring
            {
                size_t ret = preprocess_json(ss, _append.rem());
                _adv(ss.len, ret);
            }
            _apelm(close);
        }
        else if(curr == '\'' || curr == '"')  // consume quoted strings at once
        {
            csubstr ss = s.sub(i).pair_range_esc(curr, '\\');
            RYML_ASSERT(ss.begins_with(curr) && ss.ends_with(curr));
            i += ss.len;
            _apfromlast();
            --i;
        }
    }

    if(last + 1 < s.len)
    {
        _append(s.sub(last));
    }

    #undef _apfromlast
    #undef _apelm
    #undef _adv

    return _append.pos;
}

Synopsis

#include <src/c4/yml/preprocess.hpp>

template <class CharContainer>
substr preprocess_json(csubstr json, CharContainer *out)

Description

No description yet.

Mentioned in

Source

Lines 74-78 in src/c4/yml/preprocess.hpp.

template<class CharContainer>
substr preprocess_json(csubstr json, CharContainer *out)
{
    return detail::preprocess_into_container<preprocess_json>(json, out);
}

Synopsis

#include <src/c4/yml/preprocess.hpp>

template <class CharContainer>
CharContainer preprocess_json(csubstr json)

Description

No description yet.

Mentioned in

Source

Lines 82-88 in src/c4/yml/preprocess.hpp.

template<class CharContainer>
CharContainer preprocess_json(csubstr json)
{
    CharContainer c;
    preprocess_json(json, &c);
    return c;
}





Add Discussion as Guest

Log in