CUBRID Engine  latest
db_json.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 
19 /*
20  * db_json.hpp - functions related to json
21  */
22 
23 #ifndef _DB_JSON_HPP_
24 #define _DB_JSON_HPP_
25 
26 #include "error_manager.h"
28 #include "storage_common.h"
29 
30 #include <cstdint>
31 #include <string>
32 
33 // forward definitions
34 struct or_buf;
35 
36 #if defined (__cplusplus)
37 class JSON_DOC;
38 class JSON_PATH;
39 class JSON_VALIDATOR;
40 class JSON_ITERATOR;
41 #else
42 typedef void JSON_DOC;
43 typedef void JSON_PATH;
44 typedef void JSON_VALIDATOR;
45 typedef void JSON_ITERATOR;
46 #endif
47 
48 #if defined (__cplusplus)
49 #include <vector>
50 
51 /*
52  * these also double as type precedence
53  * INT and DOUBLE actually have the same precedence
54  */
55 enum DB_JSON_TYPE
56 {
57  DB_JSON_NULL = 0,
58  DB_JSON_UNKNOWN,
59  DB_JSON_INT,
60  DB_JSON_BIGINT,
61  DB_JSON_DOUBLE,
62  DB_JSON_STRING,
63  DB_JSON_OBJECT,
64  DB_JSON_ARRAY,
65  DB_JSON_BOOL,
66 };
67 
68 using JSON_DOC_STORE = cubmem::reference_store<JSON_DOC>;
69 
70 bool db_json_is_valid (const char *json_str);
71 const char *db_json_get_type_as_str (const JSON_DOC *document);
72 unsigned int db_json_get_length (const JSON_DOC *document);
73 unsigned int db_json_get_depth (const JSON_DOC *doc);
74 int db_json_extract_document_from_path (const JSON_DOC *document, const std::vector<std::string> &raw_path,
75  JSON_DOC_STORE &result, bool allow_wildcards = true);
76 int db_json_extract_document_from_path (const JSON_DOC *document, const std::string &path,
77  JSON_DOC_STORE &result, bool allow_wildcards = true);
78 int db_json_contains_path (const JSON_DOC *document, const std::vector<std::string> &paths, bool find_all,
79  bool &result);
81 
83 
84 int db_json_add_member_to_object (JSON_DOC *doc, const char *name, const char *value);
85 int db_json_add_member_to_object (JSON_DOC *doc, const char *name, int value);
86 int db_json_add_member_to_object (JSON_DOC *doc, const char *name, std::int64_t value);
87 int db_json_add_member_to_object (JSON_DOC *doc, const char *name, double value);
88 int db_json_add_member_to_object (JSON_DOC *doc, const char *name, const JSON_DOC *value);
89 
90 void db_json_add_element_to_array (JSON_DOC *doc, char *value);
91 void db_json_add_element_to_array (JSON_DOC *doc, int value);
92 void db_json_add_element_to_array (JSON_DOC *doc, std::int64_t value);
93 void db_json_add_element_to_array (JSON_DOC *doc, double value);
94 void db_json_add_element_to_array (JSON_DOC *doc, const JSON_DOC *value);
95 
96 int db_json_get_json_from_str (const char *json_raw, JSON_DOC *&doc, size_t json_raw_length);
98 
99 int db_json_serialize (const JSON_DOC &doc, or_buf &buffer);
100 std::size_t db_json_serialize_length (const JSON_DOC &doc);
101 int db_json_deserialize (or_buf *buf, JSON_DOC *&doc);
102 
103 int db_json_insert_func (const JSON_DOC *doc_to_be_inserted, JSON_DOC &doc_destination, const char *raw_path);
104 int db_json_replace_func (const JSON_DOC *value, JSON_DOC &doc, const char *raw_path);
105 int db_json_set_func (const JSON_DOC *value, JSON_DOC &doc, const char *raw_path);
106 int db_json_keys_func (const JSON_DOC &doc, JSON_DOC &result_json, const char *raw_path);
107 int db_json_array_append_func (const JSON_DOC *value, JSON_DOC &doc, const char *raw_path);
108 int db_json_array_insert_func (const JSON_DOC *value, JSON_DOC &doc, const char *raw_path);
109 int db_json_remove_func (JSON_DOC &doc, const char *raw_path);
110 int db_json_search_func (const JSON_DOC &doc, const DB_VALUE *pattern, const DB_VALUE *esc_char,
111  std::vector<JSON_PATH> &paths, const std::vector<std::string> &patterns, bool find_all);
112 int db_json_merge_patch_func (const JSON_DOC *source, JSON_DOC *&dest);
113 int db_json_merge_preserve_func (const JSON_DOC *source, JSON_DOC *&dest);
114 int db_json_get_all_paths_func (const JSON_DOC &doc, JSON_DOC *&result_json);
115 void db_json_pretty_func (const JSON_DOC &doc, char *&result_str);
116 std::string db_json_json_string_as_utf8 (std::string raw_json_string);
117 int db_json_path_unquote_object_keys_external (std::string &sql_path);
118 int db_json_unquote (const JSON_DOC &doc, char *&result_str);
119 
120 int db_json_object_contains_key (JSON_DOC *obj, const char *key, int &result);
122 int db_json_validate_json (const char *json_body);
123 
124 int db_json_load_validator (const char *json_schema_raw, JSON_VALIDATOR *&validator);
129 void db_json_delete_doc (JSON_DOC *&doc);
130 void db_json_delete_validator (JSON_VALIDATOR *&validator);
131 int db_json_validate_doc (JSON_VALIDATOR *validator, JSON_DOC *doc);
133 bool db_json_path_contains_wildcard (const char *sql_path);
134 
135 void db_json_iterator_next (JSON_ITERATOR &json_itr);
138 void db_json_set_iterator (JSON_ITERATOR *&json_itr, const JSON_DOC &new_doc);
139 void db_json_reset_iterator (JSON_ITERATOR *&json_itr);
140 bool db_json_iterator_is_empty (const JSON_ITERATOR &json_itr);
141 JSON_ITERATOR *db_json_create_iterator (const DB_JSON_TYPE &type);
144 
145 DB_JSON_TYPE db_json_get_type (const JSON_DOC *doc);
146 int db_json_get_int_from_document (const JSON_DOC *doc);
147 std::int64_t db_json_get_bigint_from_document (const JSON_DOC *doc);
148 double db_json_get_double_from_document (const JSON_DOC *doc);
149 const char *db_json_get_string_from_document (const JSON_DOC *doc);
151 bool db_json_get_bool_from_document (const JSON_DOC *doc);
153 
154 void db_json_set_string_to_doc (JSON_DOC *doc, const char *str, unsigned len);
155 void db_json_set_double_to_doc (JSON_DOC *doc, double d);
156 void db_json_set_int_to_doc (JSON_DOC *doc, int i);
157 void db_json_set_bigint_to_doc (JSON_DOC *doc, std::int64_t i);
158 
159 int db_json_value_is_contained_in_doc (const JSON_DOC *doc, const JSON_DOC *value, bool &result);
160 bool db_json_are_docs_equal (const JSON_DOC *doc1, const JSON_DOC *doc2);
162 bool db_json_doc_has_numeric_type (const JSON_DOC *doc);
163 bool db_json_doc_is_uncomparable (const JSON_DOC *doc);
164 
165 // DB_VALUE manipulation functions
166 int db_value_to_json_doc (const DB_VALUE &db_val, bool copy_json, JSON_DOC_STORE &json_doc);
167 int db_value_to_json_value (const DB_VALUE &db_val, JSON_DOC_STORE &json_doc);
168 void db_make_json_from_doc_store_and_release (DB_VALUE &value, JSON_DOC_STORE &doc_store);
169 int db_value_to_json_path (const DB_VALUE &path_value, FUNC_TYPE fcode, std::string &path_str);
170 int db_value_to_json_key (const DB_VALUE &db_val, std::string &key_str);
171 
172 int db_json_normalize_path_string (const char *pointer_path, std::string &normalized_path);
173 template <typename Fn, typename... Args>
174 inline int
175 db_json_convert_string_and_call (const char *json_raw, size_t json_raw_length, Fn &&func, Args &&... args)
176 {
177  JSON_DOC *doc = NULL;
178  int error_code;
179 
180  error_code = db_json_get_json_from_str (json_raw, doc, json_raw_length);
181  if (error_code != NO_ERROR)
182  {
183  return error_code;
184  }
185 
186  error_code = func (doc, std::forward<Args> (args)...);
187  db_json_delete_doc (doc);
188  return error_code;
189 }
190 
191 #endif /* defined (__cplusplus) */
192 
193 #endif /* _DB_JSON_HPP_ */
bool db_json_doc_has_numeric_type(const JSON_DOC *doc)
Definition: db_json.cpp:3141
#define NO_ERROR
Definition: error_code.h:46
void db_json_delete_json_iterator(JSON_ITERATOR *&json_itr)
Definition: db_json.cpp:978
bool db_json_are_validators_equal(JSON_VALIDATOR *val1, JSON_VALIDATOR *val2)
Definition: db_json.cpp:2401
unsigned int db_json_get_length(const JSON_DOC *document)
Definition: db_json.cpp:1046
double db_json_get_double_from_document(const JSON_DOC *doc)
Definition: db_json.cpp:2537
int db_json_merge_patch_func(const JSON_DOC *source, JSON_DOC *&dest)
Definition: db_json.cpp:2469
void db_make_json_from_doc_store_and_release(DB_VALUE &value, JSON_DOC_STORE &doc_store)
Definition: db_json.cpp:3333
int db_json_replace_func(const JSON_DOC *value, JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:1743
void JSON_PATH
Definition: db_json.hpp:43
const JSON_DOC * db_json_iterator_get_document(JSON_ITERATOR &json_itr)
Definition: db_json.cpp:930
JSON_ITERATOR * db_json_create_iterator(const DB_JSON_TYPE &type)
Definition: db_json.cpp:963
void JSON_VALIDATOR
Definition: db_json.hpp:44
int db_json_normalize_path_string(const char *pointer_path, std::string &output)
Definition: db_json.cpp:2749
char * db_json_get_json_body_from_document(const JSON_DOC &doc)
Definition: db_json.cpp:1370
bool db_json_iterator_has_next(JSON_ITERATOR &json_itr)
Definition: db_json.cpp:936
bool db_json_iterator_is_empty(const JSON_ITERATOR &json_itr)
Definition: db_json.cpp:957
FUNC_TYPE
JSON_DOC * db_json_get_copy_of_doc(const JSON_DOC *doc)
Definition: db_json.cpp:1627
void db_json_set_double_to_doc(JSON_DOC *doc, double d)
Definition: db_json.cpp:3104
bool db_json_is_valid(const char *json_str)
Definition: db_json.cpp:994
bool db_json_doc_is_uncomparable(const JSON_DOC *doc)
Definition: db_json.cpp:3147
int db_json_extract_document_from_path(const JSON_DOC *document, const std::string &path, JSON_DOC_STORE &result, bool allow_wildcards)
Definition: db_json.cpp:1153
void db_json_pretty_func(const JSON_DOC &doc, char *&result_str)
Definition: db_json.cpp:2881
int db_json_remove_func(JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:1911
int db_value_to_json_path(const DB_VALUE &path_value, FUNC_TYPE fcode, std::string &path_str)
Definition: db_json.cpp:3163
int db_json_value_is_contained_in_doc(const JSON_DOC *doc, const JSON_DOC *value, bool &result)
Definition: db_json.cpp:2976
void db_json_set_string_to_doc(JSON_DOC *doc, const char *str, unsigned len)
Definition: db_json.cpp:3098
JSON_DOC * db_json_make_json_array()
Definition: db_json.cpp:2347
static void db_json_add_element_to_array(JSON_DOC *doc, const JSON_VALUE *value)
Definition: db_json.cpp:1538
int db_json_get_all_paths_func(const JSON_DOC &doc, JSON_DOC *&result_json)
Definition: db_json.cpp:2852
std::size_t db_json_serialize_length(const JSON_DOC &doc)
Definition: db_json.cpp:3946
void db_json_iterator_next(JSON_ITERATOR &json_itr)
Definition: db_json.cpp:924
JSON_VALIDATOR * db_json_copy_validator(JSON_VALIDATOR *validator)
Definition: db_json.cpp:2382
JSON_DOC * db_json_make_json_object()
Definition: db_json.cpp:2339
const char * db_json_get_schema_raw_from_validator(JSON_VALIDATOR *val)
Definition: db_json.cpp:2310
int db_json_array_insert_func(const JSON_DOC *value, JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:2124
int db_json_get_json_from_str(const char *json_raw, JSON_DOC *&doc, size_t json_raw_length)
Definition: db_json.cpp:1608
const char * db_json_get_type_as_str(const JSON_DOC *document)
Definition: db_json.cpp:1002
int db_json_get_int_from_document(const JSON_DOC *doc)
Definition: db_json.cpp:2525
std::int64_t db_json_get_bigint_from_document(const JSON_DOC *doc)
Definition: db_json.cpp:2531
void db_json_delete_doc(JSON_DOC *&doc)
Definition: db_json.cpp:2355
void db_json_set_iterator(JSON_ITERATOR *&json_itr, const JSON_DOC &new_doc)
Definition: db_json.cpp:942
int db_json_merge_preserve_func(const JSON_DOC *source, JSON_DOC *&dest)
Definition: db_json.cpp:2501
void db_json_set_bigint_to_doc(JSON_DOC *doc, std::int64_t i)
Definition: db_json.cpp:3116
#define NULL
Definition: freelistheap.h:34
char * db_json_copy_string_from_document(const JSON_DOC *doc)
Definition: db_json.cpp:2561
int db_json_serialize(const JSON_DOC &doc, or_buf &buffer)
Definition: db_json.cpp:3931
int db_json_search_func(const JSON_DOC &doc, const DB_VALUE *pattern, const DB_VALUE *esc_char, std::vector< JSON_PATH > &paths, const std::vector< std::string > &patterns, bool find_all)
Definition: db_json.cpp:1961
DB_JSON_TYPE db_json_get_type(const JSON_DOC *doc)
Definition: db_json.cpp:2519
int db_json_path_unquote_object_keys_external(std::string &sql_path)
Definition: db_json.cpp:2765
int db_json_deserialize(OR_BUF *buf, JSON_DOC *&doc)
Definition: db_json.cpp:4266
int db_json_array_append_func(const JSON_DOC *value, JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:2042
int db_json_validate_json(const char *json_body)
Definition: db_json.cpp:2316
void JSON_ITERATOR
Definition: db_json.hpp:45
int db_json_unquote(const JSON_DOC &doc, char *&result_str)
Definition: db_json.cpp:1090
void JSON_DOC
Definition: db_json.hpp:34
void db_json_reset_iterator(JSON_ITERATOR *&json_itr)
Definition: db_json.cpp:948
void db_json_set_int_to_doc(JSON_DOC *doc, int i)
Definition: db_json.cpp:3110
int db_json_keys_func(const JSON_DOC &doc, JSON_DOC &result_json, const char *raw_path)
Definition: db_json.cpp:2919
const char * db_json_get_string_from_document(const JSON_DOC *doc)
Definition: db_json.cpp:2543
unsigned int db_json_get_depth(const JSON_DOC *doc)
Definition: db_json.cpp:1079
void db_json_clear_json_iterator(JSON_ITERATOR *&json_itr)
Definition: db_json.cpp:985
void db_json_make_document_null(JSON_DOC *doc)
Definition: db_json.cpp:3132
void db_json_delete_validator(JSON_VALIDATOR *&validator)
Definition: db_json.cpp:2394
int db_json_validate_doc(JSON_VALIDATOR *validator, JSON_DOC *doc)
Definition: db_json.cpp:2388
bool db_json_are_docs_equal(const JSON_DOC *doc1, const JSON_DOC *doc2)
Definition: db_json.cpp:3122
std::string db_json_json_string_as_utf8(std::string raw_json_string)
Definition: db_json.cpp:2893
int db_json_load_validator(const char *json_schema_raw, JSON_VALIDATOR *&validator)
Definition: db_json.cpp:2362
int i
Definition: dynamic_load.c:954
int db_json_add_member_to_object(JSON_DOC *doc, const char *name, const char *value)
Definition: db_json.cpp:1408
int db_json_contains_path(const JSON_DOC *document, const std::vector< std::string > &paths, bool find_all, bool &result)
Definition: db_json.cpp:1265
int db_json_insert_func(const JSON_DOC *value, JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:1665
int db_value_to_json_value(const DB_VALUE &db_val, JSON_DOC_STORE &json_doc)
Definition: db_json.cpp:3254
int db_json_object_contains_key(JSON_DOC *obj, const char *key, int &result)
Definition: db_json.cpp:2297
int db_value_to_json_doc(const DB_VALUE &db_val, bool force_copy, JSON_DOC_STORE &json_doc)
Definition: db_json.cpp:3183
bool db_json_path_contains_wildcard(const char *sql_path)
Definition: db_json.cpp:2776
int db_value_to_json_key(const DB_VALUE &db_val, std::string &key_str)
Definition: db_json.cpp:3309
bool db_json_get_bool_from_document(const JSON_DOC *doc)
Definition: db_json.cpp:2555
char * db_json_get_bool_as_str_from_document(const JSON_DOC *doc)
Definition: db_json.cpp:2549
JSON_DOC * db_json_allocate_doc()
Definition: db_json.cpp:2332
int db_json_set_func(const JSON_DOC *value, JSON_DOC &doc, const char *raw_path)
Definition: db_json.cpp:1838
char * db_json_get_raw_json_body_from_document(const JSON_DOC *doc)
Definition: db_json.cpp:1357