26 27/* 28 * When formatting a string for JSON output we must escape certain characters, 29 * as described in RFC4627. This applies to both member names and 30 * DATA_TYPE_STRING values. 31 * 32 * This function will only operate correctly if the following conditions are 33 * met: 34 * 35 * 1. The input String is encoded in the current locale. 36 * 37 * 2. The current locale includes the Basic Multilingual Plane (plane 0) 38 * as defined in the Unicode standard. 39 * 40 * The output will be entirely 7-bit ASCII (as a subset of UTF-8) with all 41 * representable Unicode characters included in their escaped numeric form. 42 */ 43static int 44nvlist_print_json_string(FILE *fp, const char *input) 45{ 46 mbstate_t mbr; 47 wchar_t c; 48 size_t sz; 49 50 bzero(&mbr, sizeof (mbr)); 51 52 FPRINTF(fp, "\""); 53 while ((sz = mbrtowc(&c, input, MB_CUR_MAX, &mbr)) > 0) { 54 switch (c) { 55 case '"': 56 FPRINTF(fp, "\\\""); 57 break; 58 case '\n': 59 FPRINTF(fp, "\\n"); 60 break; 61 case '\r': 62 FPRINTF(fp, "\\r"); 63 break; 64 case '\\': 65 FPRINTF(fp, "\\\\"); 66 break; 67 case '\f': 68 FPRINTF(fp, "\\f"); 69 break; 70 case '\t': 71 FPRINTF(fp, "\\t"); 72 break; 73 case '\b': 74 FPRINTF(fp, "\\b"); 75 break; 76 default: 77 if ((c >= 0x00 && c <= 0x1f) || 78 (c > 0x7f && c <= 0xffff)) { 79 /* 80 * Render both Control Characters and Unicode 81 * characters in the Basic Multilingual Plane 82 * as JSON-escaped multibyte characters. 83 */ 84 FPRINTF(fp, "\\u%04x", (int)(0xffff & c)); 85 } else if (c >= 0x20 && c <= 0x7f) { 86 /* 87 * Render other 7-bit ASCII characters directly 88 * and drop other, unrepresentable characters. 89 */ 90 FPRINTF(fp, "%c", (int)(0xff & c)); 91 } 92 break; 93 } 94 input += sz; 95 } 96 97 if (sz == (size_t)-1 || sz == (size_t)-2) { 98 /* 99 * We last read an invalid multibyte character sequence, 100 * so return an error. 101 */ 102 return (-1); 103 } 104 105 FPRINTF(fp, "\""); 106 return (0); 107} 108 109/* 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). 113 */ 114int 115nvlist_print_json(FILE *fp, nvlist_t *nvl) 116{ 117 nvpair_t *curr; 118 boolean_t first = B_TRUE; 119 120 FPRINTF(fp, "{"); 121 122 for (curr = nvlist_next_nvpair(nvl, NULL); curr; 123 curr = nvlist_next_nvpair(nvl, curr)) { 124 data_type_t type = nvpair_type(curr); 125 126 if (!first) 127 FPRINTF(fp, ","); 128 else 129 first = B_FALSE; 130 131 if (nvlist_print_json_string(fp, nvpair_name(curr)) == -1) 132 return (-1); 133 FPRINTF(fp, ":"); 134 135 switch (type) { 136 case DATA_TYPE_STRING: { 137 char *string = fnvpair_value_string(curr); 138 if (nvlist_print_json_string(fp, string) == -1) 139 return (-1); 140 break; 141 } 142 143 case DATA_TYPE_BOOLEAN: { 144 FPRINTF(fp, "true"); 145 break; 146 } 147 148 case DATA_TYPE_BOOLEAN_VALUE: { 149 FPRINTF(fp, "%s", fnvpair_value_boolean_value(curr) == 150 B_TRUE ? "true" : "false"); 151 break; 152 } 153 154 case DATA_TYPE_BYTE: { 155 FPRINTF(fp, "%hhu", fnvpair_value_byte(curr)); 156 break; 157 } 158 159 case DATA_TYPE_INT8: { 160 FPRINTF(fp, "%hhd", fnvpair_value_int8(curr)); 161 break; 162 } 163 164 case DATA_TYPE_UINT8: { 165 FPRINTF(fp, "%hhu", fnvpair_value_uint8_t(curr)); 166 break; 167 } 168 169 case DATA_TYPE_INT16: { 170 FPRINTF(fp, "%hd", fnvpair_value_int16(curr)); 171 break; 172 } 173 174 case DATA_TYPE_UINT16: { 175 FPRINTF(fp, "%hu", fnvpair_value_uint16(curr)); 176 break; 177 } 178 179 case DATA_TYPE_INT32: { 180 FPRINTF(fp, "%d", fnvpair_value_int32(curr)); 181 break; 182 } 183 184 case DATA_TYPE_UINT32: { 185 FPRINTF(fp, "%u", fnvpair_value_uint32(curr)); 186 break; 187 } 188 189 case DATA_TYPE_INT64: { 190 FPRINTF(fp, "%lld", 191 (long long)fnvpair_value_int64(curr)); 192 break; 193 } 194 195 case DATA_TYPE_UINT64: { 196 FPRINTF(fp, "%llu", 197 (unsigned long long)fnvpair_value_uint64(curr)); 198 break; 199 } 200 201 case DATA_TYPE_HRTIME: { 202 hrtime_t val; 203 VERIFY0(nvpair_value_hrtime(curr, &val)); 204 FPRINTF(fp, "%llu", (unsigned long long)val); 205 break; 206 } 207 208 case DATA_TYPE_DOUBLE: { 209 double val; 210 VERIFY0(nvpair_value_double(curr, &val)); 211 FPRINTF(fp, "%f", val); 212 break; 213 } 214 215 case DATA_TYPE_NVLIST: { 216 if (nvlist_print_json(fp, 217 fnvpair_value_nvlist(curr)) == -1) 218 return (-1); 219 break; 220 } 221 222 case DATA_TYPE_STRING_ARRAY: { 223 char **val; 224 uint_t valsz, i; 225 VERIFY0(nvpair_value_string_array(curr, &val, &valsz)); 226 FPRINTF(fp, "["); 227 for (i = 0; i < valsz; i++) { 228 if (i > 0) 229 FPRINTF(fp, ","); 230 if (nvlist_print_json_string(fp, val[i]) == -1) 231 return (-1); 232 } 233 FPRINTF(fp, "]"); 234 break; 235 } 236 237 case DATA_TYPE_NVLIST_ARRAY: { 238 nvlist_t **val; 239 uint_t valsz, i; 240 VERIFY0(nvpair_value_nvlist_array(curr, &val, &valsz)); 241 FPRINTF(fp, "["); 242 for (i = 0; i < valsz; i++) { 243 if (i > 0) 244 FPRINTF(fp, ","); 245 if (nvlist_print_json(fp, val[i]) == -1) 246 return (-1); 247 } 248 FPRINTF(fp, "]"); 249 break; 250 } 251 252 case DATA_TYPE_BOOLEAN_ARRAY: { 253 boolean_t *val; 254 uint_t valsz, i; 255 VERIFY0(nvpair_value_boolean_array(curr, &val, &valsz)); 256 FPRINTF(fp, "["); 257 for (i = 0; i < valsz; i++) { 258 if (i > 0) 259 FPRINTF(fp, ","); 260 FPRINTF(fp, val[i] == B_TRUE ? 261 "true" : "false"); 262 } 263 FPRINTF(fp, "]"); 264 break; 265 } 266 267 case DATA_TYPE_BYTE_ARRAY: { 268 uchar_t *val; 269 uint_t valsz, i; 270 VERIFY0(nvpair_value_byte_array(curr, &val, &valsz)); 271 FPRINTF(fp, "["); 272 for (i = 0; i < valsz; i++) { 273 if (i > 0) 274 FPRINTF(fp, ","); 275 FPRINTF(fp, "%hhu", val[i]); 276 } 277 FPRINTF(fp, "]"); 278 break; 279 } 280 281 case DATA_TYPE_UINT8_ARRAY: { 282 uint8_t *val; 283 uint_t valsz, i; 284 VERIFY0(nvpair_value_uint8_array(curr, &val, &valsz)); 285 FPRINTF(fp, "["); 286 for (i = 0; i < valsz; i++) { 287 if (i > 0) 288 FPRINTF(fp, ","); 289 FPRINTF(fp, "%hhu", val[i]); 290 } 291 FPRINTF(fp, "]"); 292 break; 293 } 294 295 case DATA_TYPE_INT8_ARRAY: { 296 int8_t *val; 297 uint_t valsz, i; 298 VERIFY0(nvpair_value_int8_array(curr, &val, &valsz)); 299 FPRINTF(fp, "["); 300 for (i = 0; i < valsz; i++) { 301 if (i > 0) 302 FPRINTF(fp, ",");
| 28 29/* 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. 33 * 34 * This function will only operate correctly if the following conditions are 35 * met: 36 * 37 * 1. The input String is encoded in the current locale. 38 * 39 * 2. The current locale includes the Basic Multilingual Plane (plane 0) 40 * as defined in the Unicode standard. 41 * 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. 44 */ 45static int 46nvlist_print_json_string(FILE *fp, const char *input) 47{ 48 mbstate_t mbr; 49 wchar_t c; 50 size_t sz; 51 52 bzero(&mbr, sizeof (mbr)); 53 54 FPRINTF(fp, "\""); 55 while ((sz = mbrtowc(&c, input, MB_CUR_MAX, &mbr)) > 0) { 56 switch (c) { 57 case '"': 58 FPRINTF(fp, "\\\""); 59 break; 60 case '\n': 61 FPRINTF(fp, "\\n"); 62 break; 63 case '\r': 64 FPRINTF(fp, "\\r"); 65 break; 66 case '\\': 67 FPRINTF(fp, "\\\\"); 68 break; 69 case '\f': 70 FPRINTF(fp, "\\f"); 71 break; 72 case '\t': 73 FPRINTF(fp, "\\t"); 74 break; 75 case '\b': 76 FPRINTF(fp, "\\b"); 77 break; 78 default: 79 if ((c >= 0x00 && c <= 0x1f) || 80 (c > 0x7f && c <= 0xffff)) { 81 /* 82 * Render both Control Characters and Unicode 83 * characters in the Basic Multilingual Plane 84 * as JSON-escaped multibyte characters. 85 */ 86 FPRINTF(fp, "\\u%04x", (int)(0xffff & c)); 87 } else if (c >= 0x20 && c <= 0x7f) { 88 /* 89 * Render other 7-bit ASCII characters directly 90 * and drop other, unrepresentable characters. 91 */ 92 FPRINTF(fp, "%c", (int)(0xff & c)); 93 } 94 break; 95 } 96 input += sz; 97 } 98 99 if (sz == (size_t)-1 || sz == (size_t)-2) { 100 /* 101 * We last read an invalid multibyte character sequence, 102 * so return an error. 103 */ 104 return (-1); 105 } 106 107 FPRINTF(fp, "\""); 108 return (0); 109} 110 111/* 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). 115 */ 116int 117nvlist_print_json(FILE *fp, nvlist_t *nvl) 118{ 119 nvpair_t *curr; 120 boolean_t first = B_TRUE; 121 122 FPRINTF(fp, "{"); 123 124 for (curr = nvlist_next_nvpair(nvl, NULL); curr; 125 curr = nvlist_next_nvpair(nvl, curr)) { 126 data_type_t type = nvpair_type(curr); 127 128 if (!first) 129 FPRINTF(fp, ","); 130 else 131 first = B_FALSE; 132 133 if (nvlist_print_json_string(fp, nvpair_name(curr)) == -1) 134 return (-1); 135 FPRINTF(fp, ":"); 136 137 switch (type) { 138 case DATA_TYPE_STRING: { 139 char *string = fnvpair_value_string(curr); 140 if (nvlist_print_json_string(fp, string) == -1) 141 return (-1); 142 break; 143 } 144 145 case DATA_TYPE_BOOLEAN: { 146 FPRINTF(fp, "true"); 147 break; 148 } 149 150 case DATA_TYPE_BOOLEAN_VALUE: { 151 FPRINTF(fp, "%s", fnvpair_value_boolean_value(curr) == 152 B_TRUE ? "true" : "false"); 153 break; 154 } 155 156 case DATA_TYPE_BYTE: { 157 FPRINTF(fp, "%hhu", fnvpair_value_byte(curr)); 158 break; 159 } 160 161 case DATA_TYPE_INT8: { 162 FPRINTF(fp, "%hhd", fnvpair_value_int8(curr)); 163 break; 164 } 165 166 case DATA_TYPE_UINT8: { 167 FPRINTF(fp, "%hhu", fnvpair_value_uint8_t(curr)); 168 break; 169 } 170 171 case DATA_TYPE_INT16: { 172 FPRINTF(fp, "%hd", fnvpair_value_int16(curr)); 173 break; 174 } 175 176 case DATA_TYPE_UINT16: { 177 FPRINTF(fp, "%hu", fnvpair_value_uint16(curr)); 178 break; 179 } 180 181 case DATA_TYPE_INT32: { 182 FPRINTF(fp, "%d", fnvpair_value_int32(curr)); 183 break; 184 } 185 186 case DATA_TYPE_UINT32: { 187 FPRINTF(fp, "%u", fnvpair_value_uint32(curr)); 188 break; 189 } 190 191 case DATA_TYPE_INT64: { 192 FPRINTF(fp, "%lld", 193 (long long)fnvpair_value_int64(curr)); 194 break; 195 } 196 197 case DATA_TYPE_UINT64: { 198 FPRINTF(fp, "%llu", 199 (unsigned long long)fnvpair_value_uint64(curr)); 200 break; 201 } 202 203 case DATA_TYPE_HRTIME: { 204 hrtime_t val; 205 VERIFY0(nvpair_value_hrtime(curr, &val)); 206 FPRINTF(fp, "%llu", (unsigned long long)val); 207 break; 208 } 209 210 case DATA_TYPE_DOUBLE: { 211 double val; 212 VERIFY0(nvpair_value_double(curr, &val)); 213 FPRINTF(fp, "%f", val); 214 break; 215 } 216 217 case DATA_TYPE_NVLIST: { 218 if (nvlist_print_json(fp, 219 fnvpair_value_nvlist(curr)) == -1) 220 return (-1); 221 break; 222 } 223 224 case DATA_TYPE_STRING_ARRAY: { 225 char **val; 226 uint_t valsz, i; 227 VERIFY0(nvpair_value_string_array(curr, &val, &valsz)); 228 FPRINTF(fp, "["); 229 for (i = 0; i < valsz; i++) { 230 if (i > 0) 231 FPRINTF(fp, ","); 232 if (nvlist_print_json_string(fp, val[i]) == -1) 233 return (-1); 234 } 235 FPRINTF(fp, "]"); 236 break; 237 } 238 239 case DATA_TYPE_NVLIST_ARRAY: { 240 nvlist_t **val; 241 uint_t valsz, i; 242 VERIFY0(nvpair_value_nvlist_array(curr, &val, &valsz)); 243 FPRINTF(fp, "["); 244 for (i = 0; i < valsz; i++) { 245 if (i > 0) 246 FPRINTF(fp, ","); 247 if (nvlist_print_json(fp, val[i]) == -1) 248 return (-1); 249 } 250 FPRINTF(fp, "]"); 251 break; 252 } 253 254 case DATA_TYPE_BOOLEAN_ARRAY: { 255 boolean_t *val; 256 uint_t valsz, i; 257 VERIFY0(nvpair_value_boolean_array(curr, &val, &valsz)); 258 FPRINTF(fp, "["); 259 for (i = 0; i < valsz; i++) { 260 if (i > 0) 261 FPRINTF(fp, ","); 262 FPRINTF(fp, val[i] == B_TRUE ? 263 "true" : "false"); 264 } 265 FPRINTF(fp, "]"); 266 break; 267 } 268 269 case DATA_TYPE_BYTE_ARRAY: { 270 uchar_t *val; 271 uint_t valsz, i; 272 VERIFY0(nvpair_value_byte_array(curr, &val, &valsz)); 273 FPRINTF(fp, "["); 274 for (i = 0; i < valsz; i++) { 275 if (i > 0) 276 FPRINTF(fp, ","); 277 FPRINTF(fp, "%hhu", val[i]); 278 } 279 FPRINTF(fp, "]"); 280 break; 281 } 282 283 case DATA_TYPE_UINT8_ARRAY: { 284 uint8_t *val; 285 uint_t valsz, i; 286 VERIFY0(nvpair_value_uint8_array(curr, &val, &valsz)); 287 FPRINTF(fp, "["); 288 for (i = 0; i < valsz; i++) { 289 if (i > 0) 290 FPRINTF(fp, ","); 291 FPRINTF(fp, "%hhu", val[i]); 292 } 293 FPRINTF(fp, "]"); 294 break; 295 } 296 297 case DATA_TYPE_INT8_ARRAY: { 298 int8_t *val; 299 uint_t valsz, i; 300 VERIFY0(nvpair_value_int8_array(curr, &val, &valsz)); 301 FPRINTF(fp, "["); 302 for (i = 0; i < valsz; i++) { 303 if (i > 0) 304 FPRINTF(fp, ",");
|
332 } 333 FPRINTF(fp, "]"); 334 break; 335 } 336 337 case DATA_TYPE_UINT32_ARRAY: { 338 uint32_t *val; 339 uint_t valsz, i; 340 VERIFY0(nvpair_value_uint32_array(curr, &val, &valsz)); 341 FPRINTF(fp, "["); 342 for (i = 0; i < valsz; i++) { 343 if (i > 0) 344 FPRINTF(fp, ","); 345 FPRINTF(fp, "%u", val[i]); 346 } 347 FPRINTF(fp, "]"); 348 break; 349 } 350 351 case DATA_TYPE_INT32_ARRAY: { 352 int32_t *val; 353 uint_t valsz, i; 354 VERIFY0(nvpair_value_int32_array(curr, &val, &valsz)); 355 FPRINTF(fp, "["); 356 for (i = 0; i < valsz; i++) { 357 if (i > 0) 358 FPRINTF(fp, ","); 359 FPRINTF(fp, "%d", val[i]); 360 } 361 FPRINTF(fp, "]"); 362 break; 363 } 364 365 case DATA_TYPE_UINT64_ARRAY: { 366 uint64_t *val; 367 uint_t valsz, i; 368 VERIFY0(nvpair_value_uint64_array(curr, &val, &valsz)); 369 FPRINTF(fp, "["); 370 for (i = 0; i < valsz; i++) { 371 if (i > 0) 372 FPRINTF(fp, ","); 373 FPRINTF(fp, "%llu", 374 (unsigned long long)val[i]); 375 } 376 FPRINTF(fp, "]"); 377 break; 378 } 379 380 case DATA_TYPE_INT64_ARRAY: { 381 int64_t *val; 382 uint_t valsz, i; 383 VERIFY0(nvpair_value_int64_array(curr, &val, &valsz)); 384 FPRINTF(fp, "["); 385 for (i = 0; i < valsz; i++) { 386 if (i > 0) 387 FPRINTF(fp, ","); 388 FPRINTF(fp, "%lld", (long long)val[i]); 389 } 390 FPRINTF(fp, "]"); 391 break; 392 } 393 394 case DATA_TYPE_UNKNOWN: 395 return (-1); 396 } 397 } 398 399 FPRINTF(fp, "}"); 400 return (0); 401}
| 334 } 335 FPRINTF(fp, "]"); 336 break; 337 } 338 339 case DATA_TYPE_UINT32_ARRAY: { 340 uint32_t *val; 341 uint_t valsz, i; 342 VERIFY0(nvpair_value_uint32_array(curr, &val, &valsz)); 343 FPRINTF(fp, "["); 344 for (i = 0; i < valsz; i++) { 345 if (i > 0) 346 FPRINTF(fp, ","); 347 FPRINTF(fp, "%u", val[i]); 348 } 349 FPRINTF(fp, "]"); 350 break; 351 } 352 353 case DATA_TYPE_INT32_ARRAY: { 354 int32_t *val; 355 uint_t valsz, i; 356 VERIFY0(nvpair_value_int32_array(curr, &val, &valsz)); 357 FPRINTF(fp, "["); 358 for (i = 0; i < valsz; i++) { 359 if (i > 0) 360 FPRINTF(fp, ","); 361 FPRINTF(fp, "%d", val[i]); 362 } 363 FPRINTF(fp, "]"); 364 break; 365 } 366 367 case DATA_TYPE_UINT64_ARRAY: { 368 uint64_t *val; 369 uint_t valsz, i; 370 VERIFY0(nvpair_value_uint64_array(curr, &val, &valsz)); 371 FPRINTF(fp, "["); 372 for (i = 0; i < valsz; i++) { 373 if (i > 0) 374 FPRINTF(fp, ","); 375 FPRINTF(fp, "%llu", 376 (unsigned long long)val[i]); 377 } 378 FPRINTF(fp, "]"); 379 break; 380 } 381 382 case DATA_TYPE_INT64_ARRAY: { 383 int64_t *val; 384 uint_t valsz, i; 385 VERIFY0(nvpair_value_int64_array(curr, &val, &valsz)); 386 FPRINTF(fp, "["); 387 for (i = 0; i < valsz; i++) { 388 if (i > 0) 389 FPRINTF(fp, ","); 390 FPRINTF(fp, "%lld", (long long)val[i]); 391 } 392 FPRINTF(fp, "]"); 393 break; 394 } 395 396 case DATA_TYPE_UNKNOWN: 397 return (-1); 398 } 399 } 400 401 FPRINTF(fp, "}"); 402 return (0); 403}
|