These will acquire a caller in an upcoming commit.
Signed-off-by: Ben Pfaff <[email protected]>
---
lib/shash.c | 39 ++++++++++++++++++++++++++++++++++++++-
lib/shash.h | 6 +++++-
2 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/lib/shash.c b/lib/shash.c
index af917b3..29b3385 100644
--- a/lib/shash.c
+++ b/lib/shash.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2009, 2010, 2011 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
#include "shash.h"
#include <assert.h>
#include "hash.h"
+#include "json.h"
static struct shash_node *shash_find__(const struct shash *,
const char *name, size_t name_len,
@@ -368,3 +369,39 @@ smap_add(struct shash *smap, const char *key, const char
*value)
{
shash_add(smap, key, xstrdup(value));
}
+
+/* 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 shash *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 shash *smap)
+{
+ const struct shash_node *node;
+ struct json *json;
+
+ json = json_object_create();
+ SHASH_FOR_EACH (node, smap) {
+ const char *value = node->data;
+ json_object_put_string(json, node->name, value);
+ }
+ return json;
+}
diff --git a/lib/shash.h b/lib/shash.h
index decfcbc..cee8ce3 100644
--- a/lib/shash.h
+++ b/lib/shash.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2009, 2010, 2011 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@
extern "C" {
#endif
+struct json;
+
struct shash_node {
struct hmap_node node;
char *name;
@@ -72,6 +74,8 @@ void smap_destroy(struct shash *);
bool smap_equal(const struct shash *, const struct shash *);
void smap_clone(struct shash *, const struct shash *);
void smap_add(struct shash *, const char *key, const char *value);
+void smap_from_json(struct shash *, const struct json *);
+struct json *smap_to_json(const struct shash *);
#ifdef __cplusplus
}
--
1.7.2.5
_______________________________________________
dev mailing list
[email protected]
http://openvswitch.org/mailman/listinfo/dev