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.
19 #include <sys/debug.h>
21 #include "libnvpair.h"
23 #define FPRINTF(fp, ...) \
25 if (fprintf(fp, __VA_ARGS__) < 0) \
30 * When formatting a string for JSON output we must escape certain characters,
31 * as described in RFC4627. This applies to both member names and
32 * DATA_TYPE_STRING values.
34 * This function will only operate correctly if the following conditions are
37 * 1. The input String is encoded in the current locale.
39 * 2. The current locale includes the Basic Multilingual Plane (plane 0)
40 * as defined in the Unicode standard.
42 * The output will be entirely 7-bit ASCII (as a subset of UTF-8) with all
43 * representable Unicode characters included in their escaped numeric form.
46 nvlist_print_json_string(FILE *fp, const char *input)
52 bzero(&mbr, sizeof (mbr));
55 while ((sz = mbrtowc(&c, input, MB_CUR_MAX, &mbr)) > 0) {
79 if ((c >= 0x00 && c <= 0x1f) ||
80 (c > 0x7f && c <= 0xffff)) {
82 * Render both Control Characters and Unicode
83 * characters in the Basic Multilingual Plane
84 * as JSON-escaped multibyte characters.
86 FPRINTF(fp, "\\u%04x", (int)(0xffff & c));
87 } else if (c >= 0x20 && c <= 0x7f) {
89 * Render other 7-bit ASCII characters directly
90 * and drop other, unrepresentable characters.
92 FPRINTF(fp, "%c", (int)(0xff & c));
99 if (sz == (size_t)-1 || sz == (size_t)-2) {
101 * We last read an invalid multibyte character sequence,
102 * so return an error.
112 * Dump a JSON-formatted representation of an nvlist to the provided FILE *.
113 * This routine does not output any new-lines or additional whitespace other
114 * than that contained in strings, nor does it call fflush(3C).
117 nvlist_print_json(FILE *fp, nvlist_t *nvl)
120 boolean_t first = B_TRUE;
124 for (curr = nvlist_next_nvpair(nvl, NULL); curr;
125 curr = nvlist_next_nvpair(nvl, curr)) {
126 data_type_t type = nvpair_type(curr);
133 if (nvlist_print_json_string(fp, nvpair_name(curr)) == -1)
138 case DATA_TYPE_STRING: {
139 char *string = fnvpair_value_string(curr);
140 if (nvlist_print_json_string(fp, string) == -1)
145 case DATA_TYPE_BOOLEAN: {
150 case DATA_TYPE_BOOLEAN_VALUE: {
151 FPRINTF(fp, "%s", fnvpair_value_boolean_value(curr) ==
152 B_TRUE ? "true" : "false");
156 case DATA_TYPE_BYTE: {
157 FPRINTF(fp, "%hhu", fnvpair_value_byte(curr));
161 case DATA_TYPE_INT8: {
162 FPRINTF(fp, "%hhd", fnvpair_value_int8(curr));
166 case DATA_TYPE_UINT8: {
167 FPRINTF(fp, "%hhu", fnvpair_value_uint8_t(curr));
171 case DATA_TYPE_INT16: {
172 FPRINTF(fp, "%hd", fnvpair_value_int16(curr));
176 case DATA_TYPE_UINT16: {
177 FPRINTF(fp, "%hu", fnvpair_value_uint16(curr));
181 case DATA_TYPE_INT32: {
182 FPRINTF(fp, "%d", fnvpair_value_int32(curr));
186 case DATA_TYPE_UINT32: {
187 FPRINTF(fp, "%u", fnvpair_value_uint32(curr));
191 case DATA_TYPE_INT64: {
193 (long long)fnvpair_value_int64(curr));
197 case DATA_TYPE_UINT64: {
199 (unsigned long long)fnvpair_value_uint64(curr));
203 case DATA_TYPE_HRTIME: {
205 VERIFY0(nvpair_value_hrtime(curr, &val));
206 FPRINTF(fp, "%llu", (unsigned long long)val);
210 case DATA_TYPE_DOUBLE: {
212 VERIFY0(nvpair_value_double(curr, &val));
213 FPRINTF(fp, "%f", val);
217 case DATA_TYPE_NVLIST: {
218 if (nvlist_print_json(fp,
219 fnvpair_value_nvlist(curr)) == -1)
224 case DATA_TYPE_STRING_ARRAY: {
227 VERIFY0(nvpair_value_string_array(curr, &val, &valsz));
229 for (i = 0; i < valsz; i++) {
232 if (nvlist_print_json_string(fp, val[i]) == -1)
239 case DATA_TYPE_NVLIST_ARRAY: {
242 VERIFY0(nvpair_value_nvlist_array(curr, &val, &valsz));
244 for (i = 0; i < valsz; i++) {
247 if (nvlist_print_json(fp, val[i]) == -1)
254 case DATA_TYPE_BOOLEAN_ARRAY: {
257 VERIFY0(nvpair_value_boolean_array(curr, &val, &valsz));
259 for (i = 0; i < valsz; i++) {
262 FPRINTF(fp, val[i] == B_TRUE ?
269 case DATA_TYPE_BYTE_ARRAY: {
272 VERIFY0(nvpair_value_byte_array(curr, &val, &valsz));
274 for (i = 0; i < valsz; i++) {
277 FPRINTF(fp, "%hhu", val[i]);
283 case DATA_TYPE_UINT8_ARRAY: {
286 VERIFY0(nvpair_value_uint8_array(curr, &val, &valsz));
288 for (i = 0; i < valsz; i++) {
291 FPRINTF(fp, "%hhu", val[i]);
297 case DATA_TYPE_INT8_ARRAY: {
300 VERIFY0(nvpair_value_int8_array(curr, &val, &valsz));
302 for (i = 0; i < valsz; i++) {
305 FPRINTF(fp, "%hhd", val[i]);
311 case DATA_TYPE_UINT16_ARRAY: {
314 VERIFY0(nvpair_value_uint16_array(curr, &val, &valsz));
316 for (i = 0; i < valsz; i++) {
319 FPRINTF(fp, "%hu", val[i]);
325 case DATA_TYPE_INT16_ARRAY: {
328 VERIFY0(nvpair_value_int16_array(curr, &val, &valsz));
330 for (i = 0; i < valsz; i++) {
333 FPRINTF(fp, "%hd", val[i]);
339 case DATA_TYPE_UINT32_ARRAY: {
342 VERIFY0(nvpair_value_uint32_array(curr, &val, &valsz));
344 for (i = 0; i < valsz; i++) {
347 FPRINTF(fp, "%u", val[i]);
353 case DATA_TYPE_INT32_ARRAY: {
356 VERIFY0(nvpair_value_int32_array(curr, &val, &valsz));
358 for (i = 0; i < valsz; i++) {
361 FPRINTF(fp, "%d", val[i]);
367 case DATA_TYPE_UINT64_ARRAY: {
370 VERIFY0(nvpair_value_uint64_array(curr, &val, &valsz));
372 for (i = 0; i < valsz; i++) {
376 (unsigned long long)val[i]);
382 case DATA_TYPE_INT64_ARRAY: {
385 VERIFY0(nvpair_value_int64_array(curr, &val, &valsz));
387 for (i = 0; i < valsz; i++) {
390 FPRINTF(fp, "%lld", (long long)val[i]);
396 case DATA_TYPE_UNKNOWN: