1 /* Copyright (C) 2018 MariaDB Corporation 2 3 This program is free software; you can redistribute it and/or modify 4 it under the terms of the GNU General Public License as published by 5 the Free Software Foundation; version 2 of the License. 6 7 This program is distributed in the hope that it will be useful, 8 but WITHOUT ANY WARRANTY; without even the implied warranty of 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 GNU General Public License for more details. 11 12 You should have received a copy of the GNU General Public License 13 along with this program; if not, write to the Free Software 14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */ 15 16 #ifndef MYSQL_SERVICE_JSON 17 #define MYSQL_SERVICE_JSON 18 19 /** 20 @file 21 json service 22 23 Exports JSON parsing methods for plugins to use. 24 25 Functions of the service: 26 json_type - returns the type of the JSON argument, 27 and the parsed value if it's scalar (not object or array) 28 29 json_get_array_item - expects JSON array as an argument, 30 and returns the type of the element at index `n_item`. 31 Returns JSV_NOTHING type if the array is shorter 32 than n_item and the actual length of the array in value_len. 33 If successful, then `value` up till `value[value_len]` contains the array 34 element at the desired index (n_item). 35 36 json_get_object_key - expects JSON object as an argument, 37 searches for a key in the object, return it's type and stores its value in `value`. 38 JSV_NOTHING if no such key found, the number of keys 39 in v_len. 40 41 json_get_object_nkey - expects JSON object as an argument. 42 finds n_key's key in the object, returns it's name, type and value. 43 JSV_NOTHING if object has less keys than n_key. 44 */ 45 46 47 #ifdef __cplusplus 48 extern "C" { 49 #endif 50 51 enum json_types 52 { 53 JSV_BAD_JSON=-1, 54 JSV_NOTHING=0, 55 JSV_OBJECT=1, 56 JSV_ARRAY=2, 57 JSV_STRING=3, 58 JSV_NUMBER=4, 59 JSV_TRUE=5, 60 JSV_FALSE=6, 61 JSV_NULL=7 62 }; 63 64 extern struct json_service_st { 65 enum json_types (*json_type)(const char *js, const char *js_end, 66 const char **value, int *value_len); 67 enum json_types (*json_get_array_item)(const char *js, const char *js_end, 68 int n_item, 69 const char **value, int *value_len); 70 enum json_types (*json_get_object_key)(const char *js, const char *js_end, 71 const char *key, 72 const char **value, int *value_len); 73 enum json_types (*json_get_object_nkey)(const char *js,const char *js_end, 74 int nkey, 75 const char **keyname, const char **keyname_end, 76 const char **value, int *value_len); 77 int (*json_escape_string)(const char *str,const char *str_end, 78 char *json, char *json_end); 79 int (*json_unescape_json)(const char *json_str, const char *json_end, 80 char *res, char *res_end); 81 } *json_service; 82 83 #ifdef MYSQL_DYNAMIC_PLUGIN 84 85 #define json_type json_service->json_type 86 #define json_get_array_item json_service->json_get_array_item 87 #define json_get_object_key json_service->json_get_object_key 88 #define json_get_object_nkey json_service->json_get_object_nkey 89 #define json_escape_string json_service->json_escape_string 90 #define json_unescape_json json_service->json_unescape_json 91 92 #else 93 94 enum json_types json_type(const char *js, const char *js_end, 95 const char **value, int *value_len); 96 enum json_types json_get_array_item(const char *js, const char *js_end, 97 int n_item, 98 const char **value, int *value_len); 99 enum json_types json_get_object_key(const char *js, const char *js_end, 100 const char *key, 101 const char **value, int *value_len); 102 enum json_types json_get_object_nkey(const char *js,const char *js_end, int nkey, 103 const char **keyname, const char **keyname_end, 104 const char **value, int *value_len); 105 int json_escape_string(const char *str,const char *str_end, 106 char *json, char *json_end); 107 int json_unescape_json(const char *json_str, const char *json_end, 108 char *res, char *res_end); 109 110 #endif /*MYSQL_DYNAMIC_PLUGIN*/ 111 112 113 #ifdef __cplusplus 114 } 115 #endif 116 117 #endif /*MYSQL_SERVICE_JSON */ 118 119 120