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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at>                 |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

#include "gate/encode/json.h"
#include "gate/results.h"
#include "gate/mathematics.h"

static char const gate_json_escaped_chars[] = { 'r',  'n',  't',  'b',  'f', '\"', '\\' };
static char const gate_json_unescaped_chars[] = { '\r', '\n', '\t', '\b', '\f', '\"', '\\' };

static char const gate_json_array_open = '[';
static char const gate_json_array_close = ']';
static char const gate_json_object_open = '{';
static char const gate_json_object_close = '}';
static char const gate_json_comma = ',';
static char const gate_json_colon = ':';
static char const gate_json_dblquote = '\"';
static char const gate_json_quote = '\'';
static char const gate_json_space = ' ';
static char const gate_json_escape_char = '\\';

static gate_string_t const gate_json_null = GATE_STRING_INIT_STATIC("null");
static gate_string_t const gate_json_true = GATE_STRING_INIT_STATIC("true");
static gate_string_t const gate_json_false = GATE_STRING_INIT_STATIC("false");


gate_result_t gate_json_escape_text(gate_string_t const* source, gate_string_t* result)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    char const* ptr;
    gate_size_t length;
    gate_size_t decoded;
    gate_char32_t current_char;
    gate_size_t pos;
    gate_size_t written;
    char tmp_chr;
    gate_strbuilder_t builder = GATE_INIT_EMPTY;
    char unicode_prefix[8];

    unicode_prefix[0] = gate_json_escape_char;
    unicode_prefix[1] = 'u';
    do
    {
        ptr = source->str;
        length = source->length;
        gate_strbuilder_create(&builder, length);

        ret = GATE_RESULT_OK;

        while (length != 0)
        {
            current_char = 0;
            decoded = gate_char_read_utf8(ptr, length, &current_char);
            if (decoded == 0)
            {
                ret = GATE_RESULT_INVALIDINPUT;
                break;
            }
            ptr += decoded;
            length -= decoded;
            written = 0;
            if (current_char >= 127)
            {
                /* non ASCII -> unicode */
                if (current_char > 65535)
                {
                    gate_str_print_hex_uint16(&unicode_prefix[2], sizeof(unicode_prefix) - 2, (gate_uint16_t)(current_char >> 16), false);
                    written += gate_strbuilder_append_text(&builder, unicode_prefix, 6);
                    current_char &= 0x0ffff;
                }
                gate_str_print_hex_uint16(&unicode_prefix[2], sizeof(unicode_prefix) - 2, (gate_uint16_t)current_char, false);
                written += gate_strbuilder_append_text(&builder, unicode_prefix, 6);
            }
            else if ((current_char < '0') || (current_char == '\\'))
            {
                /* outside of alphanumeric space */
                tmp_chr = (char)current_char;
                pos = gate_str_char_pos(gate_json_unescaped_chars, sizeof(gate_json_unescaped_chars), tmp_chr, 0);
                if (pos != GATE_STR_NPOS)
                {
                    /* char with special escape code */
                    written += gate_strbuilder_append_text(&builder, &gate_json_escape_char, 1);
                    written += gate_strbuilder_append_text(&builder, &gate_json_escaped_chars[pos], 1);
                }
                else if (current_char < 32)
                {
                    /* control character -> encode as hex */
                    gate_str_print_hex_uint16(&unicode_prefix[2], sizeof(unicode_prefix) - 2, (gate_uint16_t)current_char, false);
                    written += gate_strbuilder_append_text(&builder, unicode_prefix, 6);
                }
                else
                {
                    /* printable non-letter character like '$' or '%' */
                    written += gate_strbuilder_append_text(&builder, &tmp_chr, 1);
                }
            }
            else
            {
                /* inside of alphanumeric space */
                tmp_chr = (char)current_char;
                written += gate_strbuilder_append_text(&builder, &tmp_chr, 1);
            }

            if (written == 0)
            {
                ret = GATE_RESULT_OUTOFMEMORY;
                break;
            }
        }

        if (GATE_SUCCEEDED(ret))
        {
            if (NULL == gate_strbuilder_to_string(&builder, result))
            {
                ret = GATE_RESULT_OUTOFMEMORY;
            }
        }
    } while (0);

    gate_strbuilder_release(&builder);
    return ret;
}
gate_result_t gate_json_unescape_text(gate_string_t const* source, gate_string_t* result)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_strbuilder_t builder = GATE_INIT_EMPTY;
    char const* ptr;
    gate_size_t length;
    gate_size_t pos;
    gate_size_t written;
    gate_uint64_t hex_chr;
    gate_size_t encoded;
    char unicode_buffer[8];

    do
    {
        ptr = source->str;
        length = source->length;

        gate_strbuilder_create(&builder, length);

        ret = GATE_RESULT_OK;
        while (length != 0)
        {
            written = 0;
            if (*ptr != gate_json_escape_char)
            {
                /* regular character */
                gate_strbuilder_append_text(&builder, ptr, 1);
                ++ptr;
                --length;
            }
            else
            {
                /* escaped character: */
                if (--length == 0)
                {
                    ret = GATE_RESULT_INVALIDINPUT;
                    break;
                }
                ++ptr;
                pos = gate_str_char_pos(gate_json_escaped_chars, sizeof(gate_json_escaped_chars), *ptr, 0);
                if (GATE_STR_NPOS != pos)
                {
                    /* write known unescaped character */
                    written = gate_strbuilder_append_text(&builder, &gate_json_unescaped_chars[pos], 1);
                }
                else
                {
                    /* unicode or unknown escaped character */
                    if (*ptr == 'u')
                    {
                        /* unicode char */
                        if (length < 5)
                        {
                            ret = GATE_RESULT_INVALIDINPUT;
                            break;
                        }
                        if (4 != gate_str_parse_hex_int(ptr + 1, 4, &hex_chr))
                        {
                            ret = GATE_RESULT_INVALIDINPUT;
                            break;
                        }
                        encoded = gate_char_write_utf8((gate_char32_t)hex_chr, unicode_buffer, sizeof(unicode_buffer));
                        written += gate_strbuilder_append_text(&builder, unicode_buffer, encoded);
                        ptr += 5;
                        length -= length;
                    }
                    else
                    {
                        /* unknown escape code -> just copy it to destination string */
                        written += gate_strbuilder_append_text(&builder, &gate_json_escape_char, 1);
                        written += gate_strbuilder_append_text(&builder, ptr, 1);
                        ++ptr;
                        --length;
                    }
                }
            }
        } /* while(length != 0) */

        if (GATE_SUCCEEDED(ret))
        {
            if (NULL == gate_strbuilder_to_string(&builder, result))
            {
                ret = GATE_RESULT_OUTOFMEMORY;
            }
        }

    } while (0);

    gate_strbuilder_release(&builder);
    return ret;
}


