X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fsmap.c;h=e612ac7bad7749bf1a8212cf98af2e91ad177704;hb=cccc13569249376eb2e1a364e07dfc33a745d791;hp=866532156d3cae7348bf24d4dd5c3f689122beb2;hpb=cbb13e8e4c3e5766202ebe68bed9e414f62fa1c7;p=openvswitch diff --git a/lib/smap.c b/lib/smap.c index 86653215..e612ac7b 100644 --- a/lib/smap.c +++ b/lib/smap.c @@ -18,6 +18,7 @@ #include #include "hash.h" +#include "json.h" static struct smap_node *smap_add__(struct smap *, char *, void *, size_t hash); @@ -232,6 +233,41 @@ smap_sort(const struct smap *smap) return nodes; } } + +/* Adds each of the key-value pairs from 'json' (which must be a JSON object + * whose values are strings) to 'smap'. + * + * The caller must have initialized 'smap'. + * + * The caller retains ownership of 'json' and everything in it. */ +void +smap_from_json(struct smap *smap, const struct json *json) +{ + const struct shash_node *node; + + SHASH_FOR_EACH (node, json_object(json)) { + const struct json *value = node->data; + smap_add(smap, node->name, json_string(value)); + } +} + +/* Returns a JSON object that maps from the keys in 'smap' to their values. + * + * The caller owns the returned value and must eventually json_destroy() it. + * + * The caller retains ownership of 'smap' and everything in it. */ +struct json * +smap_to_json(const struct smap *smap) +{ + const struct smap_node *node; + struct json *json; + + json = json_object_create(); + SMAP_FOR_EACH (node, smap) { + json_object_put_string(json, node->key, node->value); + } + return json; +} /* Private Helpers. */