| 422 | } |
| 423 | |
| 424 | private void string(String value) throws IOException { |
| 425 | out.write("\""); |
| 426 | for (int i = 0, length = value.length(); i < length; i++) { |
| 427 | char c = value.charAt(i); |
| 428 | |
| 429 | /* |
| 430 | * From RFC 4627, "All Unicode characters may be placed within the |
| 431 | * quotation marks except for the characters that must be escaped: |
| 432 | * quotation mark, reverse solidus, and the control characters |
| 433 | * (U+0000 through U+001F)." |
| 434 | */ |
| 435 | switch (c) { |
| 436 | case '"': |
| 437 | case '\\': |
| 438 | out.write('\\'); |
| 439 | out.write(c); |
| 440 | break; |
| 441 | |
| 442 | case '\t': |
| 443 | out.write("\\t"); |
| 444 | break; |
| 445 | |
| 446 | case '\b': |
| 447 | out.write("\\b"); |
| 448 | break; |
| 449 | |
| 450 | case '\n': |
| 451 | out.write("\\n"); |
| 452 | break; |
| 453 | |
| 454 | case '\r': |
| 455 | out.write("\\r"); |
| 456 | break; |
| 457 | |
| 458 | case '\f': |
| 459 | out.write("\\f"); |
| 460 | break; |
| 461 | |
| 462 | case '<': |
| 463 | case '>': |
| 464 | case '&': |
| 465 | case '=': |
| 466 | case '\'': |
| 467 | if (htmlSafe) { |
| 468 | out.write(String.format("\\u%04x", (int) c)); |
| 469 | } else { |
| 470 | out.write(c); |
| 471 | } |
| 472 | break; |
| 473 | |
| 474 | default: |
| 475 | if (c <= 0x1F) { |
| 476 | out.write(String.format("\\u%04x", (int) c)); |
| 477 | } else { |
| 478 | out.write(c); |
| 479 | } |
| 480 | break; |
| 481 | } |