2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
12 * Copyright (c) 2014, Joyent, Inc.
13 * Copyright (c) 2017 by Delphix. All rights reserved.
20 #include <sys/debug.h>
22 #include "libnvpair.h"
24 #define FPRINTF(fp, ...) \
26 if (fprintf(fp, __VA_ARGS__) < 0) \
31 * When formatting a string for JSON output we must escape certain characters,
32 * as described in RFC4627. This applies to both member names and
33 * DATA_TYPE_STRING values.
35 * This function will only operate correctly if the following conditions are
38 * 1. The input String is encoded in the current locale.
40 * 2. The current locale includes the Basic Multilingual Plane (plane 0)
41 * as defined in the Unicode standard.
43 * The output will be entirely 7-bit ASCII (as a subset of UTF-8) with all
44 * representable Unicode characters included in their escaped numeric form.
47 nvlist_print_json_string(FILE *fp
, const char *input
)
54 while ((sz
= mbrtowc(&c
, input
, MB_CUR_MAX
, &mbr
)) > 0) {
55 if (sz
== (size_t)-1 || sz
== (size_t)-2) {
57 * We last read an invalid multibyte character sequence,
85 if ((c
>= 0x00 && c
<= 0x1f) ||
86 (c
> 0x7f && c
<= 0xffff)) {
88 * Render both Control Characters and Unicode
89 * characters in the Basic Multilingual Plane
90 * as JSON-escaped multibyte characters.
92 FPRINTF(fp
, "\\u%04x", (int)(0xffff & c
));
93 } else if (c
>= 0x20 && c
<= 0x7f) {
95 * Render other 7-bit ASCII characters directly
96 * and drop other, unrepresentable characters.
98 FPRINTF(fp
, "%c", (int)(0xff & c
));
110 * Dump a JSON-formatted representation of an nvlist to the provided FILE *.
111 * This routine does not output any new-lines or additional whitespace other
112 * than that contained in strings, nor does it call fflush(3C).
115 nvlist_print_json(FILE *fp
, nvlist_t
*nvl
)
118 boolean_t first
= B_TRUE
;
122 for (curr
= nvlist_next_nvpair(nvl
, NULL
); curr
;
123 curr
= nvlist_next_nvpair(nvl
, curr
)) {
124 data_type_t type
= nvpair_type(curr
);
131 if (nvlist_print_json_string(fp
, nvpair_name(curr
)) == -1)
136 case DATA_TYPE_STRING
: {
137 const char *string
= fnvpair_value_string(curr
);
138 if (nvlist_print_json_string(fp
, string
) == -1)
143 case DATA_TYPE_BOOLEAN
: {
148 case DATA_TYPE_BOOLEAN_VALUE
: {
149 FPRINTF(fp
, "%s", fnvpair_value_boolean_value(curr
) ==
150 B_TRUE
? "true" : "false");
154 case DATA_TYPE_BYTE
: {
155 FPRINTF(fp
, "%hhu", fnvpair_value_byte(curr
));
159 case DATA_TYPE_INT8
: {
160 FPRINTF(fp
, "%hhd", fnvpair_value_int8(curr
));
164 case DATA_TYPE_UINT8
: {
165 FPRINTF(fp
, "%hhu", fnvpair_value_uint8(curr
));
169 case DATA_TYPE_INT16
: {
170 FPRINTF(fp
, "%hd", fnvpair_value_int16(curr
));
174 case DATA_TYPE_UINT16
: {
175 FPRINTF(fp
, "%hu", fnvpair_value_uint16(curr
));
179 case DATA_TYPE_INT32
: {
180 FPRINTF(fp
, "%d", fnvpair_value_int32(curr
));
184 case DATA_TYPE_UINT32
: {
185 FPRINTF(fp
, "%u", fnvpair_value_uint32(curr
));
189 case DATA_TYPE_INT64
: {
191 (long long)fnvpair_value_int64(curr
));
195 case DATA_TYPE_UINT64
: {
197 (unsigned long long)fnvpair_value_uint64(curr
));
201 case DATA_TYPE_HRTIME
: {
203 VERIFY0(nvpair_value_hrtime(curr
, &val
));
204 FPRINTF(fp
, "%llu", (unsigned long long)val
);
208 case DATA_TYPE_DOUBLE
: {
210 VERIFY0(nvpair_value_double(curr
, &val
));
211 FPRINTF(fp
, "%f", val
);
215 case DATA_TYPE_NVLIST
: {
216 if (nvlist_print_json(fp
,
217 fnvpair_value_nvlist(curr
)) == -1)
222 case DATA_TYPE_STRING_ARRAY
: {
225 VERIFY0(nvpair_value_string_array(curr
, &val
, &valsz
));
227 for (i
= 0; i
< valsz
; i
++) {
230 if (nvlist_print_json_string(fp
, val
[i
]) == -1)
237 case DATA_TYPE_NVLIST_ARRAY
: {
240 VERIFY0(nvpair_value_nvlist_array(curr
, &val
, &valsz
));
242 for (i
= 0; i
< valsz
; i
++) {
245 if (nvlist_print_json(fp
, val
[i
]) == -1)
252 case DATA_TYPE_BOOLEAN_ARRAY
: {
255 VERIFY0(nvpair_value_boolean_array(curr
, &val
, &valsz
));
257 for (i
= 0; i
< valsz
; i
++) {
260 FPRINTF(fp
, val
[i
] == B_TRUE
?
267 case DATA_TYPE_BYTE_ARRAY
: {
270 VERIFY0(nvpair_value_byte_array(curr
, &val
, &valsz
));
272 for (i
= 0; i
< valsz
; i
++) {
275 FPRINTF(fp
, "%hhu", val
[i
]);
281 case DATA_TYPE_UINT8_ARRAY
: {
284 VERIFY0(nvpair_value_uint8_array(curr
, &val
, &valsz
));
286 for (i
= 0; i
< valsz
; i
++) {
289 FPRINTF(fp
, "%hhu", val
[i
]);
295 case DATA_TYPE_INT8_ARRAY
: {
298 VERIFY0(nvpair_value_int8_array(curr
, &val
, &valsz
));
300 for (i
= 0; i
< valsz
; i
++) {
303 FPRINTF(fp
, "%hhd", val
[i
]);
309 case DATA_TYPE_UINT16_ARRAY
: {
312 VERIFY0(nvpair_value_uint16_array(curr
, &val
, &valsz
));
314 for (i
= 0; i
< valsz
; i
++) {
317 FPRINTF(fp
, "%hu", val
[i
]);
323 case DATA_TYPE_INT16_ARRAY
: {
326 VERIFY0(nvpair_value_int16_array(curr
, &val
, &valsz
));
328 for (i
= 0; i
< valsz
; i
++) {
331 FPRINTF(fp
, "%hd", val
[i
]);
337 case DATA_TYPE_UINT32_ARRAY
: {
340 VERIFY0(nvpair_value_uint32_array(curr
, &val
, &valsz
));
342 for (i
= 0; i
< valsz
; i
++) {
345 FPRINTF(fp
, "%u", val
[i
]);
351 case DATA_TYPE_INT32_ARRAY
: {
354 VERIFY0(nvpair_value_int32_array(curr
, &val
, &valsz
));
356 for (i
= 0; i
< valsz
; i
++) {
359 FPRINTF(fp
, "%d", val
[i
]);
365 case DATA_TYPE_UINT64_ARRAY
: {
368 VERIFY0(nvpair_value_uint64_array(curr
, &val
, &valsz
));
370 for (i
= 0; i
< valsz
; i
++) {
374 (unsigned long long)val
[i
]);
380 case DATA_TYPE_INT64_ARRAY
: {
383 VERIFY0(nvpair_value_int64_array(curr
, &val
, &valsz
));
385 for (i
= 0; i
< valsz
; i
++) {
388 FPRINTF(fp
, "%lld", (long long)val
[i
]);
394 case DATA_TYPE_UNKNOWN
:
395 case DATA_TYPE_DONTCARE
: