2 * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 * This simple program expands the object { "$ref": "#/path/to/a/target" }
25 * "b": { "$ref": "#/type/a" }
29 * will be exapanded to
38 * Invocation: program [file|-]...
40 * without arguments, it reads the input.
47 #include <json-c/json.h>
52 * Search for a reference of type "#/a/b/c" int the
53 * parsed JSON root object
55 int search$ref(struct json_object *root, const char *ref, struct json_object **result)
58 struct json_object *i;
60 /* does it match #/ at the beginning? */
61 if (ref[0] != '#' || (ref[0] && ref[1] != '/')) {
62 fprintf(stderr, "$ref invalid. Was: %s", ref);
66 /* search from root to target */
71 if (!json_object_object_get_ex(i, d, &i))
73 d = strtok(NULL, "/");
80 struct json_object *get$ref(struct json_object *root, const char *ref)
82 struct json_object *result;
83 return search$ref(root, ref, &result) ? result : NULL;