forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.c
1080 lines (897 loc) · 23.2 KB
/
json.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
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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include "stdlib.h"
#include "str.h"
#include "value.h"
#include "json.h"
#include "malloc.h"
#include "unicode.h"
static opa_value *opa_json_parse_token(opa_json_lex *ctx, int token);
int opa_json_lex_offset(opa_json_lex *ctx)
{
return ctx->curr - ctx->input;
}
int opa_json_lex_remaining(opa_json_lex *ctx)
{
return ctx->len - opa_json_lex_offset(ctx);
}
int opa_json_lex_eof(opa_json_lex *ctx)
{
return (ctx->curr - ctx->input) >= ctx->len;
}
int opa_json_lex_read_atom(opa_json_lex *ctx, const char *str, int n, int token)
{
if (opa_json_lex_remaining(ctx) >= n)
{
if (opa_strncmp(str, ctx->curr, n) == 0)
{
ctx->curr += n;
return token;
}
}
return OPA_JSON_TOKEN_ERROR;
}
void opa_json_lex_read_digits(opa_json_lex *ctx)
{
while (!opa_json_lex_eof(ctx) && opa_isdigit(*ctx->curr))
{
ctx->curr++;
}
}
int opa_json_lex_read_unicode(opa_json_lex *ctx)
{
if (opa_json_lex_remaining(ctx) >= 4)
{
for (int i = 0; i < 4; i++)
{
if (!opa_ishex(ctx->curr[i]))
{
return -1;
}
}
ctx->curr += 4;
return 0;
}
return 1;
}
int opa_json_lex_read_number(opa_json_lex *ctx)
{
ctx->buf = ctx->curr;
// Handle sign component.
if (*ctx->curr == '-')
{
ctx->curr++;
if (opa_json_lex_eof(ctx))
{
goto err;
}
}
// Handle integer component.
if (*ctx->curr == '0')
{
ctx->curr++;
}
else if (opa_isdigit(*ctx->curr))
{
opa_json_lex_read_digits(ctx);
}
else
{
goto err;
}
if (opa_json_lex_eof(ctx))
{
goto out;
}
// Handle fraction component.
if (*ctx->curr == '.')
{
ctx->curr++;
opa_json_lex_read_digits(ctx);
if (opa_json_lex_eof(ctx))
{
goto out;
}
}
// Handle exponent component.
if (*ctx->curr == 'e' || *ctx->curr == 'E')
{
ctx->curr++;
if (opa_json_lex_eof(ctx))
{
goto err;
}
if (*ctx->curr == '+' || *ctx->curr == '-')
{
ctx->curr++;
if (opa_json_lex_eof(ctx))
{
goto err;
}
}
opa_json_lex_read_digits(ctx);
}
out:
ctx->buf_end = ctx->curr;
return OPA_JSON_TOKEN_NUMBER;
err:
return OPA_JSON_TOKEN_ERROR;
}
int opa_json_lex_read_string(opa_json_lex *ctx)
{
if (*ctx->curr != '"')
{
goto err;
}
ctx->buf = ++ctx->curr;
int escaped = 0;
while (1)
{
if (opa_json_lex_eof(ctx))
{
goto err;
}
unsigned char b = *ctx->curr;
switch (b)
{
case '\\':
escaped = 1;
ctx->curr++;
if (opa_json_lex_eof(ctx))
{
goto err;
}
b = *ctx->curr;
switch (b)
{
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
ctx->curr++;
break;
case 'u':
ctx->curr++;
if (opa_json_lex_read_unicode(ctx) != 0)
{
goto err;
}
break;
default:
goto err;
}
break;
case '"':
goto out;
default:
if (b < ' ') {
goto err;
}
if (b > '~')
{
// Revert to slow path to validate UTF-8 encoding.
escaped = 1;
}
ctx->curr++;
break;
}
}
out:
ctx->buf_end = ctx->curr++;
if (escaped)
{
return OPA_JSON_TOKEN_STRING_ESCAPED;
}
return OPA_JSON_TOKEN_STRING;
err:
return OPA_JSON_TOKEN_ERROR;
}
int opa_json_lex_read_empty_set(opa_json_lex *ctx)
{
if (!ctx->set_literals_enabled)
{
return OPA_JSON_TOKEN_ERROR;
}
int token = opa_json_lex_read_atom(ctx, "set(", 4, OPA_JSON_TOKEN_EMPTY_SET);
if (token != OPA_JSON_TOKEN_EMPTY_SET)
{
return OPA_JSON_TOKEN_ERROR;
}
while (opa_isspace(*ctx->curr))
{
ctx->curr++;
}
if (*ctx->curr != ')')
{
return OPA_JSON_TOKEN_ERROR;
}
ctx->curr++;
return token;
}
int opa_json_lex_read(opa_json_lex *ctx)
{
while (!opa_json_lex_eof(ctx))
{
char b = *ctx->curr;
switch (b)
{
case 'n':
return opa_json_lex_read_atom(ctx, "null", 4, OPA_JSON_TOKEN_NULL);
case 't':
return opa_json_lex_read_atom(ctx, "true", 4, OPA_JSON_TOKEN_TRUE);
case 'f':
return opa_json_lex_read_atom(ctx, "false", 5, OPA_JSON_TOKEN_FALSE);
case 's':
return opa_json_lex_read_empty_set(ctx);
case '"':
return opa_json_lex_read_string(ctx);
case '{':
ctx->curr++;
return OPA_JSON_TOKEN_OBJECT_START;
case '}':
ctx->curr++;
return OPA_JSON_TOKEN_OBJECT_END;
case '[':
ctx->curr++;
return OPA_JSON_TOKEN_ARRAY_START;
case ']':
ctx->curr++;
return OPA_JSON_TOKEN_ARRAY_END;
case ',':
ctx->curr++;
return OPA_JSON_TOKEN_COMMA;
case ':':
ctx->curr++;
return OPA_JSON_TOKEN_COLON;
default:
if (opa_isdigit(b) || b == '-')
{
return opa_json_lex_read_number(ctx);
}
else if (opa_isspace(b))
{
ctx->curr++;
continue;
}
return OPA_JSON_TOKEN_ERROR;
}
}
return OPA_JSON_TOKEN_EOF;
}
void opa_json_lex_init(const char *input, size_t len, opa_json_lex *ctx)
{
ctx->input = input;
ctx->len = len;
ctx->curr = input;
ctx->buf = NULL;
ctx->buf_end = NULL;
ctx->set_literals_enabled = 0;
}
size_t opa_json_max_string_len(const char *buf, size_t len)
{
// The lexer will catch invalid escaping, e.g., if the last char in the
// buffer is reverse solidus this will be caught ahead-of-time.
int skip = 0;
for (int i = 0; i < len; i++)
{
if (buf[i] == '\\')
{
int codepoint;
codepoint = opa_unicode_decode_unit(buf, i, len);
if (codepoint == -1) {
// If not a codepoint \uXXXX, must be a single
// character escaping.
skip++;
i++;
continue;
}
i += 5;
// Assume each UTF-16 encoded character to take full 4
// bytes when encoded as UTF-8. However, if encoded as a
// surrogate pair, it's split to two 2 bytes.
if (!opa_unicode_surrogate(codepoint)) {
skip += 2;
continue;
}
skip += 4;
}
}
return len - skip;
}
opa_value *opa_json_parse_string(int token, const char *buf, int len)
{
if (token == OPA_JSON_TOKEN_STRING)
{
char *cpy = (char *)opa_malloc(len);
for (int i = 0; i < len; i++)
{
cpy[i] = buf[i];
}
return opa_string_allocated(cpy, len);
}
int max_len = opa_json_max_string_len(buf, len);
char *cpy = (char *)opa_malloc(max_len);
char *out = cpy;
for (int i = 0; i < len;)
{
unsigned char c = buf[i];
if (c != '\\')
{
if (c < ' ' || c == '"')
{
opa_abort("illegal unescaped character");
}
if (c < 0x80)
{
*out++ = c;
i++;
} else {
int n;
int cp = opa_unicode_decode_utf8(buf, i, len, &n);
if (cp == -1)
{
opa_abort("illegal utf-8");
}
i += n;
n = opa_unicode_encode_utf8(cp, out);
out += n;
}
continue;
}
char next = buf[i+1];
switch (next)
{
case '"':
case '\\':
case '/':
*out++ = next;
i += 2;
break;
case 'b':
*out++ = '\b';
i += 2;
break;
case 'f':
*out++ = '\f';
i += 2;
break;
case 'n':
*out++ = '\n';
i += 2;
break;
case 'r':
*out++ = '\r';
i += 2;
break;
case 't':
*out++ = '\t';
i += 2;
break;
case 'u':
{
// JSON encodes unicode characters as UTF-16 that
// have either a single or two code units. If two
// code units, the character is represented as a
// pair of UTF-16 surrogates. Surrogates don't
// overlap with characters that can be encoded as
// a single value.
int u = opa_unicode_decode_unit(buf, i, len);
if (u == -1) {
opa_abort("illegal string escape character");
}
i += 6;
if (opa_unicode_surrogate(u)) {
int v = opa_unicode_decode_unit(buf, i, len);
if (v == -1) {
opa_abort("illegal string escape character");
}
u = opa_unicode_decode_surrogate(u, v);
i += 6;
}
out += opa_unicode_encode_utf8(u, out);
break;
}
default:
// this is unreachable.
opa_abort("illegal string escape character");
}
}
return opa_string_allocated(cpy, out-cpy);
}
opa_value *opa_json_parse_number(const char *buf, int len)
{
char *cpy = (char *)opa_malloc(len);
for (int i = 0; i < len; i++)
{
cpy[i] = buf[i];
}
return opa_number_ref_allocated(cpy, len);
}
opa_value *opa_json_parse_array(opa_json_lex *ctx)
{
opa_value *ret = opa_array();
opa_array_t *arr = opa_cast_array(ret);
int sep = 0;
while (1)
{
int token = opa_json_lex_read(ctx);
switch (token)
{
case OPA_JSON_TOKEN_ARRAY_END:
return ret;
case OPA_JSON_TOKEN_COMMA:
if (sep)
{
sep = 0;
continue;
}
}
opa_value *elem = opa_json_parse_token(ctx, token);
if (elem == NULL)
{
return NULL;
}
opa_array_append(arr, elem);
sep = 1;
}
}
opa_value *opa_json_parse_set(opa_json_lex *ctx, opa_value *elem, int token)
{
if (!ctx->set_literals_enabled)
{
return NULL;
}
opa_set_t *set = opa_cast_set(opa_set());
opa_set_add(set, elem);
if (token == OPA_JSON_TOKEN_OBJECT_END)
{
return &set->hdr;
}
token = opa_json_lex_read(ctx);
while (1)
{
elem = opa_json_parse_token(ctx, token);
if (elem == NULL)
{
return NULL;
}
opa_set_add(set, elem);
token = opa_json_lex_read(ctx);
switch (token)
{
case OPA_JSON_TOKEN_COMMA:
token = opa_json_lex_read(ctx);
break;
case OPA_JSON_TOKEN_OBJECT_END:
return &set->hdr;
default:
return NULL;
}
}
return NULL;
}
opa_value *opa_json_parse_object(opa_json_lex *ctx, opa_value *key)
{
int token = opa_json_lex_read(ctx);
opa_value *val = opa_json_parse_token(ctx, token);
if (val == NULL)
{
return NULL;
}
opa_object_t *obj = opa_cast_object(opa_object());
opa_object_insert(obj, key, val);
token = opa_json_lex_read(ctx);
switch (token)
{
case OPA_JSON_TOKEN_OBJECT_END:
return &obj->hdr;
case OPA_JSON_TOKEN_COMMA:
break;
default:
return NULL;
}
token = opa_json_lex_read(ctx);
while (1)
{
key = opa_json_parse_token(ctx, token);
if (key == NULL)
{
return NULL;
}
token = opa_json_lex_read(ctx);
if (token != OPA_JSON_TOKEN_COLON)
{
return NULL;
}
token = opa_json_lex_read(ctx);
val = opa_json_parse_token(ctx, token);
if (val == NULL)
{
return NULL;
}
opa_object_insert(obj, key, val);
token = opa_json_lex_read(ctx);
switch (token)
{
case OPA_JSON_TOKEN_OBJECT_END:
return &obj->hdr;
case OPA_JSON_TOKEN_COMMA:
{
token = opa_json_lex_read(ctx);
break;
}
default:
return NULL;
}
}
return NULL;
}
opa_value *opa_json_parse_object_or_set(opa_json_lex *ctx)
{
int token = opa_json_lex_read(ctx);
if (token == OPA_JSON_TOKEN_OBJECT_END)
{
return opa_object();
}
opa_value *head = opa_json_parse_token(ctx, token);
if (head == NULL)
{
return NULL;
}
token = opa_json_lex_read(ctx);
switch (token)
{
case OPA_JSON_TOKEN_OBJECT_END:
case OPA_JSON_TOKEN_COMMA:
return opa_json_parse_set(ctx, head, token);
case OPA_JSON_TOKEN_COLON:
return opa_json_parse_object(ctx, head);
}
return NULL;
}
opa_value *opa_json_parse_token(opa_json_lex *ctx, int token)
{
switch (token)
{
case OPA_JSON_TOKEN_NULL:
return opa_null();
case OPA_JSON_TOKEN_TRUE:
return opa_boolean(true);
case OPA_JSON_TOKEN_FALSE:
return opa_boolean(false);
case OPA_JSON_TOKEN_NUMBER:
return opa_json_parse_number(ctx->buf, ctx->buf_end - ctx->buf);
case OPA_JSON_TOKEN_STRING:
case OPA_JSON_TOKEN_STRING_ESCAPED:
return opa_json_parse_string(token, ctx->buf, ctx->buf_end - ctx->buf);
case OPA_JSON_TOKEN_ARRAY_START:
return opa_json_parse_array(ctx);
case OPA_JSON_TOKEN_OBJECT_START:
return opa_json_parse_object_or_set(ctx);
case OPA_JSON_TOKEN_EMPTY_SET:
return opa_set();
default:
return NULL;
}
}
OPA_INTERNAL
WASM_EXPORT(opa_json_parse)
opa_value *opa_json_parse(const char *input, size_t len)
{
opa_json_lex ctx;
opa_json_lex_init(input, len, &ctx);
int token = opa_json_lex_read(&ctx);
return opa_json_parse_token(&ctx, token);
}
OPA_INTERNAL
WASM_EXPORT(opa_value_parse)
opa_value *opa_value_parse(const char *input, size_t len)
{
opa_json_lex ctx;
opa_json_lex_init(input, len, &ctx);
ctx.set_literals_enabled = 1;
int token = opa_json_lex_read(&ctx);
return opa_json_parse_token(&ctx, token);
}
typedef struct {
char *buf;
char *next;
size_t len;
int set_literals_enabled;
int non_string_object_keys_enabled;
} opa_json_writer;
void opa_json_writer_init(opa_json_writer *w)
{
w->buf = NULL;
w->next = NULL;
w->len = 0;
w->set_literals_enabled = 0;
w->non_string_object_keys_enabled = 0;
}
size_t opa_json_writer_offset(opa_json_writer *w)
{
return w->next - w->buf;
}
size_t opa_json_writer_space(opa_json_writer *w)
{
return w->len - opa_json_writer_offset(w);
}
int opa_json_writer_grow(opa_json_writer *w, size_t newlen, size_t copy)
{
char *newbuf = (char *)opa_malloc(newlen);
if (newbuf == NULL)
{
return -1;
}
for (size_t i = 0; i < copy; i++)
{
newbuf[i] = w->buf[i];
}
size_t offset = opa_json_writer_offset(w);
w->buf = newbuf;
w->next = newbuf + offset;
w->len = newlen;
return 0;
}
int opa_json_writer_emit_chars(opa_json_writer *w, const char *bs, size_t nb)
{
size_t offset = opa_json_writer_offset(w);
if (offset + nb > w->len)
{
int rc = opa_json_writer_grow(w, (offset + nb) * 2, w->len);
if (rc != 0)
{
return rc;
}
}
for(int i = 0; i < nb; i++)
{
w->next[i] = bs[i];
}
w->next += nb;
return 0;
}
int opa_json_writer_emit_char(opa_json_writer *w, char b)
{
char bs[] = {b};
return opa_json_writer_emit_chars(w, bs, 1);
}
int opa_json_writer_emit_null(opa_json_writer *w)
{
char bs[] = "null";
return opa_json_writer_emit_chars(w, bs, sizeof(bs)-1);
}
int opa_json_writer_emit_boolean(opa_json_writer *w, opa_boolean_t *b)
{
if (b->v == 0)
{
char bs[] = "false";
return opa_json_writer_emit_chars(w, bs, sizeof(bs)-1);
}
char bs[] = "true";
return opa_json_writer_emit_chars(w, bs, sizeof(bs)-1);
}
int opa_json_writer_emit_integer(opa_json_writer *w, long long i)
{
char str[sizeof(i)*8+1]; // once base=2 is supported we need 8 bits per byte.
opa_itoa(i, str, 10);
return opa_json_writer_emit_chars(w, str, opa_strlen(str));
}
int opa_json_writer_emit_number(opa_json_writer *w, opa_number_t *n)
{
switch (n->repr)
{
case OPA_NUMBER_REPR_INT:
return opa_json_writer_emit_integer(w, n->v.i);
case OPA_NUMBER_REPR_REF:
return opa_json_writer_emit_chars(w, n->v.ref.s, n->v.ref.len);
default:
opa_abort("opa_json_writer_emit_number: illegal repr");
return -1;
}
}
int opa_json_writer_emit_string(opa_json_writer *w, opa_string_t *s)
{
int rc = opa_json_writer_emit_char(w, '"');
if (rc != 0)
{
return rc;
}
for (size_t i = 0; i < s->len; i++)
{
// Encode any character below 32 (space) with \u00XX, unless
// \n, \r or \t. including and above character 32, escape if
// \ or ". Anything else is expected to be valid UTF-8.
unsigned char c = s->v[i];
if (c >= ' ' && c != '\\' && c != '"')
{
rc = opa_json_writer_emit_char(w, c);
if (rc != 0)
{
return rc;
}
continue;
}
rc = opa_json_writer_emit_char(w, '\\');
if (rc != 0)
{
return rc;
}
if (c == '\\' || c == '"') {
rc = opa_json_writer_emit_char(w, c);
} else if (c == '\n') {
rc = opa_json_writer_emit_char(w, 'n');
} else if (c == '\r') {
rc = opa_json_writer_emit_char(w, 'r');
} else if (c == '\t') {
rc = opa_json_writer_emit_char(w, 't');
} else {
rc = opa_json_writer_emit_chars(w, "u00", 3);
if (rc != 0)
{
return rc;
}
char buf[3];
snprintf(buf, 3, "%02x", c);
rc = opa_json_writer_emit_chars(w, buf, 2);
if (rc != 0)
{
return rc;
}
}
if (rc != 0)
{
return rc;
}
}
rc = opa_json_writer_emit_char(w, '"');
if (rc != 0)
{
return rc;
}
return 0;
}
int opa_json_writer_emit_value(opa_json_writer *, opa_value *);
int opa_json_writer_emit_array_element(opa_json_writer *w, opa_value *coll, opa_value *k)
{
return opa_json_writer_emit_value(w, opa_value_get(coll, k));
}
int opa_json_writer_emit_set_element(opa_json_writer *w, opa_value *coll, opa_value *k)
{
return opa_json_writer_emit_value(w, k);
}
int opa_json_writer_emit_object_element(opa_json_writer *w, opa_value *coll, opa_value *k)
{
if (w->non_string_object_keys_enabled || opa_value_type(k) == OPA_STRING)
{
int rc = opa_json_writer_emit_value(w, k);
if (rc != 0)
{
return rc;
}
}
else
{
char *buf = opa_json_dump(k);
if (buf == NULL)
{
return -3;
}
opa_value *serialized = opa_string_terminated(buf);
int rc = opa_json_writer_emit_value(w, serialized);
opa_value_free(serialized);
opa_free(buf);
if (rc != 0)
{
return rc;
}
}
int rc = opa_json_writer_emit_char(w, ':');
if (rc != 0)
{
return rc;
}
return opa_json_writer_emit_value(w, opa_value_get(coll, k));
}
int opa_json_writer_emit_collection(opa_json_writer *w, opa_value *v, char open, char close, int (*emitfunc)(opa_json_writer *, opa_value *, opa_value *))
{
int rc = opa_json_writer_emit_char(w, open);
if (rc != 0)
{
return rc;
}
opa_value *prev = NULL;
opa_value *curr = NULL;
while ((curr = opa_value_iter(v, prev)) != NULL)
{
if (prev != NULL)
{
rc = opa_json_writer_emit_char(w, ',');
if (rc != 0)
{
return rc;
}
}
rc = emitfunc(w, v, curr);
if (rc != 0)
{
return rc;
}
prev = curr;
}
return opa_json_writer_emit_char(w, close);
}
int opa_json_writer_emit_set_literal(opa_json_writer *w, opa_set_t *set)
{