blob: 89d15846ce889e2358b10573d8034a7c73fc87f4 [file] [log] [blame]
Nicolas Noble614c2bf2015-01-21 15:48:36 -08001/*
2 *
David Garcia Quintas3598d442016-03-15 14:53:05 -07003 * Copyright 2015-2016, Google Inc.
Nicolas Noble614c2bf2015-01-21 15:48:36 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_JSON_JSON_H
35#define GRPC_CORE_LIB_JSON_JSON_H
Nicolas Noble614c2bf2015-01-21 15:48:36 -080036
37#include <stdlib.h>
38
Nicolas Noblee04455a2015-01-26 17:01:29 -080039#include "src/core/json/json_common.h"
Nicolas Noble614c2bf2015-01-21 15:48:36 -080040
Nicolas Noblee04455a2015-01-26 17:01:29 -080041/* A tree-like structure to hold json values. The key and value pointers
42 * are not owned by it.
43 */
Craig Tillera82950e2015-09-22 12:33:20 -070044typedef struct grpc_json {
Craig Tiller45724b32015-09-22 10:42:19 -070045 struct grpc_json *next;
46 struct grpc_json *prev;
47 struct grpc_json *child;
48 struct grpc_json *parent;
Nicolas Noble614c2bf2015-01-21 15:48:36 -080049
Nicolas Noblee04455a2015-01-26 17:01:29 -080050 grpc_json_type type;
Craig Tiller45724b32015-09-22 10:42:19 -070051 const char *key;
52 const char *value;
Nicolas Noble614c2bf2015-01-21 15:48:36 -080053} grpc_json;
54
55/* The next two functions are going to parse the input string, and
Julien Boeuf61a0ef52015-07-08 00:49:14 +020056 * modify it in the process, in order to use its space to store
Nicolas Noble614c2bf2015-01-21 15:48:36 -080057 * all of the keys and values for the returned object tree.
58 *
59 * They assume UTF-8 input stream, and will output UTF-8 encoded
Nicolas Noblee04455a2015-01-26 17:01:29 -080060 * strings in the tree. The input stream's UTF-8 isn't validated,
61 * as in, what you input is what you get as an output.
62 *
Julien Boeuf3e29de12015-06-18 23:29:13 +020063 * All the keys and values in the grpc_json objects will be strings
Nicolas Noblee04455a2015-01-26 17:01:29 -080064 * pointing at your input buffer.
Nicolas Noble614c2bf2015-01-21 15:48:36 -080065 *
Nicolas Noble8c2be9b2015-01-27 14:21:18 -080066 * Delete the allocated tree afterward using grpc_json_destroy().
Nicolas Noble614c2bf2015-01-21 15:48:36 -080067 */
Craig Tillera82950e2015-09-22 12:33:20 -070068grpc_json *grpc_json_parse_string_with_len(char *input, size_t size);
69grpc_json *grpc_json_parse_string(char *input);
Nicolas Noble614c2bf2015-01-21 15:48:36 -080070
71/* This function will create a new string using gpr_realloc, and will
72 * deserialize the grpc_json tree into it. It'll be zero-terminated,
73 * but will be allocated in chunks of 256 bytes.
74 *
75 * The indent parameter controls the way the output is formatted.
76 * If indent is 0, then newlines will be suppressed as well, and the
77 * output will be condensed at its maximum.
78 */
Craig Tillera82950e2015-09-22 12:33:20 -070079char *grpc_json_dump_to_string(grpc_json *json, int indent);
Nicolas Noble614c2bf2015-01-21 15:48:36 -080080
81/* Use these to create or delete a grpc_json object.
82 * Deletion is recursive. We will not attempt to free any of the strings
83 * in any of the objects of that tree.
84 */
Craig Tillera82950e2015-09-22 12:33:20 -070085grpc_json *grpc_json_create(grpc_json_type type);
86void grpc_json_destroy(grpc_json *json);
Nicolas Noble614c2bf2015-01-21 15:48:36 -080087
Craig Tiller9a4dddd2016-03-25 17:08:13 -070088#endif /* GRPC_CORE_LIB_JSON_JSON_H */