• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
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 #ifndef GRPC_CORE_LIB_JSON_JSON_H
20 #define GRPC_CORE_LIB_JSON_JSON_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <stdbool.h>
25 #include <stdlib.h>
26 
27 #include "src/core/lib/json/json_common.h"
28 
29 /* A tree-like structure to hold json values. The key and value pointers
30  * are not owned by it.
31  */
32 typedef struct grpc_json {
33   struct grpc_json* next;
34   struct grpc_json* prev;
35   struct grpc_json* child;
36   struct grpc_json* parent;
37 
38   grpc_json_type type;
39   const char* key;
40   const char* value;
41 
42   /* if set, destructor will free value */
43   bool owns_value;
44 } grpc_json;
45 
46 /* The next two functions are going to parse the input string, and
47  * modify it in the process, in order to use its space to store
48  * all of the keys and values for the returned object tree.
49  *
50  * They assume UTF-8 input stream, and will output UTF-8 encoded
51  * strings in the tree. The input stream's UTF-8 isn't validated,
52  * as in, what you input is what you get as an output.
53  *
54  * All the keys and values in the grpc_json objects will be strings
55  * pointing at your input buffer.
56  *
57  * Delete the allocated tree afterward using grpc_json_destroy().
58  */
59 grpc_json* grpc_json_parse_string_with_len(char* input, size_t size);
60 grpc_json* grpc_json_parse_string(char* input);
61 
62 /* This function will create a new string using gpr_realloc, and will
63  * deserialize the grpc_json tree into it. It'll be zero-terminated,
64  * but will be allocated in chunks of 256 bytes.
65  *
66  * The indent parameter controls the way the output is formatted.
67  * If indent is 0, then newlines will be suppressed as well, and the
68  * output will be condensed at its maximum.
69  */
70 char* grpc_json_dump_to_string(grpc_json* json, int indent);
71 
72 /* Use these to create or delete a grpc_json object.
73  * Deletion is recursive. We will not attempt to free any of the strings
74  * in any of the objects of that tree, unless the boolean, owns_value,
75  * is true.
76  */
77 grpc_json* grpc_json_create(grpc_json_type type);
78 void grpc_json_destroy(grpc_json* json);
79 
80 /* Links the child json object into the parent's json tree. If the parent
81  * already has children, then passing in the most recently added child as the
82  * sibling parameter is an optimization. For if sibling is NULL, this function
83  * will manually traverse the tree in order to find the right most sibling.
84  */
85 grpc_json* grpc_json_link_child(grpc_json* parent, grpc_json* child,
86                                 grpc_json* sibling);
87 
88 /* Creates a child json object into the parent's json tree then links it in
89  * as described above. */
90 grpc_json* grpc_json_create_child(grpc_json* sibling, grpc_json* parent,
91                                   const char* key, const char* value,
92                                   grpc_json_type type, bool owns_value);
93 
94 /* Creates a child json string object from the integer num, then links the
95    json object into the parent's json tree */
96 grpc_json* grpc_json_add_number_string_child(grpc_json* parent, grpc_json* it,
97                                              const char* name, int64_t num);
98 
99 #endif /* GRPC_CORE_LIB_JSON_JSON_H */
100