static gate_result_t gate_json_parse_text(gate_string_t const* source, gate_string_t* dest, gate_size_t* chars_decoded)
{
    gate_result_t ret = GATE_RESULT_NOMATCH;	/* end-char not found*/
    char const* ptr = source->str;
    gate_size_t length = source->length;
    char end_char = ptr[0];
    gate_bool_t is_escaped = false;
    gate_string_t content = GATE_STRING_INIT_EMPTY;
    gate_size_t decoded = 0;

    do
    {
        if (length == 0)
        {
            ret = GATE_RESULT_INVALIDDATA;
            break;
        }

        decoded = 1;
        ++ptr;
        --length;
        for (; length != 0; ++ptr, --length)
        {
            ++decoded;
            if (*ptr == gate_json_escape_char)
            {
                is_escaped = !is_escaped;
            }
            else
            {
                if (*ptr == end_char)
                {
                    if (!is_escaped)
                    {
                        /* end of string reached*/
                        if (NULL == gate_string_substr(&content, source, 1, decoded - 2))
                        {
                            ret = GATE_RESULT_OUTOFMEMORY;
                        }
                        else
                        {
                            ret = gate_json_unescape_text(&content, dest);
                        }
                        break;
                    }
                }
                is_escaped = false;
            }
        }


    } while (0);



    if (chars_decoded != NULL)
    {
        *chars_decoded = decoded;
    }

    gate_string_release(&content);

    return ret;
}

