2 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <json-glib/json-glib.h>
23 #include "bundle_internal.h"
25 static void __add_json_data_from_bundle(const char *key,
27 const bundle_keyval_t *kv,
30 void *basic_val = NULL;
31 size_t basic_size = 0;
32 void **array_val = NULL;
33 unsigned int array_len = 0;
34 size_t *array_elem_size = 0;
35 JsonObject *json_obj = (JsonObject *)user_data;
43 if (bundle_keyval_type_is_array((bundle_keyval_t *)kv)) {
44 ret = bundle_keyval_get_array_val((bundle_keyval_t *)kv,
45 &array_val, &array_len, &array_elem_size);
46 if (ret != BUNDLE_ERROR_NONE)
49 json_arr = json_array_new();
53 for (i = 0; i < array_len; i++)
54 json_array_add_string_element(json_arr, array_val[i]);
56 json_object_set_array_member(json_obj, key, json_arr);
58 ret = bundle_keyval_get_basic_val((bundle_keyval_t *)kv,
59 &basic_val, &basic_size);
60 if (ret != BUNDLE_ERROR_NONE)
63 json_object_set_string_member(json_obj, key, basic_val);
67 int bundle_to_json(bundle *b, char **json)
71 JsonGenerator *generator = NULL;
72 gchar *new_json = NULL;
74 int ret = BUNDLE_ERROR_NONE;
76 if (b == NULL || json == NULL)
77 return BUNDLE_ERROR_INVALID_PARAMETER;
79 object = json_object_new();
81 return BUNDLE_ERROR_OUT_OF_MEMORY;
83 bundle_foreach(b, __add_json_data_from_bundle, object);
85 node = json_node_new(JSON_NODE_OBJECT);
87 ret = BUNDLE_ERROR_OUT_OF_MEMORY;
91 json_node_set_object(node, object);
93 generator = json_generator_new();
94 if (generator == NULL) {
95 ret = BUNDLE_ERROR_OUT_OF_MEMORY;
99 json_generator_set_root(generator, node);
101 new_json = json_generator_to_data(generator, &length);
102 if (new_json == NULL) {
103 ret = BUNDLE_ERROR_OUT_OF_MEMORY;
107 *json = (char *)new_json;
111 g_object_unref(generator);
113 json_node_free(node);
115 json_object_unref(object);
120 static void __add_bundle_data_from_json(JsonObject *object,
125 bundle *b = (bundle *)user_data;
126 JsonNodeType node_type;
133 if (key == NULL || node == NULL)
136 node_type = JSON_NODE_TYPE(node);
137 if (node_type == JSON_NODE_ARRAY) {
138 json_arr = json_node_get_array(node);
139 len = json_array_get_length(json_arr);
141 arr_val = (char **)calloc(1, sizeof(char *) * len);
145 for (i = 0; i < len; i++)
146 arr_val[i] = strdup(json_array_get_string_element
149 bundle_add_str_array(b, key, (const char **)arr_val, len);
151 for (i = 0; i < len; i++)
154 } else if (node_type == JSON_NODE_VALUE) {
155 val = json_node_get_string(node);
157 bundle_add_str(b, key, (const char *)val);
161 int bundle_from_json(const char *json, bundle **b)
163 GError *error = NULL;
166 JsonObject *root_obj;
168 int ret = BUNDLE_ERROR_NONE;
170 if (json == NULL || b == NULL)
171 return BUNDLE_ERROR_INVALID_PARAMETER;
173 parser = json_parser_new();
175 return BUNDLE_ERROR_OUT_OF_MEMORY;
177 json_parser_load_from_data(parser, json, strlen(json), &error);
179 ret = BUNDLE_ERROR_INVALID_PARAMETER;
183 root = json_parser_get_root(parser);
185 ret = BUNDLE_ERROR_INVALID_PARAMETER;
189 root_obj = json_node_get_object(root);
190 if (root_obj == NULL) {
191 ret = BUNDLE_ERROR_INVALID_PARAMETER;
195 new_b = bundle_create();
197 ret = BUNDLE_ERROR_OUT_OF_MEMORY;
201 json_object_foreach_member(root_obj,
202 __add_bundle_data_from_json, new_b);
210 g_object_unref(parser);