source: josm/trunk/src/org/glassfish/json/JsonStringImpl.java@ 13763

Last change on this file since 13763 was 13231, checked in by Don-vip, 7 years ago

see #15682 - upgrade to JSR 374 (JSON Processing) API 1.1.2

File size: 4.2 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can
10 * obtain a copy of the License at
11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
12 * or LICENSE.txt. See the License for the specific
13 * language governing permissions and limitations under the License.
14 *
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at LICENSE.txt.
17 *
18 * GPL Classpath Exception:
19 * Oracle designates this particular file as subject to the "Classpath"
20 * exception as provided by Oracle in the GPL Version 2 section of the License
21 * file that accompanied this code.
22 *
23 * Modifications:
24 * If applicable, add the following below the License Header, with the fields
25 * enclosed by brackets [] replaced by your own identifying information:
26 * "Portions Copyright [year] [name of copyright owner]"
27 *
28 * Contributor(s):
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41package org.glassfish.json;
42
43import javax.json.JsonString;
44
45/**
46 * JsonString impl
47 *
48 * @author Jitendra Kotamraju
49 */
50final class JsonStringImpl implements JsonString {
51
52 private final String value;
53
54 JsonStringImpl(String value) {
55 this.value = value;
56 }
57
58 @Override
59 public String getString() {
60 return value;
61 }
62
63 @Override
64 public CharSequence getChars() {
65 return value;
66 }
67
68 @Override
69 public ValueType getValueType() {
70 return ValueType.STRING;
71 }
72
73 @Override
74 public int hashCode() {
75 return getString().hashCode();
76 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (this == obj){
81 return true;
82 }
83 if (!(obj instanceof JsonString)) {
84 return false;
85 }
86 JsonString other = (JsonString)obj;
87 return getString().equals(other.getString());
88 }
89
90 @Override
91 public String toString() {
92 StringBuilder sb = new StringBuilder();
93 sb.append('"');
94
95 for(int i = 0; i < value.length(); i++) {
96 char c = value.charAt(i);
97 // unescaped = %x20-21 | %x23-5B | %x5D-10FFFF
98 if (c >= 0x20 && c <= 0x10ffff && c != 0x22 && c != 0x5c) {
99 sb.append(c);
100 } else {
101 switch (c) {
102 case '"':
103 case '\\':
104 sb.append('\\'); sb.append(c);
105 break;
106 case '\b':
107 sb.append('\\'); sb.append('b');
108 break;
109 case '\f':
110 sb.append('\\'); sb.append('f');
111 break;
112 case '\n':
113 sb.append('\\'); sb.append('n');
114 break;
115 case '\r':
116 sb.append('\\'); sb.append('r');
117 break;
118 case '\t':
119 sb.append('\\'); sb.append('t');
120 break;
121 default:
122 String hex = "000" + Integer.toHexString(c);
123 sb.append("\\u").append(hex.substring(hex.length() - 4));
124 }
125 }
126 }
127
128 sb.append('"');
129 return sb.toString();
130 }
131}
132
Note: See TracBrowser for help on using the repository browser.