static gate_bool_t gate_json_is_numeric(gate_string_t const* text)
{
    gate_bool_t ret = false;
    char const* ptr = text->str;
    gate_size_t length = text->length;

    do
    {
        if (length == 0)
        {
            break;
        }
        if ((*ptr == '+') || (*ptr == '-'))
        {
            ++ptr;
            --length;
            if (length == 0)
            {
                break;
            }
        }

        ret = gate_char_is_digit(*ptr);

    } while (0);
    return ret;
}

static gate_size_t gate_json_ltrim(gate_string_t* text)
{
    gate_size_t trimmed = 0;
    gate_string_t new_text = GATE_STRING_INIT_EMPTY;
    if (NULL != gate_string_ltrim(&new_text, text))
    {
        trimmed = new_text.str - text->str;
        gate_string_release(text);
        gate_string_duplicate(text, &new_text);
        gate_string_release(&new_text);
    }
    return trimmed;
}
static gate_size_t gate_json_discard_chars(gate_string_t* text, gate_size_t char_count)
{
    gate_size_t ret = 0;
    gate_string_t tmp = GATE_STRING_INIT_EMPTY;
    gate_size_t remove_length = gate_string_length(text);
    if (char_count < remove_length)
    {
        remove_length = char_count;
    }
    if (NULL != gate_string_substr(&tmp, text, remove_length, GATE_STR_NPOS))
    {
        gate_string_release(text);
        gate_string_duplicate(text, &tmp);
        gate_string_release(&tmp);
        ret = remove_length;
    }
    return ret;
}

static gate_result_t gate_json_parse_array(gate_string_t const* source, gate_property_t* target_prop, gate_json_result_t* result)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_string_t content = GATE_STRING_INIT_EMPTY;
    gate_size_t used = 0;
    gate_property_t item;
    gate_json_result_t item_result;
    gate_bool_t succeeded;

    do
    {
        if (!gate_string_starts_with_char(source, gate_json_array_open))
        {
            ret = GATE_RESULT_INVALIDCONTENT;
            break;
        }

        if (NULL == gate_string_substr(&content, source, 1, gate_string_length(source) - 1))
        {
            ret = GATE_RESULT_OUTOFMEMORY;
            break;
        }
        used += 1;

        do
        {
            used += gate_json_ltrim(&content);
            if (gate_string_is_empty(&content))
            {
                ret = GATE_RESULT_INVALIDCONTENT;
                break;
            }

            if (gate_string_starts_with_char(&content, gate_json_array_close))
            {
                /* end of array reached */
                ++used;
                ret = GATE_RESULT_OK;
                break;
            }

            ret = gate_json_parse_string(&content, &item, &item_result);
            if (GATE_FAILED(ret))
            {
                break;
            }

            used += item_result.chars_processed;
            succeeded = (NULL != gate_property_array_add(target_prop, &item));
            gate_property_destroy(&item);
            if (!succeeded)
            {
                ret = GATE_RESULT_OUTOFMEMORY;
                break;
            }
            gate_json_discard_chars(&content, item_result.chars_processed);

            used += gate_json_ltrim(&content);

            if (gate_string_starts_with_char(&content, gate_json_array_close))
            {
                /* end of array reached */
                ++used;
                ret = GATE_RESULT_OK;
                break;
            }

            if (!gate_string_starts_with_char(&content, gate_json_comma))
            {
                ret = GATE_RESULT_INVALIDCONTENT;
                break;
            }
            used += gate_json_discard_chars(&content, 1);
        } while (GATE_SUCCEEDED(ret));

    } while (0);

    gate_string_release(&content);
    if (result != NULL)
    {
        result->succeeded = GATE_SUCCEEDED(ret);
        result->chars_processed = used;
    }
    return ret;
}


