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 package oracle.code.json; 27 28 import oracle.code.json.impl.JsonStringImpl; 29 30 import java.util.Objects; 31 32 /** 33 * The interface that represents JSON string. Any character may be escaped, 34 * see the JSON string <a href="https://datatracker.ietf.org/doc/html/rfc8259#section-6"> 35 * syntax</a> for the full list of two-character sequence escapes as well as 36 * the characters that must be escaped. 37 * <p> 38 * A {@code JsonString} can be produced by a {@link Json#parse(String)}. 39 * <p> Alternatively, {@link #of(String)} can be used to obtain a {@code JsonString} 40 * from a {@code String}. 41 * 42 * @spec https://datatracker.ietf.org/doc/html/rfc8259#section-7 RFC 8259: 43 * The JavaScript Object Notation (JSON) Data Interchange Format - Strings 44 * @since 99 45 */ 46 public non-sealed interface JsonString extends JsonValue { 47 48 /** 49 * {@return the {@code JsonString} created from the given 50 * {@code String}} 51 * 52 * @param src the given {@code String}. Non-null. 53 * @throws IllegalArgumentException if the given {@code src} is 54 * not a valid JSON string. 55 * @throws NullPointerException if {@code src} is {@code null} 56 */ 57 static JsonString of(String src) { 58 Objects.requireNonNull(src); 59 return new JsonStringImpl(src); 60 } 61 62 /** 63 * {@return the {@code String} value represented by this {@code JsonString}} 64 * Any escaped characters in the original JSON string are converted to their 65 * unescaped form in the returned {@code String}. 66 * 67 * @see #toString() 68 */ 69 String value(); 70 71 /** 72 * {@return the {@code String} value represented by this {@code JsonString} 73 * surrounded by quotation marks} Any escaped characters in the original JSON 74 * string are converted to their unescaped form in the returned {@code String}. 75 * 76 * @see #value() 77 */ 78 @Override 79 String toString(); 80 81 /** 82 * {@return true if the given {@code obj} is equal to this {@code JsonString}} 83 * Two {@code JsonString}s {@code js1} and {@code js2} represent the same value 84 * if {@code js1.value().equals(js2.value())}. 85 * 86 * @see #value() 87 */ 88 @Override 89 boolean equals(Object obj); 90 91 /** 92 * {@return the hash code value of this {@code JsonString}} The hash code of a 93 * {@code JsonString} is calculated by {@code Objects.hash(JsonString.value())}. 94 * 95 * @see #value() 96 */ 97 @Override 98 int hashCode(); 99 }