json-tree.h revision fcd3d6214ce1b8169b6481c78e02d9054901fed3
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef JSON_TREE_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define JSON_TREE_H
e8490a52a1bc71bc53034e68f464435684ad810fTimo Sirainen
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen#include "json-parser.h"
d23c747de9d33966483fbdd41f08ad7766da7c5cTimo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen/* Direct access to this structure is not encouraged, use the inline
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen function accessors where possible, so that the implementation
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen details can remain fluid, or, even better, hidden. */
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainenstruct json_tree_node {
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen /* object key, or NULL if we're in a list */
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen const char *key;
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen struct json_tree_node *parent, *next;
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainen enum json_type value_type;
e8490a52a1bc71bc53034e68f464435684ad810fTimo Sirainen struct {
e8490a52a1bc71bc53034e68f464435684ad810fTimo Sirainen /* for JSON_TYPE_OBJECT and JSON_TYPE_ARRAY */
296dca49e4fe6046e0328c67ef1cf4b8077dec9cTimo Sirainen struct json_tree_node *child;
7fd51f7b0b4d990ec3cfef4e60ee685bf9fb32deTimo Sirainen /* for other types */
7fd51f7b0b4d990ec3cfef4e60ee685bf9fb32deTimo Sirainen const char *str;
7fd51f7b0b4d990ec3cfef4e60ee685bf9fb32deTimo Sirainen } value;
eb64c3586d854cddd693f0b811d897399076a441Timo Sirainen};
eb64c3586d854cddd693f0b811d897399076a441Timo Sirainenstatic inline ATTR_PURE const struct json_tree_node *json_tree_get_child(const struct json_tree_node *node)
eb64c3586d854cddd693f0b811d897399076a441Timo Sirainen{
eb64c3586d854cddd693f0b811d897399076a441Timo Sirainen return node->value.child;
296dca49e4fe6046e0328c67ef1cf4b8077dec9cTimo Sirainen}
eb64c3586d854cddd693f0b811d897399076a441Timo Sirainenstatic inline ATTR_PURE const char *json_tree_get_value_str(const struct json_tree_node *node)
4ac2d38239cea8090154e17faefd77de5a71d882Timo Sirainen{
eb64c3586d854cddd693f0b811d897399076a441Timo Sirainen return node->value.str;
4ac2d38239cea8090154e17faefd77de5a71d882Timo Sirainen}
eb64c3586d854cddd693f0b811d897399076a441Timo Sirainen
32e1554df9abca74fef0af2ba2e4c37e90a06cd0Timo Sirainen/* You can build a list or an object, nothing else. */
eb64c3586d854cddd693f0b811d897399076a441Timo Sirainenstruct json_tree *json_tree_init_type(enum json_type container);
32e1554df9abca74fef0af2ba2e4c37e90a06cd0Timo Sirainenstatic inline struct json_tree *json_tree_init(void)
a988c3fd9251806e38931a011aaa4006dd081cbdTimo Sirainen{
16db188cfddce117500a161302f17ae691b4500eTimo Sirainen return json_tree_init_type(JSON_TYPE_OBJECT);
b337d3b6871b878d6467d7d8ed600433af5da5a1Timo Sirainen}
b337d3b6871b878d6467d7d8ed600433af5da5a1Timo Sirainenstatic inline struct json_tree *json_tree_init_array(void)
16db188cfddce117500a161302f17ae691b4500eTimo Sirainen{
60b42c6dfdf9edcca8a96b380ef9a0adc60c2464Timo Sirainen return json_tree_init_type(JSON_TYPE_ARRAY);
16db188cfddce117500a161302f17ae691b4500eTimo Sirainen}
16db188cfddce117500a161302f17ae691b4500eTimo Sirainen
16db188cfddce117500a161302f17ae691b4500eTimo Sirainenvoid json_tree_deinit(struct json_tree **tree);
16db188cfddce117500a161302f17ae691b4500eTimo Sirainen
16db188cfddce117500a161302f17ae691b4500eTimo Sirainen/* Append data to a tree. The type/value should normally come from json-parser.
296dca49e4fe6046e0328c67ef1cf4b8077dec9cTimo Sirainen Returns 0 on success, -1 if the input was invalid (which should never happen
296dca49e4fe6046e0328c67ef1cf4b8077dec9cTimo Sirainen if it's coming from json-parser). */
e8490a52a1bc71bc53034e68f464435684ad810fTimo Sirainenint json_tree_append(struct json_tree *tree, enum json_type type,
2b95b7a9f4f06e7640ef431d9e6efc2423cacf1aTimo Sirainen const char *value);
e8490a52a1bc71bc53034e68f464435684ad810fTimo Sirainen
6145bd3b17b9135b77b0b42409a0cc3fa0d1b946Timo Sirainen/* Return the root node. */
e8490a52a1bc71bc53034e68f464435684ad810fTimo Sirainenconst struct json_tree_node *
3e0bae44b65f5c46989fcef3d1e07203f496327eTimo Sirainenjson_tree_root(const struct json_tree *tree);
296dca49e4fe6046e0328c67ef1cf4b8077dec9cTimo Sirainen/* Find a node with the specified key from an OBJECT node */
e8490a52a1bc71bc53034e68f464435684ad810fTimo Sirainenconst struct json_tree_node *
67c47dbb3fde79218320fd38a45c33f61bbf3012Timo Sirainenjson_tree_find_key(const struct json_tree_node *node, const char *key);
e8490a52a1bc71bc53034e68f464435684ad810fTimo Sirainen/* Find an object node (from an array), which contains the specified key=value
e8490a52a1bc71bc53034e68f464435684ad810fTimo Sirainen in its values. */
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainenconst struct json_tree_node *
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainenjson_tree_find_child_with(const struct json_tree_node *node,
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen const char *key, const char *value);
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen#endif
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen