mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
8ee7124797
commit
a8ca13d8d6
@ -6,7 +6,11 @@ import cn.hutool.core.lang.mutable.MutableEntry;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* JSON字符串解析器
|
||||
* JSON字符串解析器,实现:
|
||||
* <ul>
|
||||
* <li>JSON字符串 --> {@link JSONTokener} --> {@link JSONObject}</li>
|
||||
* <li>JSON字符串 --> {@link JSONTokener} --> {@link JSONArray}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.8.0
|
||||
|
@ -12,6 +12,7 @@ import cn.hutool.json.serialize.GlobalSerializeMapping;
|
||||
import cn.hutool.json.serialize.JSONArraySerializer;
|
||||
import cn.hutool.json.serialize.JSONDeserializer;
|
||||
import cn.hutool.json.serialize.JSONObjectSerializer;
|
||||
import cn.hutool.json.xml.JSONXMLUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -201,7 +202,7 @@ public class JSONUtil {
|
||||
* @return JSONObject
|
||||
*/
|
||||
public static JSONObject parseFromXml(final String xmlStr) {
|
||||
return XML.toJSONObject(xmlStr);
|
||||
return JSONXMLUtil.toJSONObject(xmlStr);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------- Parse end
|
||||
@ -362,7 +363,7 @@ public class JSONUtil {
|
||||
* @return XML字符串
|
||||
*/
|
||||
public static String toXmlStr(final JSON json) {
|
||||
return XML.toXml(json);
|
||||
return JSONXMLUtil.toXml(json);
|
||||
}
|
||||
// -------------------------------------------------------------------- toString end
|
||||
|
||||
@ -735,7 +736,7 @@ public class JSONUtil {
|
||||
* @since 4.0.8
|
||||
*/
|
||||
public static JSONObject xmlToJson(final String xml) {
|
||||
return XML.toJSONObject(xml);
|
||||
return JSONXMLUtil.toJSONObject(xml);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,7 +12,7 @@ import cn.hutool.json.JSONException;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONParser;
|
||||
import cn.hutool.json.JSONTokener;
|
||||
import cn.hutool.json.XML;
|
||||
import cn.hutool.json.xml.JSONXMLUtil;
|
||||
import cn.hutool.json.serialize.GlobalSerializeMapping;
|
||||
import cn.hutool.json.serialize.JSONSerializer;
|
||||
|
||||
@ -149,7 +149,7 @@ public class ObjectMapper {
|
||||
final String jsonStr = StrUtil.trim(source);
|
||||
if (StrUtil.startWith(jsonStr, '<')) {
|
||||
// 可能为XML
|
||||
XML.toJSONObject(jsonObject, jsonStr, false);
|
||||
JSONXMLUtil.toJSONObject(jsonObject, jsonStr, false);
|
||||
return;
|
||||
}
|
||||
mapFromTokener(new JSONTokener(StrUtil.trim(source), jsonObject.getConfig()), jsonObject);
|
||||
|
@ -1,5 +1,15 @@
|
||||
/**
|
||||
* JSON封装,基于json.org官方库改造
|
||||
* JSON(JavaScript Object Notation JavaScript对象表示法)封装,包含以下组件:
|
||||
* <ul>
|
||||
* <li>JSONObject:使用键值对表示的数据类型,使用"{}"包围</li>
|
||||
* <li>JSONArray:使用列表表示的数据类型,使用"[]"包围</li>
|
||||
* </ul>
|
||||
* JSON封装主要包括JSON表示和JSON转换:
|
||||
*
|
||||
* <pre>
|
||||
* Java对象 <----> JSON对象 <----> JSON字符串
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
|
@ -4,8 +4,6 @@ import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.json.InternalJSONUtil;
|
||||
import cn.hutool.json.JSONException;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.XML;
|
||||
import cn.hutool.json.XMLTokener;
|
||||
|
||||
/**
|
||||
* XML解析器,将XML解析为JSON对象
|
||||
@ -50,7 +48,7 @@ public class JSONXMLParser {
|
||||
|
||||
token = x.nextToken();
|
||||
|
||||
if (token == XML.BANG) {
|
||||
if (token == JSONXMLUtil.BANG) {
|
||||
c = x.next();
|
||||
if (c == '-') {
|
||||
if (x.next() == '-') {
|
||||
@ -76,19 +74,19 @@ public class JSONXMLParser {
|
||||
token = x.nextMeta();
|
||||
if (token == null) {
|
||||
throw x.syntaxError("Missing '>' after '<!'.");
|
||||
} else if (token == XML.LT) {
|
||||
} else if (token == JSONXMLUtil.LT) {
|
||||
i += 1;
|
||||
} else if (token == XML.GT) {
|
||||
} else if (token == JSONXMLUtil.GT) {
|
||||
i -= 1;
|
||||
}
|
||||
} while (i > 0);
|
||||
return false;
|
||||
} else if (token == XML.QUEST) {
|
||||
} else if (token == JSONXMLUtil.QUEST) {
|
||||
|
||||
// <?
|
||||
x.skipPast("?>");
|
||||
return false;
|
||||
} else if (token == XML.SLASH) {
|
||||
} else if (token == JSONXMLUtil.SLASH) {
|
||||
|
||||
// Close tag </
|
||||
|
||||
@ -99,7 +97,7 @@ public class JSONXMLParser {
|
||||
if (!token.equals(name)) {
|
||||
throw x.syntaxError("Mismatched " + name + " and " + token);
|
||||
}
|
||||
if (x.nextToken() != XML.GT) {
|
||||
if (x.nextToken() != JSONXMLUtil.GT) {
|
||||
throw x.syntaxError("Misshaped close tag");
|
||||
}
|
||||
return true;
|
||||
@ -122,7 +120,7 @@ public class JSONXMLParser {
|
||||
if (token instanceof String) {
|
||||
string = (String) token;
|
||||
token = x.nextToken();
|
||||
if (token == XML.EQ) {
|
||||
if (token == JSONXMLUtil.EQ) {
|
||||
token = x.nextToken();
|
||||
if (!(token instanceof String)) {
|
||||
throw x.syntaxError("Missing value");
|
||||
@ -133,9 +131,9 @@ public class JSONXMLParser {
|
||||
jsonobject.append(string, "");
|
||||
}
|
||||
|
||||
} else if (token == XML.SLASH) {
|
||||
} else if (token == JSONXMLUtil.SLASH) {
|
||||
// Empty tag <.../>
|
||||
if (x.nextToken() != XML.GT) {
|
||||
if (x.nextToken() != JSONXMLUtil.GT) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
if (jsonobject.size() > 0) {
|
||||
@ -145,7 +143,7 @@ public class JSONXMLParser {
|
||||
}
|
||||
return false;
|
||||
|
||||
} else if (token == XML.GT) {
|
||||
} else if (token == JSONXMLUtil.GT) {
|
||||
// Content, between <...> and </...>
|
||||
for (; ; ) {
|
||||
token = x.nextContent();
|
||||
@ -160,7 +158,7 @@ public class JSONXMLParser {
|
||||
jsonobject.append("content", keepStrings ? token : InternalJSONUtil.stringToValue(string));
|
||||
}
|
||||
|
||||
} else if (token == XML.LT) {
|
||||
} else if (token == JSONXMLUtil.LT) {
|
||||
// Nested element
|
||||
if (parse(x, jsonobject, tagName, keepStrings)) {
|
||||
if (jsonobject.size() == 0) {
|
||||
|
@ -1,8 +1,8 @@
|
||||
package cn.hutool.json;
|
||||
package cn.hutool.json.xml;
|
||||
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.json.xml.JSONXMLParser;
|
||||
import cn.hutool.json.xml.JSONXMLSerializer;
|
||||
import cn.hutool.json.JSONException;
|
||||
import cn.hutool.json.JSONObject;
|
||||
|
||||
/**
|
||||
* 提供静态方法在XML和JSONObject之间转换
|
||||
@ -11,7 +11,7 @@ import cn.hutool.json.xml.JSONXMLSerializer;
|
||||
* @see JSONXMLParser
|
||||
* @see JSONXMLSerializer
|
||||
*/
|
||||
public class XML {
|
||||
public class JSONXMLUtil {
|
||||
|
||||
/**
|
||||
* The Character '&'.
|
@ -1,4 +1,8 @@
|
||||
package cn.hutool.json;
|
||||
package cn.hutool.json.xml;
|
||||
|
||||
import cn.hutool.json.JSONConfig;
|
||||
import cn.hutool.json.JSONException;
|
||||
import cn.hutool.json.JSONTokener;
|
||||
|
||||
/**
|
||||
* XML分析器,继承自JSONTokener,提供XML的语法分析
|
||||
@ -15,11 +19,11 @@ public class XMLTokener extends JSONTokener {
|
||||
|
||||
static {
|
||||
entity = new java.util.HashMap<>(8);
|
||||
entity.put("amp", XML.AMP);
|
||||
entity.put("apos", XML.APOS);
|
||||
entity.put("gt", XML.GT);
|
||||
entity.put("lt", XML.LT);
|
||||
entity.put("quot", XML.QUOT);
|
||||
entity.put("amp", JSONXMLUtil.AMP);
|
||||
entity.put("apos", JSONXMLUtil.APOS);
|
||||
entity.put("gt", JSONXMLUtil.GT);
|
||||
entity.put("lt", JSONXMLUtil.LT);
|
||||
entity.put("quot", JSONXMLUtil.QUOT);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -73,7 +77,7 @@ public class XMLTokener extends JSONTokener {
|
||||
return null;
|
||||
}
|
||||
if (c == '<') {
|
||||
return XML.LT;
|
||||
return JSONXMLUtil.LT;
|
||||
}
|
||||
sb = new StringBuilder();
|
||||
for (; ; ) {
|
||||
@ -130,17 +134,17 @@ public class XMLTokener extends JSONTokener {
|
||||
case 0:
|
||||
throw syntaxError("Misshaped meta tag");
|
||||
case '<':
|
||||
return XML.LT;
|
||||
return JSONXMLUtil.LT;
|
||||
case '>':
|
||||
return XML.GT;
|
||||
return JSONXMLUtil.GT;
|
||||
case '/':
|
||||
return XML.SLASH;
|
||||
return JSONXMLUtil.SLASH;
|
||||
case '=':
|
||||
return XML.EQ;
|
||||
return JSONXMLUtil.EQ;
|
||||
case '!':
|
||||
return XML.BANG;
|
||||
return JSONXMLUtil.BANG;
|
||||
case '?':
|
||||
return XML.QUEST;
|
||||
return JSONXMLUtil.QUEST;
|
||||
case '"':
|
||||
case '\'':
|
||||
q = c;
|
||||
@ -197,15 +201,15 @@ public class XMLTokener extends JSONTokener {
|
||||
case '<':
|
||||
throw syntaxError("Misplaced '<'");
|
||||
case '>':
|
||||
return XML.GT;
|
||||
return JSONXMLUtil.GT;
|
||||
case '/':
|
||||
return XML.SLASH;
|
||||
return JSONXMLUtil.SLASH;
|
||||
case '=':
|
||||
return XML.EQ;
|
||||
return JSONXMLUtil.EQ;
|
||||
case '!':
|
||||
return XML.BANG;
|
||||
return JSONXMLUtil.BANG;
|
||||
case '?':
|
||||
return XML.QUEST;
|
||||
return JSONXMLUtil.QUEST;
|
||||
|
||||
// Quoted string
|
||||
|
@ -2,7 +2,6 @@ package cn.hutool.json.xml;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.json.XML;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.hamcrest.MatcherAssert;
|
||||
import org.junit.Assert;
|
||||
@ -22,11 +21,11 @@ public class XMLTest {
|
||||
@Test
|
||||
public void escapeTest(){
|
||||
final String xml = "<a>•</a>";
|
||||
final JSONObject jsonObject = XML.toJSONObject(xml);
|
||||
final JSONObject jsonObject = JSONXMLUtil.toJSONObject(xml);
|
||||
|
||||
Assert.assertEquals("{\"a\":\"•\"}", jsonObject.toString());
|
||||
|
||||
final String xml2 = XML.toXml(JSONUtil.parseObj(jsonObject));
|
||||
final String xml2 = JSONXMLUtil.toXml(JSONUtil.parseObj(jsonObject));
|
||||
Assert.assertEquals(xml, xml2);
|
||||
}
|
||||
|
||||
@ -34,10 +33,10 @@ public class XMLTest {
|
||||
public void xmlContentTest(){
|
||||
final JSONObject jsonObject = JSONUtil.createObj().set("content","123456");
|
||||
|
||||
String xml = XML.toXml(jsonObject);
|
||||
String xml = JSONXMLUtil.toXml(jsonObject);
|
||||
Assert.assertEquals("123456", xml);
|
||||
|
||||
xml = XML.toXml(jsonObject, null, new String[0]);
|
||||
xml = JSONXMLUtil.toXml(jsonObject, null, new String[0]);
|
||||
Assert.assertEquals("<content>123456</content>", xml);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user