Skip to content

Commit b3e9b8c

Browse files
committed
Add javadocs
1 parent 7abd64d commit b3e9b8c

File tree

5 files changed

+479
-14
lines changed

5 files changed

+479
-14
lines changed

src/main/java/io/github/malczuuu/problem4j/core/JsonEscape.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
/**
7+
* Utility class for escaping strings for safe inclusion in JSON.
8+
*
9+
* <p>Replaces control characters, special characters, and Unicode ranges with appropriate escape
10+
* sequences to produce valid JSON string literals.
11+
*/
612
final class JsonEscape {
713

14+
/** Mapping of characters to their JSON escape sequences. */
815
private static final Map<Character, String> REPLACEMENTS = new HashMap<>();
916

1017
static {
@@ -18,6 +25,18 @@ final class JsonEscape {
1825
REPLACEMENTS.put('/', "\\/");
1926
}
2027

28+
/** Private constructor to prevent instantiation. */
29+
private JsonEscape() {}
30+
31+
/**
32+
* Escapes the given string for inclusion in JSON.
33+
*
34+
* <p>Replaces control characters, special characters, and certain Unicode ranges with appropriate
35+
* escape sequences.
36+
*
37+
* @param value the string to escape
38+
* @return the escaped string
39+
*/
2140
static String escape(String value) {
2241
StringBuilder result = new StringBuilder();
2342

@@ -34,24 +53,46 @@ static String escape(String value) {
3453
return result.toString();
3554
}
3655

56+
/**
57+
* Determines if the character should be replaced using a predefined escape sequence.
58+
*
59+
* @param character the character to check
60+
* @return true if the character has a predefined replacement
61+
*/
3762
private static boolean shouldBeReplaced(char character) {
3863
return REPLACEMENTS.containsKey(character);
3964
}
4065

66+
/**
67+
* Appends the JSON escape sequence for a character that has a predefined replacement.
68+
*
69+
* @param result the StringBuilder to append to
70+
* @param character the character to escape
71+
*/
4172
private static void replace(StringBuilder result, char character) {
4273
result.append(REPLACEMENTS.get(character));
4374
}
4475

76+
/**
77+
* Determines if the character should be escaped using a Unicode hexadecimal escape.
78+
*
79+
* <p>Characters that are control characters or in certain Unicode ranges are escaped.
80+
*
81+
* @param character the character to check
82+
* @return true if the character should be represented as a Unicode escape
83+
*/
4584
private static boolean shouldBeHexed(char character) {
4685
return character <= '\u001F'
4786
|| character >= '\u007F' && character <= '\u009F'
4887
|| character >= '\u2000' && character <= '\u20FF';
4988
}
5089

5190
/**
52-
* Appends the Unicode hexadecimal escape sequence for the given character to the result. The
53-
* escape sequence is in the format {@code "\\uXXXX"}, where {@code "XXXX"} is the uppercase
54-
* hexadecimal representation of the character code, padded with leading zeros to four digits.
91+
* Appends the Unicode hexadecimal escape sequence for the given character to the result.
92+
*
93+
* <p>The escape sequence is in the format {@code "\\uXXXX"}, where {@code "XXXX"} is the
94+
* uppercase hexadecimal representation of the character code, padded with leading zeros to four
95+
* digits.
5596
*
5697
* @param result the StringBuilder to append the escape sequence to
5798
* @param character the character to be escaped

0 commit comments

Comments
 (0)