Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed assertment for invalid json to error #698

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions bindings/python/tests/input/Invalid3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"uri": "http://onto-ns.com/meta/0.1/Invalid3",
"description": "Invalid JSON (square bracket for dimensions instead of braces).",
"dimensions": [
"nfrequencies": "Number of eigen-frequencies."
],
"properties": {
"name": {
"type": "str",
"description": "Name of the item."
},
"f": {
"type": "float64",
"shape": ["nf"],
"unit": "Hz",
"description": "The magic eigen-frequencies of the item."
}
}
}
8 changes: 7 additions & 1 deletion bindings/python/tests/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@


thisdir = Path(__file__).absolute().parent
dlite.storage_path.append(thisdir / "input" / "*.json")
indir = thisdir / "input"
dlite.storage_path.append(indir / "*.json")

url = f"json://{thisdir}/MyEntity.json"

Expand Down Expand Up @@ -319,3 +320,8 @@
Invalid2 = dlite.get_instance("http://onto-ns.com/meta/0.1/Invalid2")
with raises(dlite.DLiteMissingInstanceError, dlite.DLiteSyntaxError):
invalid2 = Invalid2([2])


# For issue #691
with raises(dlite.DLiteStorageOpenError):
Invalid3 = dlite.Instance.from_location("json", indir / "Invalid3.json")
12 changes: 8 additions & 4 deletions src/utils/jsmnx.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,16 @@ const jsmntok_t *jsmn_item(const char *js, const jsmntok_t *t, const char *key)
{
int i, n, nitems;
int len, keylen=strlen(key);
if (t->type != JSMN_OBJECT) return errx(1, "expected JSMN OBJECT"), NULL;
if (t->type != JSMN_OBJECT)
return errx(1, "expected JSON object in string starting with:\n%.200s\n",
js + t->start), NULL;
nitems = t->size;
for (i=0; i<nitems; i++) {
t++;
assert(t->type == JSMN_STRING);
len = t->end - t->start;
if (t->type != JSMN_STRING)
return errx(1, "invalid JSON, object key must be a string, got '%.*s'",
len, js + t->start), NULL;
if (len == keylen && strncmp(key, js + t->start, len) == 0) return t+1;
t++;
if ((n = jsmn_count(t)) < 0) return NULL;
Expand All @@ -139,9 +143,9 @@ const jsmntok_t *jsmn_item(const char *js, const jsmntok_t *t, const char *key)
const jsmntok_t *jsmn_element(const char *js, const jsmntok_t *t, int i)
{
int j, n;
(void)js; // unused
int len = t->end - t->start;
if (t->type != JSMN_ARRAY)
return errx(1, "expected JSMN ARRAY"), NULL;
return errx(1, "expected JSON array, got '%.*s", len, js + t->start), NULL;
if (i < 0 || i >= t->size)
return errx(1, "element i=%d is out of range [0:%d]", i, t->size-1), NULL;
for (j=0; j<i; j++) {
Expand Down