static gate_result_t gate_json_parse_object(gate_string_t const* source, gate_property_t* target_prop, gate_json_result_t* result)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_size_t used = 0;
    gate_string_t content = GATE_STRING_INIT_EMPTY;
    gate_string_t prop_name = GATE_STRING_INIT_EMPTY;
    gate_property_t prop_value = GATE_INIT_EMPTY;
    gate_size_t item_chars_decoded;
    gate_json_result_t prop_result = GATE_INIT_EMPTY;

    do
    {
        if (!gate_string_starts_with_char(source, gate_json_object_open))
        {
            ret = GATE_RESULT_INVALIDCONTENT;
            break;
        }

        if (NULL == gate_string_substr(&content, source, 1, gate_string_length(source) - 1))
        {
            ret = GATE_RESULT_OUTOFMEMORY;
            break;
        }
        used += 1;

        do
        {
            used += gate_json_ltrim(&content);

            if (gate_string_starts_with_char(&content, gate_json_object_close))
            {
                /* end of array reached */
                ++used;
                ret = GATE_RESULT_OK;
                break;
            }

            item_chars_decoded = 0;
            ret = gate_json_parse_text(&content, &prop_name, &item_chars_decoded);
            if (GATE_FAILED(ret))
            {
                break;
            }
            gate_json_discard_chars(&content, item_chars_decoded);
            used += item_chars_decoded;

            used += gate_json_ltrim(&content);
            if (!gate_string_starts_with_char(&content, gate_json_colon))
            {
                ret = GATE_RESULT_INVALIDCONTENT;
                break;
            }

            used += gate_json_discard_chars(&content, 1);

            used += gate_json_ltrim(&content);

            ret = gate_json_parse_string(&content, &prop_value, &prop_result);
            if (GATE_FAILED(ret))
            {
                /* parsing failed */
                break;
            }
            if (NULL == gate_property_member_add(target_prop, &prop_name, &prop_value))
            {
                /* failed to add property as member to target (propably allocation trouble) */
                ret = GATE_RESULT_OUTOFMEMORY;
                break;
            }

            gate_json_discard_chars(&content, prop_result.chars_processed);
            used += prop_result.chars_processed;

            gate_string_release(&prop_name);
            gate_property_destroy(&prop_value);

            used += gate_json_ltrim(&content);

            if (gate_string_starts_with_char(&content, gate_json_object_close))
            {
                /* end of object reached */
                ++used;
                ret = GATE_RESULT_OK;
                break;
            }

            if (!gate_string_starts_with_char(&content, gate_json_comma))
            {
                ret = GATE_RESULT_INVALIDCONTENT;
                break;
            }
            used += gate_json_discard_chars(&content, 1);
            /* ready to parse next member entry */
        } while (GATE_SUCCEEDED(ret));

    } while (0);

    if (result != NULL)
    {
        result->succeeded = GATE_SUCCEEDED(ret);
        result->chars_processed = used;
    }

    gate_string_release(&prop_name);
    gate_property_destroy(&prop_value);
    gate_string_release(&content);

    return ret;
}


