cJSON_stream_print.c 11.8 KB
Newer Older
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
/****************************************************************************
 * apps/netutils/json/cJSON_stream_print.c
 *
 * This file is a part of NuttX:
 *
 *   Copyright (c) 2011 Gregory Nutt. All rights reserved.
 *   Copyright (c) 2015 Haltian Ltd.
 *     Author: Jussi Kivilinna <jussi.kivilinna@haltian.com>
 *
 * And derives from the cJSON Project which has an MIT license:
 *
 *   Copyright (c) 2009 Dave Gamble
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
#include <unistd.h>

#include <apps/netutils/cJSON.h>

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#define cJSON_malloc malloc
#define cJSON_free free
#define cJSON_realloc realloc
#define cJSON_strdup strdup

/****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct cJSON_outstream_s
{
  void (*putc_fn)(char c, void *priv);
  void *fn_priv;
} cJSON_outstream;

struct putc_string_priv_s
{
  char *buf;
  size_t buflen;
  size_t numput;
};

/****************************************************************************
 * Private Data
 ****************************************************************************/

/****************************************************************************
 * Private Prototypes
 ****************************************************************************/

static void stream_print_value(cJSON *item, int depth, bool fmt,
                               cJSON_outstream *stream);
static void stream_print_array(cJSON *item, int depth, bool fmt,
                               cJSON_outstream *stream);
static void stream_print_object(cJSON *item, int depth, bool fmt,
                                cJSON_outstream *stream);

/****************************************************************************
 * Private Functions
 ****************************************************************************/

static inline void stream_putc(cJSON_outstream *stream, char c)
{
  stream->putc_fn(c, stream->fn_priv);
}

static inline void stream_puts(cJSON_outstream *stream, char *string)
{
  while (*string)
    {
      stream_putc(stream, *string++);
    }
}

107
108
109
110
111
112
113
static bool dbl_equal(double a, double b)
{
  double fabs_a = fabs(a);
  double fabs_b = fabs(b);
  return fabs(a - b) <= ((fabs_a > fabs_b ? fabs_b : fabs_a) * DBL_EPSILON);
}

114
115
116
117
118
119
120
121
122
123
124
125
/* Render the number nicely from the given item into a string. */

static void stream_print_number(cJSON *item, cJSON_outstream *stream)
{
  char str[32];
  double d = item->valuedouble;
  double id = item->valueint;
  size_t slen;
  int type;

  /* Get type and needed length output string. */

126
  if (item->valueint == 0 && d == 0.0)
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
      /* Handle zero separately as it's relation to DBL_EPSILON is special one
       * and as zero has two exact presentations in floating point format (positive
       * zero and negative zero). */

      if (copysign(1.0, d) > 0.0)
        {
          /* Simply zero. */

          str[0] = '0';
          str[1] = '\0';
          slen = 1;
          type = 3;
        }
      else
        {
          /* Negative zero. */

          slen = snprintf(str, sizeof(str), "%s", "-0.0");
          type = 2;
        }
    }
  else if (item->valueint != 0 && (d <= INT_MAX && d >= INT_MIN) &&
           dbl_equal(d, id))
    {
      /* Integer value can accurately represent this value. */

154
155
156
      slen = snprintf(str, sizeof(str), "%d", item->valueint);
      type = 1;
    }
157
  else if (fabs(d) < 1.0e-6 || fabs(d) > 1.0e14)
158
    {
159
160
161
      /* Double can represent 15 significant digits. */

      slen = snprintf(str, sizeof(str), "%.14e", d);
162
163
164
165
      type = -1;
    }
  else
    {
166
167
168
169
170
171
172
173
174
175
176
177
      int ndigits_before_dot = (int)log10(fabs(d));
      int ndigits_after_dot = 15 - ndigits_before_dot;
      char fmtstr[8];

      /* Double can represent 15 significant digits. */

      if (ndigits_after_dot < 0)
        ndigits_after_dot = 0;

      snprintf(fmtstr, sizeof(fmtstr), "%%.%df", ndigits_after_dot);

      slen = snprintf(str, sizeof(str), fmtstr, d);
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
      type = 0;
    }

  DEBUGASSERT(slen < sizeof(str) - 1);

  if (type == 0 && strchr(str, '.'))
    {
      /* Remove trailing zeros. */

      slen -= 1;

      while (slen > 0)
        {
          if (str[--slen] != '0')
            break;

          str[slen] = '\0';
        }

      /* Remove trailing dot. */

      if (slen > 0 && str[slen] == '.')
        str[slen] = '\0';
    }

  stream_puts(stream, str);
}

/* Render the cstring provided to an escaped version that can be printed. */

static void stream_print_string_ptr(const char *str, cJSON_outstream *stream)
{
  const char *ptr;
  unsigned char token;

  if (!str)
    {
      return;
    }

  ptr = str;
  stream_putc(stream, '\"');
  while (*ptr)
    {
      if ((unsigned char)*ptr > 31 && *ptr != '\"' && *ptr != '\\')
        {
          stream_putc(stream, *ptr++);
        }
      else
        {
          char tmp[6];

          stream_putc(stream, '\\');
          switch (token = *ptr++)
            {
            case '\\':
              stream_putc(stream, '\\');
              break;

            case '\"':
              stream_putc(stream, '\"');
              break;

            case '\b':
              stream_putc(stream, 'b');
              break;

            case '\f':
              stream_putc(stream, 'f');
              break;

            case '\n':
              stream_putc(stream, 'n');
              break;

            case '\r':
              stream_putc(stream, 'r');
              break;

            case '\t':
              stream_putc(stream, 't');
              break;

            default:
              /* Escape and print */

              snprintf(tmp, sizeof(tmp), "u%04x", token);
              stream_puts(stream, tmp);
              break;
            }
        }
    }

  stream_putc(stream, '\"');
}

