com.ajaxjs.js.jsonparser.lexer.StringToken Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ajaxjs-base Show documentation
Show all versions of ajaxjs-base Show documentation
AJAXJS aims to full-stack, not only the server-side framework,
but also integrates the front-end library. It's written in HTML5 + Java, a successor to the JVM platform, efficient, secure, stable, cross-platform and many other advantages, but it abandoned the traditional enterprise architecture brought about by the large and bloated,
emphasizing the lightweight, and fast, very suitable for the Internet fast application.
/**
* Copyright Sp42 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ajaxjs.js.jsonparser.lexer;
import com.ajaxjs.js.jsonparser.JsonParseException;
/**
* 字符串 Token
*
* @author Sp42 [email protected]
*/
public class StringToken extends Token {
/**
* 创建字符串 Token
*
* @param value
* 这是 JSON 字符串上的那个原始值。
*/
public StringToken(String value) {
super(0, "STR", "字符串", null, value);
}
/**
* 转义
*
* @param str
* @return 转义后的结果
*/
static String unescape(String str) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '\\') {
c = str.charAt(++i);// 游标前进一个字符
switch (c) {
case '"':
sb.append('"');
break;
case '\\':
sb.append('\\');
break;
case '/':
sb.append('/');
break;
case 'b':
sb.append('\b');
break;
case 'f':
sb.append('\f');
break;
case 'n':
sb.append('\n');
break;
case 'r':
sb.append('\r');
break;
case 't':
sb.append('\t');
break;
case 'u':
String hex = str.substring(i + 1, i + 5);
sb.append((char) Integer.parseInt(hex, 16));
i += 4;
break;
default:
throw new JsonParseException("“\\”后面期待“\"\\/bfnrtu”中的字符,结果得到“" + c + "”");
}
} else {
sb.append(c);
}
}
return sb.toString();
}
}