gate_result_t gate_json_parse_string(gate_string_t const* source, gate_property_t* prop, gate_json_result_t* result)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_size_t str_used = 0;
    gate_string_t json = GATE_STRING_INIT_EMPTY;
    gate_string_t tmp_str = GATE_STRING_INIT_EMPTY;
    gate_size_t chars_decoded = 0;
    gate_size_t chars_decoded2 = 0;
    gate_json_result_t result_entry = GATE_INIT_EMPTY;
    gate_int64_t i64 = 0;
    gate_real64_t r64 = 0.0;

    do
    {
        if (NULL == gate_string_clone(&json, source))
        {
            ret = GATE_RESULT_OUTOFMEMORY;
            break;
        }

        str_used += gate_json_ltrim(&json);

        if (gate_string_starts_with(&json, &gate_json_null))
        {
            /* NULL entry */
            if (NULL == gate_property_create_empty(prop))
            {
                ret = GATE_RESULT_OUTOFMEMORY;
            }
            else
            {
                str_used += gate_string_length(&gate_json_null);
                ret = GATE_RESULT_OK;
            }
        }
        else if (gate_string_starts_with(&json, &gate_json_true))
        {
            /* BOOLEAN TRUE entry */
            if (NULL == gate_property_create_bool(prop, true))
            {
                ret = GATE_RESULT_OUTOFMEMORY;
            }
            else
            {
                str_used += gate_string_length(&gate_json_true);
                ret = GATE_RESULT_OK;
            }
        }
        else if (gate_string_starts_with(&json, &gate_json_false))
        {
            /* BOOLEAN FALSE entry */
            if (NULL == gate_property_create_bool(prop, false))
            {
                ret = GATE_RESULT_OUTOFMEMORY;
            }
            else
            {
                str_used += gate_string_length(&gate_json_false);
                ret = GATE_RESULT_OK;
            }
        }
        else if (gate_string_starts_with_char(&json, gate_json_dblquote) || gate_string_starts_with_char(&json, gate_json_quote))
        {
            /* STRING entry */
            ret = gate_json_parse_text(&json, &tmp_str, &chars_decoded);
            if (GATE_SUCCEEDED(ret))
            {
                str_used += chars_decoded;
                if (NULL == gate_property_create_string(prop, &tmp_str))
                {
                    ret = GATE_RESULT_OUTOFMEMORY;
                }
            }
        }
        else if (gate_string_starts_with_char(&json, gate_json_array_open))
        {
            /* ARRAY entry */
            if (NULL == gate_property_create_array(prop, NULL, 0))
            {
                ret = GATE_RESULT_OUTOFMEMORY;
            }
            else
            {
                ret = gate_json_parse_array(&json, prop, &result_entry);
                if (GATE_SUCCEEDED(ret))
                {
                    str_used += result_entry.chars_processed;
                }
            }
        }
        else if (gate_string_starts_with_char(&json, gate_json_object_open))
        {
            /* OBJECT entry */
            if (NULL == gate_property_create_object(prop))
            {
                ret = GATE_RESULT_OUTOFMEMORY;
            }
            else
            {
                ret = gate_json_parse_object(&json, prop, &result_entry);
                if (GATE_SUCCEEDED(ret))
                {
                    str_used += result_entry.chars_processed;
                }
            }
        }
        else if (gate_json_is_numeric(&json))
        {
            /* NUMBER entry */
            chars_decoded = gate_str_parse_int64(json.str, json.length, &i64);
            chars_decoded2 = gate_str_parse_real(json.str, json.length, &r64);
            if (chars_decoded2 == 0)
            {
                ret = GATE_RESULT_INVALIDDATA;
            }
            else if (chars_decoded2 > chars_decoded)
            {
                /* seems to be a REAL value */
                str_used = chars_decoded2;
                ret = (NULL == gate_property_create_real(prop, r64)) ? GATE_RESULT_OUTOFMEMORY : GATE_RESULT_OK;
            }
            else
            {
                /* seems to be an INT value */
                str_used = chars_decoded;
                ret = (NULL == gate_property_create_int(prop, i64)) ? GATE_RESULT_OUTOFMEMORY : GATE_RESULT_OK;
            }
        }
        else
        {
            /* unsupported or invalid data */
            ret = GATE_RESULT_INVALIDDATA;
        }

    } while (0);

    gate_string_release(&json);
    gate_string_release(&tmp_str);

    if (result != NULL)
    {
        result->succeeded = GATE_SUCCEEDED(ret);
        result->chars_processed = str_used;
    }

    return ret;
}

