1+ /*
2+ * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
10+ *
11+ * This code is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ * version 2 for more details (a copy is included in the LICENSE file that
15+ * accompanied this code).
16+ *
17+ * You should have received a copy of the GNU General Public License version
18+ * 2 along with this work; if not, write to the Free Software Foundation,
19+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+ *
21+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+ /*
27+ * @test
28+ * @enablePreview
29+ * @summary Test escaped characters in JSON object keys
30+ * @run junit EscapedKeyBugTest
31+ */
32+
33+ package jdk .sandbox .java .util .json ;
34+
35+ import org .junit .jupiter .api .Test ;
36+ import static org .junit .jupiter .api .Assertions .*;
37+ import java .util .logging .Logger ;
38+
39+ public class EscapedKeyBugTest {
40+ private static final Logger LOGGER = Logger .getLogger (EscapedKeyBugTest .class .getName ());
41+
42+ @ Test
43+ public void testEscapedCharactersInKeys () {
44+ LOGGER .info ("Executing testEscapedCharactersInKeys" );
45+ // Should parse successfully with escaped characters in keys
46+ String json = """
47+ {"foo\\ nbar":1,"foo\\ tbar":2}
48+ """ ;
49+
50+ JsonValue result = Json .parse (json );
51+ JsonObject obj = (JsonObject ) result ;
52+
53+ // Verify both keys are parsed correctly
54+ assertEquals (1L , ((JsonNumber ) obj .members ().get ("foo\n bar" )).toNumber ());
55+ assertEquals (2L , ((JsonNumber ) obj .members ().get ("foo\t bar" )).toNumber ());
56+ }
57+
58+ @ Test
59+ public void testEscapedQuoteInKey () {
60+ LOGGER .info ("Executing testEscapedQuoteInKey" );
61+ // Test escaped quotes in keys
62+ String json = """
63+ {"foo\\ "bar":1}
64+ """ ;
65+
66+ JsonValue result = Json .parse (json );
67+ JsonObject obj = (JsonObject ) result ;
68+
69+ // Verify key with escaped quote is parsed correctly
70+ assertEquals (1L , ((JsonNumber ) obj .members ().get ("foo\" bar" )).toNumber ());
71+ }
72+
73+ @ Test
74+ public void testEscapedBackslashInKey () {
75+ LOGGER .info ("Executing testEscapedBackslashInKey" );
76+ // Test escaped backslashes in keys
77+ String json = """
78+ {"foo\\ \\ bar":1}
79+ """ ;
80+
81+ JsonValue result = Json .parse (json );
82+ JsonObject obj = (JsonObject ) result ;
83+
84+ // Verify key with escaped backslash is parsed correctly
85+ assertEquals (1L , ((JsonNumber ) obj .members ().get ("foo\\ bar" )).toNumber ());
86+ }
87+
88+ @ Test
89+ public void testMultipleEscapedCharactersInKey () {
90+ LOGGER .info ("Executing testMultipleEscapedCharactersInKey" );
91+ // Test multiple escaped characters in one key
92+ String json = """
93+ {"foo\\ n\\ t\\ "bar":1}
94+ """ ;
95+
96+ JsonValue result = Json .parse (json );
97+ JsonObject obj = (JsonObject ) result ;
98+
99+ // Verify key with multiple escaped characters is parsed correctly
100+ assertEquals (1L , ((JsonNumber ) obj .members ().get ("foo\n \t \" bar" )).toNumber ());
101+ }
102+ }
0 commit comments