gen-rack

Create VCV Rack modules from gen~ exports
Log | Files | Refs | README | LICENSE

json_builder.h (5242B)


      1 
      2 /* vim: set et ts=3 sw=3 sts=3 ft=c:
      3  *
      4  * Copyright (C) 2014 James McLaughlin.  All rights reserved.
      5  * https://github.com/udp/json-builder
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  *
     11  * 1. Redistributions of source code must retain the above copyright
     12  *   notice, this list of conditions and the following disclaimer.
     13  *
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *   notice, this list of conditions and the following disclaimer in the
     16  *   documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #ifndef _JSON_BUILDER_H
     32 #define _JSON_BUILDER_H
     33 
     34 /* Requires json.h from json-parser
     35  * https://github.com/udp/json-parser
     36  */
     37 #include "json.h"
     38 
     39 #ifdef __cplusplus
     40 extern "C"
     41 {
     42 #endif
     43 
     44 /* IMPORTANT NOTE:  If you want to use json-builder functions with values
     45  * allocated by json-parser as part of the parsing process, you must pass
     46  * json_builder_extra as the value_extra setting in json_settings when
     47  * parsing.  Otherwise there will not be room for the extra state and
     48  * json-builder WILL invoke undefined behaviour.
     49  *
     50  * Also note that unlike json-parser, json-builder does not currently support
     51  * custom allocators (for no particular reason other than that it doesn't have
     52  * any settings or global state.)
     53  */
     54 extern const size_t json_builder_extra;
     55 
     56 
     57 /*** Arrays
     58  ***
     59  * Note that all of these length arguments are just a hint to allow for
     60  * pre-allocation - passing 0 is fine.
     61  */
     62 json_value * json_array_new (size_t length);
     63 json_value * json_array_push (json_value * array, json_value *);
     64 
     65 
     66 /*** Objects
     67  ***/
     68 json_value * json_object_new (size_t length);
     69 
     70 json_value * json_object_push (json_value * object,
     71                                const json_char * name,
     72                                json_value *);
     73 
     74 /* Same as json_object_push, but doesn't call strlen() for you.
     75  */
     76 json_value * json_object_push_length (json_value * object,
     77                                       unsigned int name_length, const json_char * name,
     78                                       json_value *);
     79 
     80 /* Same as json_object_push_length, but doesn't copy the name buffer before
     81  * storing it in the value.  Use this micro-optimisation at your own risk.
     82  */
     83 json_value * json_object_push_nocopy (json_value * object,
     84                                       unsigned int name_length, json_char * name,
     85                                       json_value *);
     86 
     87 /* Merges all entries from objectB into objectA and destroys objectB.
     88  */
     89 json_value * json_object_merge (json_value * objectA, json_value * objectB);
     90 
     91 /* Sort the entries of an object based on the order in a prototype object.
     92  * Helpful when reading JSON and writing it again to preserve user order.
     93  */
     94 void json_object_sort (json_value * object, json_value * proto);
     95 
     96 
     97 
     98 /*** Strings
     99  ***/
    100 json_value * json_string_new (const json_char *);
    101 json_value * json_string_new_length (unsigned int length, const json_char *);
    102 json_value * json_string_new_nocopy (unsigned int length, json_char *);
    103 
    104 
    105 /*** Everything else
    106  ***/
    107 json_value * json_integer_new (json_int_t);
    108 json_value * json_double_new (double);
    109 json_value * json_boolean_new (int);
    110 json_value * json_null_new ();
    111 
    112 
    113 /*** Serializing
    114  ***/
    115 #define json_serialize_mode_multiline     0
    116 #define json_serialize_mode_single_line   1
    117 #define json_serialize_mode_packed        2
    118 
    119 #define json_serialize_opt_CRLF                    (1 << 1)
    120 #define json_serialize_opt_pack_brackets           (1 << 2)
    121 #define json_serialize_opt_no_space_after_comma    (1 << 3)
    122 #define json_serialize_opt_no_space_after_colon    (1 << 4)
    123 #define json_serialize_opt_use_tabs                (1 << 5)
    124 
    125 typedef struct json_serialize_opts
    126 {
    127    int mode;
    128    int opts;
    129    int indent_size;
    130 
    131 } json_serialize_opts;
    132 
    133 
    134 /* Returns a length in characters that is at least large enough to hold the
    135  * value in its serialized form, including a null terminator.
    136  */
    137 size_t json_measure (json_value *);
    138 size_t json_measure_ex (json_value *, json_serialize_opts);
    139 
    140 
    141 /* Serializes a JSON value into the buffer given (which must already be
    142  * allocated with a length of at least json_measure(value, opts))
    143  */
    144 void json_serialize (json_char * buf, json_value *);
    145 void json_serialize_ex (json_char * buf, json_value *, json_serialize_opts);
    146 
    147 
    148 /*** Cleaning up
    149  ***/
    150 void json_builder_free (json_value *);
    151 
    152 #ifdef __cplusplus
    153 }
    154 #endif
    155 
    156 #endif
    157 
    158 
    159 
    160 
    161