Debian Codegraph @

src/org/eclipse/parsson/JsonStringImpl.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17 package org.eclipse.parsson;
18
19 import jakarta.json.JsonString;
20
21 /**
22 * JsonString impl
23 *
24 * @author Jitendra Kotamraju
25 */
26 final class JsonStringImpl implements JsonString {
27
28 private final CharSequence value;
29
30 JsonStringImpl(CharSequence value) {
31 this.value = value;
32 }
33
34 @Override
35 public String getString() {
36 return value.toString();
37 }
38
39 @Override
40 public CharSequence getChars() {
41 return value;
42 }
43
44 @Override
45 public ValueType getValueType() {
46 return ValueType.STRING;
47 }
48
49 @Override
50 public int hashCode() {
51 return getString().hashCode();
52 }
53
54 @Override
55 public boolean equals(Object obj) {
56 if (this == obj){
57 return true;
58 }
59 if (!(obj instanceof JsonString)) {
60 return false;
61 }
62 JsonString other = (JsonString)obj;
63 return getString().equals(other.getString());
64 }
65
66 @Override
67 public String toString() {
68 StringBuilder sb = new StringBuilder();
69 sb.append('"');
70
71 for(int i = 0; i < value.length(); i++) {
72 char c = value.charAt(i);
73 // unescaped = %x20-21 | %x23-5B | %x5D-10FFFF
74 if (c >= 0x20 && c <= 0x10ffff && c != 0x22 && c != 0x5c) {
75 sb.append(c);
76 } else {
77 switch (c) {
78 case '"':
79 case '\\':
80 sb.append('\\'); sb.append(c);
81 break;
82 case '\b':
83 sb.append('\\'); sb.append('b');
84 break;
85 case '\f':
86 sb.append('\\'); sb.append('f');
87 break;
88 case '\n':
89 sb.append('\\'); sb.append('n');
90 break;
91 case '\r':
92 sb.append('\\'); sb.append('r');
93 break;
94 case '\t':
95 sb.append('\\'); sb.append('t');
96 break;
97 default:
98 String hex = "000" + Integer.toHexString(c);
99 sb.append("\\u").append(hex.substring(hex.length() - 4));
100 }
101 }
102 }
103
104 sb.append('"');
105 return sb.toString();
106 }
107 }
108