/
json.c
1006 lines (864 loc) · 26.8 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
/*
* This program is is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* $Id$
* @file json.c
* @brief Common functions for working with json-c
*
* @author Arran Cudbard-Bell
* @author Matthew Newton
*
* @copyright 2015 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
* @copyright 2015,2020 Network RADIUS SARL (legal@networkradius.com)
* @copyright 2015 The FreeRADIUS Server Project
*/
#include <freeradius-devel/server/rad_assert.h>
#include "base.h"
fr_table_num_sorted_t const fr_json_format_table[] = {
{ "array", JSON_MODE_ARRAY },
{ "array_of_names", JSON_MODE_ARRAY_OF_NAMES },
{ "array_of_values", JSON_MODE_ARRAY_OF_VALUES },
{ "object", JSON_MODE_OBJECT },
{ "object_simple", JSON_MODE_OBJECT_SIMPLE },
};
size_t fr_json_format_table_len = NUM_ELEMENTS(fr_json_format_table);
static fr_json_format_t const default_json_format = {
.attr = { .prefix = NULL },
.value = { .value_as_array = true },
};
/** Convert json object to fr_value_box_t
*
* @param[in] ctx to allocate any value buffers in (should usually be the same as out).
* @param[in] out Where to write value. Must be initialised.
* @param[in] object to convert.
* @param[in] enumv Any string values are assumed to be in PRESENTATION format, meaning
* that if an enumv is specified, they'll be checked against the list
* of aliases for that enumeration, and possibly converted into one of
* the enumeration values (which may not be a string).
* @param[in] tainted Whether the data source is untrusted.
* @return
* - 0 on success.
* - -1 on failure.
*/
int fr_json_object_to_value_box(TALLOC_CTX *ctx, fr_value_box_t *out, json_object *object,
fr_dict_attr_t const *enumv, bool tainted)
{
switch (fr_json_object_get_type(object)) {
case json_type_string:
{
char const *value;
size_t len;
fr_dict_enum_t *found;
value = json_object_get_string(object);
len = json_object_get_string_len(object);
if (!enumv) goto no_enumv;
if (fr_dict_valid_name(value, len) < 0) goto no_enumv;
/*
* If an alias exists, use that value instead
*/
found = fr_dict_enum_by_name(enumv, value, len);
if (found) {
if (fr_value_box_copy(ctx, out, found->value) < 0) return -1;
return 0;
}
no_enumv:
/*
* Just copy the string to the box.
*/
fr_value_box_bstrndup(ctx, out, NULL, value, len, tainted);
}
break;
case json_type_double:
out->type = FR_TYPE_FLOAT64;
out->vb_float64 = json_object_get_double(object);
break;
case json_type_int:
{
#ifdef HAVE_JSON_OBJECT_GET_INT64
int64_t num;
#else
int32_t num;
#endif
#ifndef HAVE_JSON_OBJECT_GET_INT64
num = json_object_get_int(object);
#else
num = json_object_get_int64(object);
if (num < INT32_MIN) { /* 64bit signed*/
out->type = FR_TYPE_INT64;
out->vb_int64 = (int64_t) num;
} else if (num > UINT32_MAX) { /* 64bit unsigned */
out->type = FR_TYPE_UINT64;
out->vb_uint64 = (uint64_t) num;
} else
#endif
if (num < INT16_MIN) { /* 32bit signed */
out->type = FR_TYPE_INT32;
out->vb_int32 = (int32_t)num;
} else if (num < INT8_MIN) { /* 16bit signed */
out->type = FR_TYPE_INT16;
out->vb_int16 = (int16_t)num;
} else if (num < 0) { /* 8bit signed */
out->type = FR_TYPE_INT8;
out->vb_int8 = (int8_t)num;
} else if (num > UINT16_MAX) { /* 32bit unsigned */
out->type = FR_TYPE_UINT32;
out->vb_uint32 = (uint32_t) num;
} else if (num > UINT8_MAX) { /* 16bit unsigned */
out->type = FR_TYPE_UINT16;
out->vb_uint16 = (uint16_t) num;
} else { /* 8bit unsigned */
out->type = FR_TYPE_UINT8;
out->vb_uint8 = (uint8_t) num;
}
}
break;
case json_type_boolean:
out->type = FR_TYPE_BOOL;
out->datum.boolean = json_object_get_boolean(object);
break;
case json_type_null:
case json_type_array:
case json_type_object:
{
char const *value = json_object_to_json_string(object);
fr_value_box_bstrndup(ctx, out, NULL, value, strlen(value), tainted);
}
break;
}
out->tainted = tainted;
return 0;
}
/** Convert boxed value_box to a JSON object
*
* @param[in] ctx to allocate temporary buffers in
* @param[in] data to convert.
*/
json_object *json_object_from_value_box(TALLOC_CTX *ctx, fr_value_box_t const *data)
{
/*
* We're converting to PRESENTATION format
* so any attributes with enumeration values
* should be converted to string types.
*/
if (data->enumv) {
fr_dict_enum_t *enumv;
enumv = fr_dict_enum_by_value(data->enumv, data);
if (enumv) return json_object_new_string(enumv->name);
}
switch (data->type) {
default:
do_string:
{
char *p;
json_object *obj;
p = fr_value_box_asprint(ctx, data, '\0');
if (!p) return NULL;
obj = json_object_new_string(p);
talloc_free(p);
return obj;
}
case FR_TYPE_BOOL:
return json_object_new_boolean(data->vb_uint8);
case FR_TYPE_UINT8:
return json_object_new_int(data->vb_uint8);
case FR_TYPE_UINT16:
return json_object_new_int(data->vb_uint16);
#ifdef HAVE_JSON_OBJECT_GET_INT64
case FR_TYPE_UINT32:
return json_object_new_int64((int64_t)data->vb_uint64); /* uint32_t (max) > int32_t (max) */
case FR_TYPE_UINT64:
if (data->vb_uint64 > INT64_MAX) goto do_string;
return json_object_new_int64(data->vb_uint64);
#else
case FR_TYPE_UINT32:
if (data->vb_uint32 > INT32_MAX) goto do_string;
return json_object_new_in(data->vb_uint32);
#endif
case FR_TYPE_INT8:
return json_object_new_int(data->vb_int8);
case FR_TYPE_INT16:
return json_object_new_int(data->vb_int16);
case FR_TYPE_INT32:
return json_object_new_int(data->vb_int32);
#ifdef HAVE_JSON_OBJECT_GET_INT64
case FR_TYPE_INT64:
return json_object_new_int64(data->vb_int16);
#endif
}
}
/** Escapes string for use as a JSON string
*
* @param ctx Talloc context to allocate this string
* @param s Input string
* @param include_quotes Include the surrounding quotes of JSON strings
* @return New allocated character string, or NULL if something failed.
*/
char *fr_json_from_string(TALLOC_CTX *ctx, char const *s, bool include_quotes)
{
char const *p;
char *out = NULL;
struct json_object *json;
int len;
json = json_object_new_string(s);
if (!json) return NULL;
if ((p = json_object_to_json_string(json))) {
if (include_quotes) {
out = talloc_typed_strdup(ctx, p);
} else {
len = strlen(p);
out = talloc_bstrndup(ctx, p + 1, len - 2); /* to_json_string adds quotes (") */
}
}
json_object_put(json);
return out;
}
/** Prints attribute as string, escaped suitably for use as JSON string
*
* Returns < 0 if the buffer may be (or have been) too small to write the encoded
* JSON value to.
*
* @param out Where to write the string.
* @param outlen Length of output buffer.
* @param vp to print.
* @return
* - Length of data written to out.
* - value >= outlen on truncation.
*/
size_t fr_json_from_pair(char *out, size_t outlen, VALUE_PAIR const *vp)
{
size_t len, freespace = outlen;
if (!vp->da->flags.has_tag) {
switch (vp->vp_type) {
case FR_TYPE_UINT32:
if (vp->da->flags.has_value) break;
return snprintf(out, freespace, "%u", vp->vp_uint32);
case FR_TYPE_UINT16:
if (vp->da->flags.has_value) break;
return snprintf(out, freespace, "%u", (unsigned int) vp->vp_uint16);
case FR_TYPE_UINT8:
if (vp->da->flags.has_value) break;
return snprintf(out, freespace, "%u", (unsigned int) vp->vp_uint8);
case FR_TYPE_INT32:
return snprintf(out, freespace, "%d", vp->vp_int32);
default:
break;
}
}
if (vp->vp_type == FR_TYPE_STRING) {
char *tmp = fr_json_from_string(NULL, vp->vp_strvalue, true);
/* Indicate truncation */
if (!tmp) return outlen + 1;
len = strlen(tmp);
if (freespace <= len) return outlen + 1;
strcpy(out, tmp);
talloc_free(tmp);
return len;
}
/* Indicate truncation */
if (freespace < 2) return outlen + 1;
*out++ = '"';
freespace--;
len = fr_pair_value_snprint(out, freespace, vp, 0);
if (is_truncated(len, freespace)) return (outlen - freespace) + len;
out += len;
freespace -= len;
/* Indicate truncation */
if (freespace < 2) return outlen + 1;
*out++ = '"';
freespace--;
*out = '\0'; // We don't increment out, because the nul byte should not be included in the length
return outlen - freespace;
}
/** Print JSON-C version
*
*/
void fr_json_version_print(void)
{
#ifdef HAVE_JSON_C_VERSION
INFO("libfreeradius-json: json-c version: %s", json_c_version());
#else
INFO("libfreeradius-json: json-c version: Unknown (less than 0.10) - Please upgrade");
#endif
}
/** Convert VALUE_PAIR into a JSON object
*
* If format.value.enum_as_int is set, and the given VP is an enum
* value, the integer value is returned as a json_object rather
* than the text representation.
*
* If format.value.always_string is set then a numeric value pair
* will be returned as a JSON string object.
*
* @param[in] ctx Talloc context.
* @param[out] out returned json object.
* @param[in] vp to get the value of.
* @param[in] format format definition, or NULL.
* @return
* - 1 if 'out' is the integer enum value, 0 otherwise
* - -1 on error.
*/
static int json_afrom_value_box(TALLOC_CTX *ctx, json_object **out,
VALUE_PAIR *vp, fr_json_format_t const *format)
{
struct json_object *obj;
fr_value_box_t const *vb;
fr_value_box_t vb_str;
int is_enum = 0;
rad_assert(vp);
vb = &vp->data;
if (format && format->value.enum_as_int) {
is_enum = fr_pair_value_enum_box(&vb, vp);
rad_assert(is_enum >= 0);
}
if (format && format->value.always_string) {
if (fr_value_box_cast(ctx, &vb_str, FR_TYPE_STRING, NULL, vb) == 0) {
vb = &vb_str;
} else {
return -1;
}
}
MEM(obj = json_object_from_value_box(ctx, vb));
if (format && format->value.always_string) {
fr_value_box_clear(&vb_str);
}
*out = obj;
return is_enum;
}
/** Add prefix to attribute name
*
* If the format "attr.prefix" string is set then prepend this
* to the given attribute name, otherwise return name unchanged.
*
* @param[out] buf where to write the new name, if set
* @param[in] buf_len length of buf
* @param[in] name original attribute name
* @param[in] format json format structure
* @return pointer to name, or buf if the prefix was added
*/
static inline char const *attr_name_with_prefix(char *buf, size_t buf_len, const char *name, fr_json_format_t const *format)
{
int len;
if (!format->attr.prefix) return name;
len = snprintf(buf, buf_len, "%s:%s", format->attr.prefix, name);
if (len == (int)strlen(buf)) {
return buf;
}
return name;
}
/** Verify that the options in fr_json_format_t are valid
*
* Warnings are optional, will fatal error if the format is corrupt.
*
* @param[in] format the format structure to check
* @param[in] verbose print out warnings if set
* @return true if format is good, otherwise false
*/
bool fr_json_format_verify(fr_json_format_t const *format, bool verbose)
{
bool ret = true;
rad_assert(format);
switch (format->output_mode) {
case JSON_MODE_OBJECT:
case JSON_MODE_OBJECT_SIMPLE:
case JSON_MODE_ARRAY:
/* all options are valid */
return true;
case JSON_MODE_ARRAY_OF_VALUES:
if (format->attr.prefix) {
if (verbose) WARN("attribute name prefix not valid in output_mode 'array_of_values' and will be ignored");
ret = false;
}
if (format->value.value_as_array) {
if (verbose) WARN("'value_as_array' not valid in output_mode 'array_of_values' and will be ignored");
ret = false;
}
return ret;
case JSON_MODE_ARRAY_OF_NAMES:
if (format->value.value_as_array) {
if (verbose) WARN("'value_as_array' not valid in output_mode 'array_of_names' and will be ignored");
ret = false;
}
if (format->value.enum_as_int) {
if (verbose) WARN("'enum_as_int' not valid in output_mode 'array_of_names' and will be ignored");
ret = false;
}
if (format->value.always_string) {
if (verbose) WARN("'always_string' not valid in output_mode 'array_of_names' and will be ignored");
ret = false;
}
return ret;
default:
ERROR("JSON format output mode is invalid");
}
/* If we get here, something has gone wrong */
rad_assert(0);
}
/** Returns a JSON object representation of a list of value pairs
*
* The result is a struct json_object, which should be free'd with
* json_object_put() by the caller. Intended to only be called by
* fr_json_afrom_pair_list().
*
* This function generates the "object" format, JSON_MODE_OBJECT.
* @see fr_json_format_s
*
* @param[in] ctx Talloc context.
* @param[in] vps a list of value pairs.
* @param[in] format Formatting control, must be set.
* @return JSON object with the generated representation.
*/
static json_object *json_object_afrom_pair_list(TALLOC_CTX *ctx, VALUE_PAIR *vps,
fr_json_format_t const *format)
{
fr_cursor_t cursor;
VALUE_PAIR *vp;
struct json_object *obj;
char buf[FR_DICT_ATTR_MAX_NAME_LEN + 32];
/* Check format and type */
rad_assert(format);
rad_assert(format->output_mode == JSON_MODE_OBJECT);
MEM(obj = json_object_new_object());
for (vp = fr_cursor_init(&cursor, &vps);
vp;
vp = fr_cursor_next(&cursor)) {
char const *attr_name;
struct json_object *vp_object, *values, *value, *type_name;
/*
* Get attribute name and value.
*/
attr_name = attr_name_with_prefix(buf, sizeof(buf), vp->da->name, format);
if (json_afrom_value_box(ctx, &value, vp, format) < 0) {
fr_strerror_printf("Failed to convert attribute value to JSON object");
error:
json_object_put(obj);
return NULL;
}
/*
* Look in the table to see if we already have
* a key for the attribute we're working on.
*/
if (!json_object_object_get_ex(obj, attr_name, &vp_object)) {
/*
* Wasn't there, so create a new object for this attribute.
*/
MEM(vp_object = json_object_new_object());
json_object_object_add(obj, attr_name, vp_object);
/*
* Add "type" to newly created keys.
*/
MEM(type_name = json_object_new_string(fr_table_str_by_value(fr_value_box_type_table,
vp->vp_type, "<INVALID>")));
json_object_object_add(vp_object, "type", type_name);
/*
* Create a "value" array to hold any attribute values for this attribute...
*/
if (format->value.value_as_array) {
MEM(values = json_object_new_array());
json_object_object_add(vp_object, "value", values);
} else {
/*
* ...unless this is the first time we've seen the attribute and
* value_as_array is false, in which case just add the value directly
* and move on to the next attribute.
*/
json_object_object_add(vp_object, "value", value);
continue;
}
} else {
/*
* Find the 'values' array to add the current value to.
*/
if (!fr_cond_assert(json_object_object_get_ex(vp_object, "value", &values))) {
fr_strerror_printf("Inconsistent JSON tree");
goto error;
}
/*
* If value_as_array is no set then "values" may not be an array, so it will
* need converting to an array to add this extra attribute.
*/
if (!format->value.value_as_array) {
json_type type;
struct json_object *convert_value = values;
/* Check "values" type */
type = json_object_get_type(values);
/* It wasn't an array, so turn it into one with the old value as the first entry */
if (type != json_type_array) {
MEM(values = json_object_new_array());
json_object_array_add(values, json_object_get(convert_value));
json_object_object_del(vp_object, "value");
json_object_object_add(vp_object, "value", values);
}
}
}
/*
* Append to the JSON array.
*/
json_object_array_add(values, value);
}
return obj;
}
/** Returns a JSON object representation of a list of value pairs
*
* The result is a struct json_object, which should be free'd with
* json_object_put() by the caller. Intended to only be called by
* fr_json_afrom_pair_list().
*
* This function generates the "simple object" format, JSON_MODE_OBJECT_SIMPLE.
* @see fr_json_format_s
*
* @param[in] ctx Talloc context.
* @param[in] vps a list of value pairs.
* @param[in] format Formatting control, must be set.
* @return JSON object with the generated representation.
*/
static json_object *json_smplobj_afrom_pair_list(TALLOC_CTX *ctx, VALUE_PAIR *vps,
fr_json_format_t const *format)
{
fr_cursor_t cursor;
VALUE_PAIR *vp;
struct json_object *obj;
char buf[FR_DICT_ATTR_MAX_NAME_LEN + 32];
json_type type;
/* Check format and type */
rad_assert(format);
rad_assert(format->output_mode == JSON_MODE_OBJECT_SIMPLE);
MEM(obj = json_object_new_object());
for (vp = fr_cursor_init(&cursor, &vps);
vp;
vp = fr_cursor_next(&cursor)) {
char const *attr_name;
struct json_object *vp_object, *values, *value;
bool add_single = false;
/*
* Get attribute name and value.
*/
attr_name = attr_name_with_prefix(buf, sizeof(buf), vp->da->name, format);
if (json_afrom_value_box(ctx, &value, vp, format) < 0) {
fr_strerror_printf("Failed to convert attribute value to JSON object");
json_object_put(obj);
return NULL;
}
/*
* See if we already have a key in the table we're working on,
* if not then create a new one.
*/
if (!json_object_object_get_ex(obj, attr_name, &vp_object)) {
if (format->value.value_as_array) {
/*
* We have been asked to ensure /all/ values are lists,
* even if there's only one attribute.
*/
MEM(values = json_object_new_array());
json_object_object_add(obj, attr_name, values);
} else {
/*
* Deal with it later on.
*/
add_single = true;
}
/*
* If we do have the key already, get its value array.
*/
} else {
type = json_object_get_type(vp_object);
if (type == json_type_array) {
values = vp_object;
} else {
/*
* We've seen one of these before, but didn't add
* it as an array the first time. Sort that out.
*/
MEM(values = json_object_new_array());
json_object_array_add(values, json_object_get(vp_object));
json_object_object_del(obj, attr_name);
json_object_object_add(obj, attr_name, values);
}
}
if (add_single) {
/*
* Only ever used the first time adding a new
* attribute when "value_as_array" is not set.
*/
json_object_object_add(obj, attr_name, value);
} else {
/*
* Otherwise we're always appending to a JSON array.
*/
json_object_array_add(values, value);
}
}
return obj;
}
/** Returns a JSON array representation of a list of value pairs
*
* The result is a struct json_object, which should be free'd with
* json_object_put() by the caller. Intended to only be called by
* fr_json_afrom_pair_list().
*
* This function generates the "array" format, JSON_MODE_ARRAY.
* @see fr_json_format_s
*
* @param[in] ctx Talloc context.
* @param[in] vps a list of value pairs.
* @param[in] format Formatting control, must be set.
* @return JSON object with the generated representation.
*/
static struct json_object *json_array_afrom_pair_list(TALLOC_CTX *ctx, VALUE_PAIR *vps,
fr_json_format_t const *format)
{
fr_cursor_t cursor;
VALUE_PAIR *vp;
struct json_object *obj;
struct json_object *seen_attributes;
char buf[FR_DICT_ATTR_MAX_NAME_LEN + 32];
/* Check format and type */
rad_assert(format);
rad_assert(format->output_mode == JSON_MODE_ARRAY);
MEM(obj = json_object_new_array());
/*
* If attribute values should be in a list format, then keep track
* of the attributes we've previously seen in a JSON object.
*/
if (format->value.value_as_array) {
seen_attributes = json_object_new_object();
}
for (vp = fr_cursor_init(&cursor, &vps);
vp;
vp = fr_cursor_next(&cursor)) {
char const *attr_name;
struct json_object *values, *name, *value, *type_name;
struct json_object *attrobj = NULL;
bool already_seen = false;
/*
* Get attribute name and value.
*/
attr_name = attr_name_with_prefix(buf, sizeof(buf), vp->da->name, format);
if (json_afrom_value_box(ctx, &value, vp, format) < 0) {
fr_strerror_printf("Failed to convert attribute value to JSON object");
json_object_put(obj);
return NULL;
}
if (format->value.value_as_array) {
/*
* Try and find this attribute in the "seen_attributes" object. If it is
* there then get the "values" array to add this attribute value to.
*/
already_seen = json_object_object_get_ex(seen_attributes, attr_name, &values);
}
/*
* If we're adding all attributes to the toplevel array, or we're adding values
* to an array of an existing attribute but haven't seen it before, then we need
* to create a new JSON object for this attribute.
*/
if (!format->value.value_as_array || !already_seen) {
/*
* Create object and add it to top-level array
*/
MEM(attrobj = json_object_new_object());
json_object_array_add(obj, attrobj);
/*
* Add the attribute name in the "name" key and the type in the "type" key
*/
MEM(name = json_object_new_string(attr_name));
json_object_object_add(attrobj, "name", name);
MEM(type_name = json_object_new_string(fr_table_str_by_value(fr_value_box_type_table,
vp->vp_type, "<INVALID>")));
json_object_object_add(attrobj, "type", type_name);
}
if (format->value.value_as_array) {
/*
* We're adding values to an array for the first copy of this attribute
* that we saw. First time around we need to create an array.
*/
if (!already_seen) {
MEM(values = json_object_new_array());
/*
* Add "value":[] key to the attribute object
*/
json_object_object_add(attrobj, "value", values);
/*
* Also add to "seen_attributes" to check later
*/
json_object_object_add(seen_attributes, attr_name, json_object_get(values));
}
/*
* Always add the value to the respective "values" array.
*/
json_object_array_add(values, value);
} else {
/*
* This is simpler; just add a "value": key to the attribute object.
*/
json_object_object_add(attrobj, "value", value);
}
}
/*
* No longer need the "seen_attributes" object, it was just used for tracking.
*/
if (format->value.value_as_array) {
json_object_put(seen_attributes);
}
return obj;
}
/** Returns a JSON array of a list of value pairs
*
* The result is a struct json_object, which should be free'd with
* json_object_put() by the caller. Intended to only be called by
* fr_json_afrom_pair_list().
*
* This function generates the "array_of_values" format,
* JSON_MODE_ARRAY_OF_VALUES, listing just the attribute values.
* @see fr_json_format_s
*
* @param[in] ctx Talloc context.
* @param[in] vps a list of value pairs.
* @param[in] format Formatting control, must be set.
* @return JSON object with the generated representation.
*/
static struct json_object *json_value_array_afrom_pair_list(TALLOC_CTX *ctx, VALUE_PAIR *vps,
fr_json_format_t const *format)
{
fr_cursor_t cursor;
VALUE_PAIR *vp;
struct json_object *obj;
/* Check format and type */
rad_assert(format);
rad_assert(format->output_mode == JSON_MODE_ARRAY_OF_VALUES);
MEM(obj = json_object_new_array());
/*
* This array format is very simple - just add all the
* attribute values to the array in order.
*/
for (vp = fr_cursor_init(&cursor, &vps);
vp;
vp = fr_cursor_next(&cursor)) {
struct json_object *value;
if (json_afrom_value_box(ctx, &value, vp, format) < 0) {
fr_strerror_printf("Failed to convert attribute value to JSON object");
json_object_put(obj);
return NULL;
}
json_object_array_add(obj, value);
}
return obj;
}
/** Returns a JSON array of a list of value pairs
*
* The result is a struct json_object, which should be free'd with
* json_object_put() by the caller. Intended to only be called by
* fr_json_afrom_pair_list().
*
* This function generates the "array_of_names" format,
* JSON_MODE_ARRAY_OF_NAMES, listing just the attribute names.
* @see fr_json_format_s
*
* @param[in] ctx Talloc context.
* @param[in] vps a list of value pairs.
* @param[in] format Formatting control, must be set.
* @return JSON object with the generated representation.
*/
static struct json_object *json_attr_array_afrom_pair_list(UNUSED TALLOC_CTX *ctx, VALUE_PAIR *vps,
fr_json_format_t const *format)
{
fr_cursor_t cursor;
VALUE_PAIR *vp;
struct json_object *obj;
char buf[FR_DICT_ATTR_MAX_NAME_LEN + 32];
/* Check format and type */
rad_assert(format);
rad_assert(format->output_mode == JSON_MODE_ARRAY_OF_NAMES);
MEM(obj = json_object_new_array());
/*
* Add all the attribute names to the array in order.
*/
for (vp = fr_cursor_init(&cursor, &vps);
vp;
vp = fr_cursor_next(&cursor)) {
char const *attr_name;
struct json_object *value;
attr_name = attr_name_with_prefix(buf, sizeof(buf), vp->da->name, format);
value = json_object_new_string(attr_name);
json_object_array_add(obj, value);
}
return obj;
}
/** Returns a JSON string of a list of value pairs
*
* The result is a talloc-ed string, freeing the string is
* the responsibility of the caller.
*
* The 'format' struct contains settings to configure the output
* JSON document format.
* @see fr_json_format_s
*
* Default output, when format is NULL, is:
@verbatim
{
"<attribute0>":{
"type":"<type0>",
"value":[<value0>,<value1>,<valueN>]
},
"<attribute1>":{
"type":"<type1>",
"value":[...]
},
"<attributeN>":{
"type":"<typeN>",
"value":[...]
}
}
@endverbatim
*
* @param[in] ctx Talloc context.
* @param[in] vps a list of value pairs.
* @param[in] format Formatting control, can be NULL to use default format.
* @return JSON string representation of the value pairs
*/
char *fr_json_afrom_pair_list(TALLOC_CTX *ctx, VALUE_PAIR *vps,
fr_json_format_t const *format)
{
struct json_object *obj;
const char *p;
char *out;
if (!format) format = &default_json_format;
switch (format->output_mode) {
case JSON_MODE_OBJECT:
MEM(obj = json_object_afrom_pair_list(ctx, vps, format));
break;
case JSON_MODE_OBJECT_SIMPLE:
MEM(obj = json_smplobj_afrom_pair_list(ctx, vps, format));
break;
case JSON_MODE_ARRAY:
MEM(obj = json_array_afrom_pair_list(ctx, vps, format));
break;
case JSON_MODE_ARRAY_OF_VALUES:
MEM(obj = json_value_array_afrom_pair_list(ctx, vps, format));
break;
case JSON_MODE_ARRAY_OF_NAMES:
MEM(obj = json_attr_array_afrom_pair_list(ctx, vps, format));
break;
default:
/* This should never happen */
rad_assert(0);
}
MEM(p = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
MEM(out = talloc_strdup(ctx, p));
/*