Re: printing JsonbPair values of input JSONB on server side? - Mailing list pgsql-general

From T L
Subject Re: printing JsonbPair values of input JSONB on server side?
Date
Msg-id CAOb=C0fHdRb0SzG41AHbJXS53hA1MgG7yuV+rO8oAirO2Y744g@mail.gmail.com
Whole thread Raw
In response to Re: printing JsonbPair values of input JSONB on server side?  (Michel Pelletier <pelletier.michel@gmail.com>)
List pgsql-general
BTW, my pointer math code was trying to mimic the below code I found for "uniqueifyJsonbObject".

I just removed the logic for dropping duplicates. My difficulty is that I couldn't find out how to interface the jsonb object I get from
my "print_kv_pair()" to this function. Just out of curiosity, I am still interested in finding a way to extract
and feed the JsonValue the right way.

static void
uniqueifyJsonbObject(JsonbValue *object)
{
    bool        hasNonUniq = false;

    Assert(object->type == jbvObject);

    if (object->val.object.nPairs > 1)
        qsort_arg(object->val.object.pairs, object->val.object.nPairs, sizeof(JsonbPair),
                  lengthCompareJsonbPair, &hasNonUniq);

    if (hasNonUniq)
    {
        JsonbPair  *ptr = object->val.object.pairs + 1,
                   *res = object->val.object.pairs;

        while (ptr - object->val.object.pairs < object->val.object.nPairs)
        {
            // Avoid copying over duplicate
            if (lengthCompareJsonbStringValue(ptr, res) != 0)
            {
                res++;
                if (ptr != res)
                    memcpy(res, ptr, sizeof(JsonbPair));
            }
            ptr++;
        }

        object->val.object.nPairs = res + 1 - object->val.object.pairs;
    }
}


On Tue, Mar 19, 2019 at 9:50 AM Michel Pelletier <pelletier.michel@gmail.com> wrote:
Yeah I'm not sure why you're looping using pointer math, the iterators are there to provide that service.  Another function to check out 'jsonb_each', other than the set returning function parts, it does what it looks like your are trying to do.

-Michel

On Mon, Mar 18, 2019 at 4:12 PM Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:
>>>>> "T" == T L <tinlyx@gmail.com> writes:

 T> Below is my test. It prints a strange character instead of "a"; and
 T> says that the value isn't numeric.

Yeah, there's plenty else wrong with your code.

Did you look at how JsonbToCStringWorker does it? that looks like the
best example I can find on a quick scan.

--
Andrew (irc:RhodiumToad)

pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: subscription broken after upgrade to pg11
Next
From: Karl Denninger
Date:
Subject: FreeBSD 12 and Postgres build failures