static gate_result_t gate_json_builder(gate_property_t const* prop, gate_strbuilder_t* dest_builder,
    gate_uint32_t indent, gate_uint32_t indent_add, gate_bool_t is_member)
{
    gate_result_t ret = GATE_RESULT_OK;
    gate_uint32_t prop_type = gate_property_get_type(prop);
    gate_size_t char_count = 0;
    gate_bool_t bool_value = false;
    gate_int64_t int_value = 0;
    gate_real64_t real_value = 0.0;
    gate_string_t string_value = GATE_STRING_INIT_EMPTY;
    gate_string_t text = GATE_STRING_INIT_EMPTY;
    gate_size_t index = 0, count = 0;
    gate_property_t const* ptr_item = NULL;
    gate_array_t names = GATE_INIT_EMPTY;
    gate_string_t const* ptr_name = NULL;

    if ((indent != 0) && !is_member)
    {
        char_count += gate_strbuilder_append_chars(dest_builder, indent, ' ');
    }

    switch (prop_type)
    {
    case GATE_PROPERTY_TYPE_EMPTY:
    {
        char_count += gate_strbuilder_append_string(dest_builder, &gate_json_null);
        break;
    }
    case GATE_PROPERTY_TYPE_BOOL:
    {
        gate_property_get_bool(prop, &bool_value);
        if (bool_value)
        {
            char_count += gate_strbuilder_append_string(dest_builder, &gate_json_true);
        }
        else
        {
            char_count += gate_strbuilder_append_string(dest_builder, &gate_json_false);
        }
        break;
    }
    case GATE_PROPERTY_TYPE_INT:
    {
        gate_property_get_int(prop, &int_value);
        char_count += gate_strbuilder_append_int64(dest_builder, int_value);
        break;
    }
    case GATE_PROPERTY_TYPE_REAL:
    {
        gate_property_get_real(prop, &real_value);
        char_count += gate_strbuilder_append_real(dest_builder, real_value, 0, gate_math_decimal_length(real_value), 0);
        break;
    }
    case GATE_PROPERTY_TYPE_STRING:
    {
        gate_property_get_string(prop, &string_value);
        gate_json_escape_text(&string_value, &text);
        char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_dblquote);
        char_count += gate_strbuilder_append_string(dest_builder, &text);
        char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_dblquote);
        break;
    }
    case GATE_PROPERTY_TYPE_ARRAY:
    {
        if (is_member)
        {
            char_count += gate_strbuilder_append_text(dest_builder, GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH);
            /*indent += indent_add;*/
            char_count += gate_strbuilder_append_chars(dest_builder, indent, gate_json_space);
        }
        char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_array_open);
        count = gate_property_array_length(prop);
        if (count != 0)
        {
            for (index = 0; index != count; ++index)
            {
                if (indent_add != 0)
                {
                    char_count += gate_strbuilder_append_text(dest_builder, GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH);
                }
                ptr_item = gate_property_array_get(prop, index);
                ret = gate_json_builder(ptr_item, dest_builder, indent + indent_add, indent_add, false);
                if (GATE_FAILED(ret))
                {
                    break;
                }
                if (index + 1 < count)
                {
                    char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_comma);
                }
            }
            if (indent_add != 0)
            {
                char_count += gate_strbuilder_append_text(dest_builder, GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH);
            }
            if (indent != 0)
            {
                char_count += gate_strbuilder_append_chars(dest_builder, indent, gate_json_space);
            }
        }
        char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_array_close);
        break;
    }
    case GATE_PROPERTY_TYPE_OBJECT:
    {
        if (is_member)
        {
            char_count += gate_strbuilder_append_text(dest_builder, GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH);
            /*indent += indent_add;*/
            char_count += gate_strbuilder_append_chars(dest_builder, indent, gate_json_space);
        }
        char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_object_open);
        if (NULL != gate_property_member_names(prop, &names))
        {
            count = gate_array_length(&names);
            if (count != 0)
            {
                for (index = 0; index != count; ++index)
                {
                    if (indent_add != 0)
                    {
                        char_count += gate_strbuilder_append_text(dest_builder, GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH);
                        char_count += gate_strbuilder_append_chars(dest_builder,
                            (gate_size_t)indent + (gate_size_t)indent_add,
                            gate_json_space);
                    }
                    char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_dblquote);
                    ptr_name = (gate_string_t const*)gate_array_get(&names, index);
                    if (ptr_name)
                    {
                        gate_strbuilder_append_string(dest_builder, ptr_name);
                    }
                    char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_dblquote);
                    char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_colon);
                    char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_space);

                    ptr_item = gate_property_member_get(prop, ptr_name);

                    ret = gate_json_builder(ptr_item, dest_builder, indent + indent_add, indent_add, true);
                    if (GATE_FAILED(ret))
                    {
                        break;
                    }

                    if (index + 1 < count)
                    {
                        char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_comma);
                    }
                }
                if (indent_add != 0)
                {
                    char_count += gate_strbuilder_append_text(dest_builder, GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH);
                }
                if (indent != 0)
                {
                    char_count += gate_strbuilder_append_chars(dest_builder, indent, gate_json_space);
                }
            }
        }
        char_count += gate_strbuilder_append_chars(dest_builder, 1, gate_json_object_close);
        break;
    }
    default:
    {
        ret = GATE_RESULT_NOTSUPPORTED;
        break;
    }
    }

    gate_array_release(&names);
    gate_string_release(&string_value);
    gate_string_release(&text);

    if (GATE_SUCCEEDED(ret) && (char_count == 0))
    {
        ret = GATE_RESULT_OUTOFMEMORY;
    }

    return ret;
}


