Re: printing JsonbPair values of input JSONB on server side?

From: T L <tinlyx(at)gmail(dot)com>
To: Michel Pelletier <pelletier(dot)michel(at)gmail(dot)com>
Cc: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>, pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: printing JsonbPair values of input JSONB on server side?
Date: 2019-03-19 20:49:09
Message-ID: CAOb=C0fHdRb0SzG41AHbJXS53hA1MgG7yuV+rO8oAirO2Y744g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(dot)michel(at)gmail(dot)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(at)tao11(dot)riddles(dot)org(dot)uk>
> wrote:
>
>> >>>>> "T" == T L <tinlyx(at)gmail(dot)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)
>>
>>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Karl Denninger 2019-03-19 21:48:47 FreeBSD 12 and Postgres build failures
Previous Message Adrian Klaver 2019-03-19 20:37:54 Re: subscription broken after upgrade to pg11