1 /*** 2 This file is part of PulseAudio. 3 4 Copyright 2016 Arun Raghavan <mail@arunraghavan.net> 5 6 PulseAudio is free software; you can redistribute it and/or modify 7 it under the terms of the GNU Lesser General Public License as published 8 by the Free Software Foundation; either version 2.1 of the License, 9 or (at your option) any later version. 10 11 PulseAudio is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public License 17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>. 18 ***/ 19 20 #pragma once 21 22 #include <stdbool.h> 23 #include <stdint.h> 24 #include <pulsecore/hashmap.h> 25 26 #define PA_DOUBLE_IS_EQUAL(x, y) (((x) - (y)) < 0.000001 && ((x) - (y)) > -0.000001) 27 28 typedef enum { 29 PA_JSON_TYPE_INIT = 0, 30 PA_JSON_TYPE_NULL, 31 PA_JSON_TYPE_INT, 32 PA_JSON_TYPE_DOUBLE, 33 PA_JSON_TYPE_BOOL, 34 PA_JSON_TYPE_STRING, 35 PA_JSON_TYPE_ARRAY, 36 PA_JSON_TYPE_OBJECT, 37 } pa_json_type; 38 39 typedef struct pa_json_object pa_json_object; 40 41 pa_json_object* pa_json_parse(const char *str); 42 pa_json_type pa_json_object_get_type(const pa_json_object *obj); 43 void pa_json_object_free(pa_json_object *obj); 44 45 /* All pointer members that are returned are valid while the corresponding object is valid */ 46 47 int64_t pa_json_object_get_int(const pa_json_object *o); 48 double pa_json_object_get_double(const pa_json_object *o); 49 bool pa_json_object_get_bool(const pa_json_object *o); 50 const char* pa_json_object_get_string(const pa_json_object *o); 51 52 const pa_json_object* pa_json_object_get_object_member(const pa_json_object *o, const char *name); 53 54 /** Returns pa_hashmap (char* -> const pa_json_object*) to iterate over object members. \since 15.0 */ 55 const pa_hashmap *pa_json_object_get_object_member_hashmap(const pa_json_object *o); 56 57 int pa_json_object_get_array_length(const pa_json_object *o); 58 const pa_json_object* pa_json_object_get_array_member(const pa_json_object *o, int index); 59 60 bool pa_json_object_equal(const pa_json_object *o1, const pa_json_object *o2); 61 62 /** @{ \name Write functions */ 63 64 /** Structure which holds a JSON encoder. Wrapper for pa_strbuf and encoder context. \since 15.0 */ 65 typedef struct pa_json_encoder pa_json_encoder; 66 67 /** Create a new pa_json_encoder structure. \since 15.0 */ 68 pa_json_encoder *pa_json_encoder_new(void); 69 /** Free a pa_json_encoder structure. \since 15.0 */ 70 void pa_json_encoder_free(pa_json_encoder *encoder); 71 /** Convert pa_json_encoder to string, free pa_json_encoder structure. 72 * The returned string needs to be freed with pa_xree(). \since 15.0 */ 73 char *pa_json_encoder_to_string_free(pa_json_encoder *encoder); 74 /** Check if a pa_json_encoder is empty (nothing has been added). \since 16.0 */ 75 bool pa_json_encoder_is_empty(pa_json_encoder *encoder); 76 77 /** Start appending JSON object element by writing an opening brace. \since 15.0 */ 78 void pa_json_encoder_begin_element_object(pa_json_encoder *encoder); 79 /** Start appending JSON object member to JSON object. \since 15.0 */ 80 void pa_json_encoder_begin_member_object(pa_json_encoder *encoder, const char *name); 81 /** End appending JSON object element or member to JSON object. \since 15.0 */ 82 void pa_json_encoder_end_object(pa_json_encoder *encoder); 83 /** Start appending JSON array element by writing an opening bracket. \since 15.0 */ 84 void pa_json_encoder_begin_element_array(pa_json_encoder *encoder); 85 /** Start appending JSON array member to JSON object. \since 15.0 */ 86 void pa_json_encoder_begin_member_array(pa_json_encoder *encoder, const char *name); 87 /** End appending JSON array element or member to JSON object. \since 15.0 */ 88 void pa_json_encoder_end_array(pa_json_encoder *encoder); 89 /** Append null element to JSON. \since 15.0 */ 90 void pa_json_encoder_add_element_null(pa_json_encoder *encoder); 91 /** Append null member to JSON object. \since 15.0 */ 92 void pa_json_encoder_add_member_null(pa_json_encoder *encoder, const char *name); 93 /** Append boolean element to JSON. \since 15.0 */ 94 void pa_json_encoder_add_element_bool(pa_json_encoder *encoder, bool value); 95 /** Append boolean member to JSON object. \since 15.0 */ 96 void pa_json_encoder_add_member_bool(pa_json_encoder *encoder, const char *name, bool value); 97 /** Append string element to JSON. Value will be escaped. \since 15.0 */ 98 void pa_json_encoder_add_element_string(pa_json_encoder *encoder, const char *value); 99 /** Append string member to JSON object. Value will be escaped. \since 15.0 */ 100 void pa_json_encoder_add_member_string(pa_json_encoder *encoder, const char *name, const char *value); 101 /** Append integer element to JSON. \since 15.0 */ 102 void pa_json_encoder_add_element_int(pa_json_encoder *encoder, int64_t value); 103 /** Append integer member to JSON object. \since 15.0 */ 104 void pa_json_encoder_add_member_int(pa_json_encoder *encoder, const char *name, int64_t value); 105 /** Append double element to JSON. \since 15.0 */ 106 void pa_json_encoder_add_element_double(pa_json_encoder *encoder, double value, int precision); 107 /** Append double member to JSON object. \since 15.0 */ 108 void pa_json_encoder_add_member_double(pa_json_encoder *encoder, const char *name, double value, int precision); 109 /** Append raw json string element to JSON. String will be written as is. \since 15.0 */ 110 void pa_json_encoder_add_element_raw_json(pa_json_encoder *encoder, const char *raw_json_string); 111 /** Append raw json string member to JSON object. String will be written as is. \since 15.0 */ 112 void pa_json_encoder_add_member_raw_json(pa_json_encoder *encoder, const char *name, const char *raw_json_string); 113