gate_result_t gate_json_build_string(gate_property_t const* prop, gate_strbuilder_t* dest_builder, gate_uint32_t line_indent)
{
    return gate_json_builder(prop, dest_builder, 0, line_indent, false);
}

gate_result_t gate_json_parse(gate_stream_t* source, gate_property_t* target_prop, gate_json_result_t* result)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_stringstream_t* str_stream = NULL;
    gate_string_t str = GATE_STRING_INIT_EMPTY;
    do
    {
        str_stream = gate_stringstream_create(256);
        if (str_stream == NULL)
        {
            ret = GATE_RESULT_OUTOFMEMORY;
            break;
        }
        ret = gate_stream_transfer(source, (gate_stream_t*)str_stream);
        GATE_BREAK_IF_FAILED(ret);

        ret = gate_stringstream_get_current_view(str_stream, &str);
        GATE_BREAK_IF_FAILED(ret);

        ret = gate_json_parse_string(&str, target_prop, result);
    } while (0);

    gate_string_release(&str);

    if (str_stream)
    {
        gate_object_release(str_stream);
    }
    return ret;
}

gate_result_t gate_json_build(gate_property_t const* prop, gate_stream_t* dest, gate_uint32_t line_indent)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_strbuilder_t builder = GATE_INIT_EMPTY;
    gate_size_t written = 0;

    do
    {
        gate_strbuilder_create(&builder, 256);
        ret = gate_json_build_string(prop, &builder, line_indent);
        GATE_BREAK_IF_FAILED(ret);

        ret = gate_stream_write_block(dest, gate_strbuilder_ptr(&builder, 0), gate_strbuilder_length(&builder), &written);
    } while (0);

    gate_strbuilder_release(&builder);

    return ret;
}