1 CREATE EXTENSION jsonb_plpython3u CASCADE;
2 NOTICE: installing required extension "plpython3u"
3 -- test jsonb -> python dict
4 CREATE FUNCTION test1(val jsonb) RETURNS int
6 TRANSFORM FOR TYPE jsonb
8 assert isinstance(val, dict)
9 assert(val == {'a': 1, 'c': 'NULL'})
12 SELECT test1('{"a": 1, "c": "NULL"}'::jsonb);
18 -- test jsonb -> python dict
19 -- complex dict with dicts as value
20 CREATE FUNCTION test1complex(val jsonb) RETURNS int
22 TRANSFORM FOR TYPE jsonb
24 assert isinstance(val, dict)
25 assert(val == {"d": {"d": 1}})
28 SELECT test1complex('{"d": {"d": 1}}'::jsonb);
34 -- test jsonb[] -> python dict
35 -- dict with array as value
36 CREATE FUNCTION test1arr(val jsonb) RETURNS int
38 TRANSFORM FOR TYPE jsonb
40 assert isinstance(val, dict)
41 assert(val == {"d": [12, 1]})
44 SELECT test1arr('{"d":[12, 1]}'::jsonb);
50 -- test jsonb[] -> python list
52 CREATE FUNCTION test2arr(val jsonb) RETURNS int
54 TRANSFORM FOR TYPE jsonb
56 assert isinstance(val, list)
57 assert(val == [12, 1])
60 SELECT test2arr('[12, 1]'::jsonb);
66 -- test jsonb[] -> python list
68 CREATE FUNCTION test3arr(val jsonb) RETURNS int
70 TRANSFORM FOR TYPE jsonb
72 assert isinstance(val, list)
73 assert(val == [{"a": 1,"b": 2}, {"c": 3,"d": 4}])
76 SELECT test3arr('[{"a": 1, "b": 2}, {"c": 3,"d": 4}]'::jsonb);
82 -- test jsonb int -> python int
83 CREATE FUNCTION test1int(val jsonb) RETURNS int
85 TRANSFORM FOR TYPE jsonb
90 SELECT test1int('1'::jsonb);
96 -- test jsonb string -> python string
97 CREATE FUNCTION test1string(val jsonb) RETURNS text
99 TRANSFORM FOR TYPE jsonb
104 SELECT test1string('"a"'::jsonb);
110 -- test jsonb null -> python None
111 CREATE FUNCTION test1null(val jsonb) RETURNS int
113 TRANSFORM FOR TYPE jsonb
118 SELECT test1null('null'::jsonb);
124 -- test python -> jsonb
125 CREATE FUNCTION roundtrip(val jsonb) RETURNS jsonb
127 TRANSFORM FOR TYPE jsonb
131 SELECT roundtrip('null'::jsonb);
137 SELECT roundtrip('1'::jsonb);
143 SELECT roundtrip('1234567890.0987654321'::jsonb);
145 -----------------------
146 1234567890.0987654321
149 SELECT roundtrip('-1234567890.0987654321'::jsonb);
151 ------------------------
152 -1234567890.0987654321
155 SELECT roundtrip('true'::jsonb);
161 SELECT roundtrip('"string"'::jsonb);
167 SELECT roundtrip('{"1": null}'::jsonb);
173 SELECT roundtrip('{"1": 1}'::jsonb);
179 SELECT roundtrip('{"1": true}'::jsonb);
185 SELECT roundtrip('{"1": "string"}'::jsonb);
191 SELECT roundtrip('[null]'::jsonb);
197 SELECT roundtrip('[1]'::jsonb);
203 SELECT roundtrip('[true]'::jsonb);
209 SELECT roundtrip('["string"]'::jsonb);
215 SELECT roundtrip('[null, 1]'::jsonb);
221 SELECT roundtrip('[1, true]'::jsonb);
227 SELECT roundtrip('[true, "string"]'::jsonb);
233 SELECT roundtrip('["string", "string2"]'::jsonb);
235 -----------------------
236 ["string", "string2"]
239 -- complex numbers -> jsonb
240 CREATE FUNCTION testComplexNumbers() RETURNS jsonb
242 TRANSFORM FOR TYPE jsonb
247 SELECT testComplexNumbers();
248 ERROR: could not convert value "(1+2j)" to jsonb
249 CONTEXT: while creating return value
250 PL/Python function "testcomplexnumbers"
252 CREATE FUNCTION testRange() RETURNS jsonb
254 TRANSFORM FOR TYPE jsonb
266 CREATE FUNCTION testDecimal() RETURNS jsonb
268 TRANSFORM FOR TYPE jsonb
273 SELECT testDecimal();
280 CREATE FUNCTION testTuple() RETURNS jsonb
282 TRANSFORM FOR TYPE jsonb
284 x = (1, 'String', None)
289 ---------------------
293 -- interesting dict -> jsonb
294 CREATE FUNCTION test_dict1() RETURNS jsonb
296 TRANSFORM FOR TYPE jsonb
298 x = {"a": 1, None: 2, 33: 3}
303 --------------------------
304 {"": 2, "a": 1, "33": 3}