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
)
53 bzero(&mbr
, sizeof (mbr
));
56 while ((sz
= mbrtowc(&c
, input
, MB_CUR_MAX
, &mbr
)) > 0) {
80 if ((c
>= 0x00 && c
<= 0x1f) ||
81 (c
> 0x7f && c
<= 0xffff)) {
83 * Render both Control Characters and Unicode
84 * characters in the Basic Multilingual Plane
85 * as JSON-escaped multibyte characters.
87 FPRINTF(fp
, "\\u%04x", (int)(0xffff & c
));
88 } else if (c
>= 0x20 && c
<= 0x7f) {
90 * Render other 7-bit ASCII characters directly
91 * and drop other, unrepresentable characters.
93 FPRINTF(fp
, "%c", (int)(0xff & c
));
100 if (sz
== (size_t)-1 || sz
== (size_t)-2) {
102 * We last read an invalid multibyte character sequence,
103 * so return an error.
113 * Dump a JSON-formatted representation of an nvlist to the provided FILE *.
114 * This routine does not output any new-lines or additional whitespace other
115 * than that contained in strings, nor does it call fflush(3C).
118 nvlist_print_json(FILE *fp
, nvlist_t
*nvl
)
121 boolean_t first
= B_TRUE
;
125 for (curr
= nvlist_next_nvpair(nvl
, NULL
); curr
;
126 curr
= nvlist_next_nvpair(nvl
, curr
)) {
127 data_type_t type
= nvpair_type(curr
);
134 if (nvlist_print_json_string(fp
, nvpair_name(curr
)) == -1)
139 case DATA_TYPE_STRING
: {
140 char *string
= fnvpair_value_string(curr
);
141 if (nvlist_print_json_string(fp
, string
) == -1)
146 case DATA_TYPE_BOOLEAN
: {
151 case DATA_TYPE_BOOLEAN_VALUE
: {
152 FPRINTF(fp
, "%s", fnvpair_value_boolean_value(curr
) ==
153 B_TRUE
? "true" : "false");
157 case DATA_TYPE_BYTE
: {
158 FPRINTF(fp
, "%hhu", fnvpair_value_byte(curr
));
162 case DATA_TYPE_INT8
: {
163 FPRINTF(fp
, "%hhd", fnvpair_value_int8(curr
));
167 case DATA_TYPE_UINT8
: {
168 FPRINTF(fp
, "%hhu", fnvpair_value_uint8_t(curr
));
172 case DATA_TYPE_INT16
: {
173 FPRINTF(fp
, "%hd", fnvpair_value_int16(curr
));
177 case DATA_TYPE_UINT16
: {
178 FPRINTF(fp
, "%hu", fnvpair_value_uint16(curr
));
182 case DATA_TYPE_INT32
: {
183 FPRINTF(fp
, "%d", fnvpair_value_int32(curr
));
187 case DATA_TYPE_UINT32
: {
188 FPRINTF(fp
, "%u", fnvpair_value_uint32(curr
));
192 case DATA_TYPE_INT64
: {
194 (long long)fnvpair_value_int64(curr
));
198 case DATA_TYPE_UINT64
: {
200 (unsigned long long)fnvpair_value_uint64(curr
));
204 case DATA_TYPE_HRTIME
: {
206 VERIFY0(nvpair_value_hrtime(curr
, &val
));
207 FPRINTF(fp
, "%llu", (unsigned long long)val
);
211 case DATA_TYPE_DOUBLE
: {
213 VERIFY0(nvpair_value_double(curr
, &val
));
214 FPRINTF(fp
, "%f", val
);
218 case DATA_TYPE_NVLIST
: {
219 if (nvlist_print_json(fp
,
220 fnvpair_value_nvlist(curr
)) == -1)
225 case DATA_TYPE_STRING_ARRAY
: {
228 VERIFY0(nvpair_value_string_array(curr
, &val
, &valsz
));
230 for (i
= 0; i
< valsz
; i
++) {
233 if (nvlist_print_json_string(fp
, val
[i
]) == -1)
240 case DATA_TYPE_NVLIST_ARRAY
: {
243 VERIFY0(nvpair_value_nvlist_array(curr
, &val
, &valsz
));
245 for (i
= 0; i
< valsz
; i
++) {
248 if (nvlist_print_json(fp
, val
[i
]) == -1)
255 case DATA_TYPE_BOOLEAN_ARRAY
: {
258 VERIFY0(nvpair_value_boolean_array(curr
, &val
, &valsz
));
260 for (i
= 0; i
< valsz
; i
++) {
263 FPRINTF(fp
, val
[i
] == B_TRUE
?
270 case DATA_TYPE_BYTE_ARRAY
: {
273 VERIFY0(nvpair_value_byte_array(curr
, &val
, &valsz
));
275 for (i
= 0; i
< valsz
; i
++) {
278 FPRINTF(fp
, "%hhu", val
[i
]);
284 case DATA_TYPE_UINT8_ARRAY
: {
287 VERIFY0(nvpair_value_uint8_array(curr
, &val
, &valsz
));
289 for (i
= 0; i
< valsz
; i
++) {
292 FPRINTF(fp
, "%hhu", val
[i
]);
298 case DATA_TYPE_INT8_ARRAY
: {
301 VERIFY0(nvpair_value_int8_array(curr
, &val
, &valsz
));
303 for (i
= 0; i
< valsz
; i
++) {
306 FPRINTF(fp
, "%hd", val
[i
]);
312 case DATA_TYPE_UINT16_ARRAY
: {
315 VERIFY0(nvpair_value_uint16_array(curr
, &val
, &valsz
));
317 for (i
= 0; i
< valsz
; i
++) {
320 FPRINTF(fp
, "%hu", val
[i
]);
326 case DATA_TYPE_INT16_ARRAY
: {
329 VERIFY0(nvpair_value_int16_array(curr
, &val
, &valsz
));
331 for (i
= 0; i
< valsz
; i
++) {
334 FPRINTF(fp
, "%hd", val
[i
]);
340 case DATA_TYPE_UINT32_ARRAY
: {
343 VERIFY0(nvpair_value_uint32_array(curr
, &val
, &valsz
));
345 for (i
= 0; i
< valsz
; i
++) {
348 FPRINTF(fp
, "%u", val
[i
]);
354 case DATA_TYPE_INT32_ARRAY
: {
357 VERIFY0(nvpair_value_int32_array(curr
, &val
, &valsz
));
359 for (i
= 0; i
< valsz
; i
++) {
362 FPRINTF(fp
, "%d", val
[i
]);
368 case DATA_TYPE_UINT64_ARRAY
: {
371 VERIFY0(nvpair_value_uint64_array(curr
, &val
, &valsz
));
373 for (i
= 0; i
< valsz
; i
++) {
377 (unsigned long long)val
[i
]);
383 case DATA_TYPE_INT64_ARRAY
: {
386 VERIFY0(nvpair_value_int64_array(curr
, &val
, &valsz
));
388 for (i
= 0; i
< valsz
; i
++) {
391 FPRINTF(fp
, "%lld", (long long)val
[i
]);
397 case DATA_TYPE_UNKNOWN
:
398 case DATA_TYPE_DONTCARE
: