Permalink
Browse files

Return NULL on non-existant JSON fields. Fixes #3

  • Loading branch information...
1 parent ac27d4d commit a888cc7d4403d748855f8a6109219a25233fe242 @theirix committed Oct 29, 2012
Showing with 18 additions and 6 deletions.
  1. +1 −1 Makefile
  2. +12 −5 src/json_accessors.c
  3. +3 −0 test/expected/regress.out
  4. +2 −0 test/sql/regress.sql
View
@@ -6,7 +6,7 @@ DATA = $(wildcard sql/*--*.sql) sql/$(EXTENSION)--$(EXTVERSION).sql
DOCS = $(wildcard doc/*.md)
TESTS = $(wildcard test/sql/*.sql)
REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
-REGRESS_OPTS = --inputdir=test
+REGRESS_OPTS += --inputdir=test
PG_CONFIG := pg_config
#PG_CPPFLAGS =
EXTRA_CLEAN = sql/$(EXTENSION)--$(EXTVERSION).sql
View
@@ -189,9 +189,10 @@ ArrayType* construct_typed_array(Datum *elems, int nelems, Oid elmtype)
*/
Datum json_object_get_generic(text *argJson, text *argKey, int json_type, pextract_type_from_json extractor)
{
- Datum result;
+ Datum result = (Datum)0;
char *strJson, *strKey;
cJSON *root, *sel;
+ bool valid = false;
strJson = text_to_cstring(argJson);
strKey = text_to_cstring(argKey);
@@ -211,6 +212,7 @@ Datum json_object_get_generic(text *argJson, text *argKey, int json_type, pextra
errmsg("type extractor refused to parse object type %s",
json_type_str(json_type))));
}
+ valid = true;
}
else
{
@@ -222,10 +224,11 @@ Datum json_object_get_generic(text *argJson, text *argKey, int json_type, pextra
}
else
{
- ereport(ERROR,
+ valid = true;
+ /*ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("cannot extract from \"%s\" json object by the key \"%s\"",
- strJson, strKey)));
+ strJson, strKey)));*/
}
cJSON_Delete(root);
}
@@ -239,7 +242,7 @@ Datum json_object_get_generic(text *argJson, text *argKey, int json_type, pextra
pfree(strJson);
pfree(strKey);
- if (!result)
+ if (!valid)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("json string cannot be parsed")));
@@ -252,7 +255,11 @@ Datum json_object_get_generic(text *argJson, text *argKey, int json_type, pextra
*/
Datum json_object_get_generic_args(PG_FUNCTION_ARGS, int json_type, pextract_type_from_json extractor)
{
- return json_object_get_generic(PG_GETARG_TEXT_P(0), PG_GETARG_TEXT_P(1), json_type, extractor);
+ Datum result = json_object_get_generic(PG_GETARG_TEXT_P(0), PG_GETARG_TEXT_P(1), json_type, extractor);
+ if (result)
+ PG_RETURN_DATUM(result);
+ else
+ PG_RETURN_NULL();
}
/*
@@ -22,6 +22,9 @@ select json_get_bigint('{"baz": 9223372036854, "boo": 42.424242}', 'baz');
-- 42.424242
select json_get_numeric('{"baz": 42, "boo": 42.424242}', 'boo');
42.424242
+-- t
+select json_get_text('{"foo":"qq", "bar": true}', 'noneofthese') is null;
+t
-- Tue Dec 01 01:23:45 2009
select json_get_timestamp('{"foo":"qq", "bar": "2009-12-01 01:23:45"}', 'bar');
Tue Dec 01 01:23:45 2009
View
@@ -18,6 +18,8 @@ select json_get_bigint('{"baz": 42, "boo": 42.424242}', 'baz');
select json_get_bigint('{"baz": 9223372036854, "boo": 42.424242}', 'baz');
-- 42.424242
select json_get_numeric('{"baz": 42, "boo": 42.424242}', 'boo');
+-- t
+select json_get_text('{"foo":"qq", "bar": true}', 'noneofthese') is null;
-- Tue Dec 01 01:23:45 2009
select json_get_timestamp('{"foo":"qq", "bar": "2009-12-01 01:23:45"}', 'bar');

0 comments on commit a888cc7

Please sign in to comment.