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=C0ebwj_BfLQRHKDOJQRgRPbYt6ktqtEgM_da3Y9HmB921w@mail.gmail.com
Whole thread Raw
In response to Re: printing JsonbPair values of input JSONB on server side?  (Andrew Gierth <andrew@tao11.riddles.org.uk>)
Responses Re: printing JsonbPair values of input JSONB on server side?
List pgsql-general
My fault on the first line.
You are right. The value type isn't actually numeric.

I changed the problem lines to:

        //problem lines!!! //either elog crashes pg server
        char *buf = pnstrdup(ptr->key.val.string.val, ptr->key.val.string.len);
        elog(NOTICE, "print_kv_pair(): k = %s", buf);  //debug
        if (ptr->value.type != jbvNumeric) {
            ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("value must be numeric")));
        }
        elog(NOTICE, "print_kv_pair(): v = %s", DatumGetCString(DirectFunctionCall1(numeric_out,
                NumericGetDatum(ptr->value.val.numeric))) ); //debug

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

I don't know why the value isn't numeric. I tried:
select print_kv_pair('{"a":1, "b": 2)');
 and
select print_kv_pair('{"a":1.0, "b": 2.0}'::jsonb);

It seems there are other problems in my code, as if the memory `ptr` refers to is invalid.
I suspect I didn't extract the JsonbPair correctly.

=> select print_kv_pair('{"a":1.0, "b": 2.0}'::jsonb);
NOTICE:  print_kv_pair(): ok0
NOTICE:  print_kv_pair(): ok1
NOTICE:  print_kv_pair(): ok2
NOTICE:  print_kv_pair(): ok3, nPairs = 2
NOTICE:  print_kv_pair(): k = �J
ERROR:  value must be numeric




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

 T>         //problem lines!!! //either elog crashes pg server
 T>         char *buf = pnstrdup(ptr->key.val.string.val,
 T>                              ptr-> key.val.string.len);
 T>         elog(NOTICE, "print_kv_pair(): k = %s",
 T> (ptr->key).val.string.val);  //debug

It doesn't help to make a null-terminated copy of the string if you're
then just going to try and print the original.

    elog(NOTICE, "print_kv_pair(): k = %s", buf);

 T>         elog(NOTICE, "print_kv_pair(): v = %s",
 T> DatumGetCString(DirectFunctionCall1(numeric_out,
 T>                 NumericGetDatum(ptr->value.val.numeric))) ); //debug

That should work, _provided_ that value.type == jbvNumeric - did you
consider checking that first?

--
Andrew (irc:RhodiumToad)

pgsql-general by date:

Previous
From: Andrew Gierth
Date:
Subject: Re: printing JsonbPair values of input JSONB on server side?
Next
From: Andrew Gierth
Date:
Subject: Re: printing JsonbPair values of input JSONB on server side?