Skip to content

Commit

Permalink
Return NULL on non-existant JSON fields. Fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
theirix committed Oct 29, 2012
1 parent ac27d4d commit a888cc7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions src/json_accessors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
{
Expand All @@ -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);
}
Expand All @@ -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")));
Expand All @@ -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();
}

/*
Expand Down
3 changes: 3 additions & 0 deletions test/expected/regress.out
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/sql/regress.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit a888cc7

Please sign in to comment.