/* Invote print_string_ptr (which is useful) on an item. */

static void stream_print_string(cJSON *item, cJSON_outstream *stream)
{
  stream_print_string_ptr(item->valuestring, stream);
}

/* Render a value to text. */

static void stream_print_value(cJSON *item, int depth, bool fmt,
                               cJSON_outstream *stream)
{
  if (!item)
    {
      return;
    }

  switch ((item->type) & 255)
    {
    case cJSON_NULL:
      stream_puts(stream, "null");
      break;

    case cJSON_False:
      stream_puts(stream, "false");
      break;

    case cJSON_True:
      stream_puts(stream, "true");
      break;

    case cJSON_Number:
      stream_print_number(item, stream);
      break;

    case cJSON_String:
      stream_print_string(item, stream);
      break;

    case cJSON_Array:
      stream_print_array(item, depth, fmt, stream);
      break;

    case cJSON_Object:
      stream_print_object(item, depth, fmt, stream);
      break;
    }
}

/* Render an array to text */

static void stream_print_array(cJSON *item, int depth, bool fmt,
                               cJSON_outstream *stream)
{
  cJSON *child;

  /* Compose the output array. */

  stream_putc(stream, '[');

  child = item->child;
  while (child)
    {
      /* Print all childs. */

      stream_print_value(child, depth + 1, fmt, stream);

      child = child->next;
      if (child)
        {
          stream_putc(stream, ',');
          if (fmt)
            {
              stream_putc(stream, ' ');
            }
        }
    }

  stream_putc(stream, ']');
}

/* Render an object to text. */

static void stream_print_object(cJSON *item, int depth, bool fmt,
                                cJSON_outstream *stream)
{
  cJSON *child;
  int j, i;

  /* Compose the output object. */

  stream_putc(stream, '{');
  if (fmt)
    {
      stream_putc(stream, '\n');
    }

  child = item->child;
  while (child)
    {
      if (fmt)
        {
          for (j = 0; j < depth; j++)
            {
              stream_putc(stream, '\t');
            }
        }

      /* Print object name. */

      stream_print_string_ptr(child->string, stream);

      stream_putc(stream, ':');
      if (fmt)
        {
          stream_putc(stream, '\t');
        }

      /* Print object value. */

      stream_print_value(child, depth + 1, fmt, stream);

      child = child->next;
      if (child)
        {
          stream_putc(stream, ',');
        }

      if (fmt)
        {
          stream_putc(stream, '\n');
        }
    }

  if (fmt)
    {
      for (i = 0; i < depth - 1; i++)
        {
          stream_putc(stream, '\t');
        }
    }

  stream_putc(stream, '}');
}

void putc_string(char c, void *_priv)
{
  struct putc_string_priv_s *priv = _priv;

  if (priv->numput >= priv->buflen)
    {
      priv->numput++;
      return;
    }

  priv->buf[priv->numput++] = c;
}

/* Render a cJSON item/entity/structure to text. */

static char *cJSON_Print_String(cJSON *item, bool formatted)
{
  struct putc_string_priv_s priv = {};
  size_t num_chars;
  char *out;

  priv.buf = NULL;
  priv.buflen = 0;
  priv.numput = 0;

  cJSON_Print_Stream(item, formatted, putc_string, &priv);

  num_chars = priv.numput;
  out = cJSON_malloc(num_chars + 1);
  if (!out)
    {
      return NULL;
    }

  priv.buf = out;
  priv.buflen = num_chars;
  priv.numput = 0;

  cJSON_Print_Stream(item, formatted, putc_string, &priv);

  DEBUGASSERT(priv.numput == num_chars);

  out[num_chars] = '\0';
  return out;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/* Render a cJSON item/entity/structure to stream function. */

void cJSON_Print_Stream(cJSON *item, bool formatted,
                        void (*putc_fn)(char c, void *priv), void *putc_priv)
{
  cJSON_outstream stream = {};

  stream.putc_fn = putc_fn;
  stream.fn_priv = putc_priv;

  stream_print_value(item, 0, formatted, &stream);
}

/* Render a cJSON item/entity/structure to newly allocated text string. */

char *cJSON_Print(cJSON *item)
{
  return cJSON_Print_String(item, true);
}

char *cJSON_PrintUnformatted(cJSON *item)
{
  return cJSON_Print_String(item, false);
}

/* Render a cJSON item/entity/structure to buffer. */

size_t cJSON_Print_Buf(cJSON *item, bool formatted, char *buf, size_t buflen)
{
  struct putc_string_priv_s priv = {};

  priv.buf = buf;
  priv.buflen = buflen;
  priv.numput = 0;

  cJSON_Print_Stream(item, formatted, putc_string, &priv);

  return priv.numput;
}