Cross-platform C SDK logo

Cross-platform C SDK

Read/Write Json

❮ Back
Next ❯
This page has been automatically translated using the Google Translate API services. We are working on improving texts. Thank you for your understanding and patience.

Listing 1: demo/htjson/htjson.c
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* JSON parsing examples */

#include "res_htjson.h"
#include <draw2d/draw2dall.h>
#include <inet/json.h>

/*---------------------------------------------------------------------------*/

/* C structs that map a Json object */
typedef struct _product_t Product;
typedef struct _products_t Products;

struct _product_t
{
    String *description;
    real32_t price;
};

struct _products_t
{
    uint32_t size;
    ArrSt(Product) *data;
};

DeclSt(Product);

/*---------------------------------------------------------------------------*/

static Stream* i_stm_from_json(const char_t* json_data)
{
    return stm_from_block((const byte_t*)json_data, str_len_c(json_data));
}

/*---------------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
    unref(argc);
    unref(argv);
    draw2d_start();

    /* Parsing a Json boolean */
    {
        Stream *stm = i_stm_from_json("true");
        bool_t *json = json_read(stm, NULL, bool_t);
        bstd_printf("bool_t from Json: %d\n", *json);
        json_destroy(&json, bool_t);
        stm_close(&stm);
    }

    /* Parsing a Json unsigned int */
    {
        Stream *stm = i_stm_from_json("6654");
        uint16_t *json = json_read(stm, NULL, uint16_t);
        bstd_printf("uint16_t from Json: %d\n", *json);
        json_destroy(&json, uint16_t);
        stm_close(&stm);
    }

    /* Parsing a Json signed int */
    {
        Stream *stm = i_stm_from_json("-567");
        int16_t *json = json_read(stm, NULL, int16_t);
        bstd_printf("int16_t from Json: %d\n", *json);
        json_destroy(&json, int16_t);
        stm_close(&stm);
    }

    /* Parsing a Json real */
    {
        Stream *stm = i_stm_from_json("456.45");
        real32_t *json = json_read(stm, NULL, real32_t);
        bstd_printf("real32_t from Json: %.3f\n", *json);
        json_destroy(&json, real32_t);
        stm_close(&stm);
    }

    /* Parsing a Json string */
    {
        Stream *stm = i_stm_from_json("\"Hello World\"");
        String *json = json_read(stm, NULL, String);
        bstd_printf("String from Json: %s\n", tc(json));
        json_destroy(&json, String);
        stm_close(&stm);
    }

    /* Parsing a Json b64 encoded image */
    {
        uint32_t size;
        ResPack *pack = res_htjson_respack("");
        const byte_t *data = respack_file(pack, JSON_B64_IMAGE_TXT, &size);
        Stream *stm = stm_from_block(data, size);
        Image *json = json_read(stm, NULL, Image);
        uint32_t width = image_width(json);
        uint32_t height = image_height(json);
        bstd_printf("Image from Json: width: %d height: %d\n", width, height);
        json_destroy(&json, Image);
        stm_close(&stm);
        respack_destroy(&pack);
    }

    /* Parsing a Json int array */
    {
        Stream *stm = i_stm_from_json("[ -321, 12, -8943, 228, -220, 347 ]");
        ArrSt(int16_t) *json = json_read(stm, NULL, ArrSt(int16_t));
        bstd_printf("ArrSt(int16_t) from Json: ");
        arrst_foreach(id, json, int16_t)
            bstd_printf("%d ", *id);
        arrst_end();
        bstd_printf("\n");
        json_destroy(&json, ArrSt(int16_t));
        stm_close(&stm);
    }

    /* Parsing a Json String array */
    {
        Stream *stm = i_stm_from_json("[ \"Red\", \"Green\", \"Blue\", \"Yellow\", \"Orange\" ]");
        ArrPt(String) *json = json_read(stm, NULL, ArrPt(String));
        bstd_printf("ArrPt(String) from Json: ");
        arrpt_foreach(str, json, String)
            bstd_printf("%s ", tc(str));
        arrpt_end();
        bstd_printf("\n");
        json_destroy(&json, ArrPt(String));
        stm_close(&stm);
    }

    /* Data binding (only once time in application) */
    /* This allows the Json parser to know the structure of the objects */
    dbind(Product, String*, description);
    dbind(Product, real32_t, price);
    dbind(Products, uint32_t, size);
    dbind(Products, ArrSt(Product)*, data);

    /* Parsing a Json object */
    {
        static const char_t *JSON_OBJECT = "\
        {\
            \"size\" : 3,\
            \"data\" : [\
                {\
                    \"description\" : \"Intel i7-7700K\",\
                    \"price\" : 329.99\
                },\
                {\
                    \"description\" : \"Ryzen-5-1600\",\
                    \"price\" : 194.99\
                },\
                {\
                    \"description\" : \"GTX-1060\",\
                    \"price\" : 449.99\
                }\
            ]\
        }";

        Stream *stm = i_stm_from_json(JSON_OBJECT);
        Products *json = json_read(stm, NULL, Products);
        bstd_printf("Products object from Json: size %d\n", json->size);
        arrst_foreach(elem, json->data, Product)
            bstd_printf("    Product: %s Price %.2f\n", tc(elem->description), elem->price);
        arrst_end();
        bstd_printf("\n");
        json_destroy(&json, Products);
        stm_close(&stm);
    }

    /* Writting data/objects to JSon */
    {
        Stream *stm = stm_memory(1024);

        /* Write boolean as Json */
        {
            bool_t data_bool = TRUE;
            stm_writef(stm, "Json from bool_t: ");
            json_write(stm, &data_bool, NULL, bool_t);
            stm_writef(stm, "\n");
        }

        /* Write unsigned integer as Json */
        {
            uint16_t data_uint = 6654;
            stm_writef(stm, "Json from uint16_t: ");
            json_write(stm, &data_uint, NULL, uint16_t);
            stm_writef(stm, "\n");
        }

        /* Write integer as Json */
        {
            int16_t data_int = -567;
            stm_writef(stm, "Json from int16_t: ");
            json_write(stm, &data_int, NULL, int16_t);
            stm_writef(stm, "\n");
        }

        /* Write real32_t as Json */
        {
            real32_t data_real = 456.45f;
            stm_writef(stm, "Json from real32_t: ");
            json_write(stm, &data_real, NULL, real32_t);
            stm_writef(stm, "\n");
        }

        /* Write String as Json */
        {
            String *data_str = str_c("Hello World");
            stm_writef(stm, "Json from String: ");
            json_write(stm, data_str, NULL, String);
            stm_writef(stm, "\n");
            str_destroy(&data_str);
        }

        /* Write Image as Json (string b64) */
        {
            Pixbuf *pixbuf = pixbuf_create(2, 2, ekGRAY8);
            Image *data_image = NULL;
            bmem_set1(pixbuf_data(pixbuf), 2 * 2, 128);
            data_image = image_from_pixbuf(pixbuf, NULL);
            stm_writef(stm, "Json from Image: ");
            json_write(stm, data_image, NULL, Image);
            stm_writef(stm, "\n");
            pixbuf_destroy(&pixbuf);
            image_destroy(&data_image);
        }

        /* Write int array as Json */
        {
            ArrSt(int16_t) *array = arrst_create(int16_t);
            arrst_append(array, -321, int16_t);
            arrst_append(array, 12, int16_t);
            arrst_append(array, -8943, int16_t);
            arrst_append(array, 228, int16_t);
            arrst_append(array, -220, int16_t);
            arrst_append(array, 347, int16_t);
            stm_writef(stm, "Json from int array: ");
            json_write(stm, array, NULL, ArrSt(int16_t));
            stm_writef(stm, "\n");
            arrst_destroy(&array, NULL, int16_t);
        }

        /* Write string array as Json */
        {
            ArrPt(String) *array = arrpt_create(String);
            arrpt_append(array, str_c("Red"), String);
            arrpt_append(array, str_c("Green"), String);
            arrpt_append(array, str_c("Blue"), String);
            arrpt_append(array, str_c("Yellow"), String);
            arrpt_append(array, str_c("Orange"), String);
            stm_writef(stm, "Json from string array: ");
            json_write(stm, array, NULL, ArrPt(String));
            stm_writef(stm, "\n");
            arrpt_destroy(&array, str_destroy, String);
        }

        /* Write object as Json */
        {
            Products *products = heap_new(Products);
            products->size = 3;
            products->data = arrst_create(Product);

            {
                Product *product = arrst_new(products->data, Product);
                product->description = str_c("Intel i7-7700K");
                product->price = 329.99f;
            }

            {
                Product *product = arrst_new(products->data, Product);
                product->description = str_c("Ryzen-5-1600");
                product->price = 194.99f;
            }

            {
                Product *product = arrst_new(products->data, Product);
                product->description = str_c("GTX-1060");
                product->price = 449.99f;
            }

            stm_writef(stm, "Json from object: ");
            json_write(stm, products, NULL, Products);
            stm_writef(stm, "\n");
            dbind_destroy(&products, Products);
        }

        {
            String *str = stm_str(stm);
            bstd_printf("%s\n", tc(str));
            str_destroy(&str);
        }

        stm_close(&stm);
    }

    draw2d_finish();
    return 0;
}

Program output.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool_t from Json: 1
uint16_t from Json: 6654
int16_t from Json: -567
real32_t from Json: 456.450
String from Json: Hello World
Image from Json: width: 269 height: 400
ArrSt(int16_t) from Json: -321 12 -8943 228 -220 347
ArrPt(String) from Json: Red Green Blue Yellow Orange
Products object from Json: size 3
    Product: Intel i7-7700K Price 329.99
    Product: Ryzen-5-1600 Price 194.99
    Product: GTX-1060 Price 449.99

Json from bool_t: true
Json from uint16_t: 6654
Json from int16_t: -567
Json from real32_t: 456.450012
Json from String: "Hello World"
Json from Image: "iVBORw0KGgoAAAANSUhEUgAAAAI..."
Json from int array: [ -321, 12, -8943, 228, -220, 347 ]
Json from string array: [ "Red", "Green", "Blue", "Yellow", "Orange" ]
Json from object: {"size" : 3, "data" : [ {"description" : "Intel i7-7700K", "price" : 329.989990 }, {"description" : "Ryzen-5-1600", "price" : 194.990005 }, {"description" : "GTX-1060", "price" : 449.989990 } ] }
❮ Back
Next ❯