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
571d76f578
commit
5a1ae69c21
@ -1,11 +1,11 @@
|
|||||||
package cn.hutool.json;
|
package cn.hutool.json;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
|
||||||
@ -67,13 +67,13 @@ public class JSONTokener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从InputStream中构建
|
* 从InputStream中构建,使用UTF-8编码
|
||||||
*
|
*
|
||||||
* @param inputStream InputStream
|
* @param inputStream InputStream
|
||||||
* @param config JSON配置
|
* @param config JSON配置
|
||||||
*/
|
*/
|
||||||
public JSONTokener(InputStream inputStream, JSONConfig config) throws JSONException {
|
public JSONTokener(InputStream inputStream, JSONConfig config) throws JSONException {
|
||||||
this(new InputStreamReader(inputStream), config);
|
this(IoUtil.getUtf8Reader(inputStream), config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,7 @@ package cn.hutool.json;
|
|||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.collection.ArrayIter;
|
import cn.hutool.core.collection.ArrayIter;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.lang.Filter;
|
import cn.hutool.core.lang.Filter;
|
||||||
import cn.hutool.core.lang.mutable.Mutable;
|
import cn.hutool.core.lang.mutable.Mutable;
|
||||||
import cn.hutool.core.lang.mutable.MutablePair;
|
import cn.hutool.core.lang.mutable.MutablePair;
|
||||||
@ -13,6 +14,8 @@ import cn.hutool.json.serialize.GlobalSerializeMapping;
|
|||||||
import cn.hutool.json.serialize.JSONObjectSerializer;
|
import cn.hutool.json.serialize.JSONObjectSerializer;
|
||||||
import cn.hutool.json.serialize.JSONSerializer;
|
import cn.hutool.json.serialize.JSONSerializer;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.Reader;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -24,6 +27,8 @@ import java.util.ResourceBundle;
|
|||||||
* <li>Map 转 JSONObject,将键值对加入JSON对象</li>
|
* <li>Map 转 JSONObject,将键值对加入JSON对象</li>
|
||||||
* <li>Map.Entry 转 JSONObject</li>
|
* <li>Map.Entry 转 JSONObject</li>
|
||||||
* <li>CharSequence 转 JSONObject,使用JSONTokener解析</li>
|
* <li>CharSequence 转 JSONObject,使用JSONTokener解析</li>
|
||||||
|
* <li>{@link Reader} 转 JSONObject,使用JSONTokener解析</li>
|
||||||
|
* <li>{@link InputStream} 转 JSONObject,使用JSONTokener解析</li>
|
||||||
* <li>JSONTokener 转 JSONObject,直接解析</li>
|
* <li>JSONTokener 转 JSONObject,直接解析</li>
|
||||||
* <li>ResourceBundle 转 JSONObject</li>
|
* <li>ResourceBundle 转 JSONObject</li>
|
||||||
* <li>Bean 转 JSONObject,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象。例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"</li>
|
* <li>Bean 转 JSONObject,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象。例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"</li>
|
||||||
@ -75,7 +80,7 @@ public class ObjectMapper {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ArrayUtil.isArray(source) || source instanceof JSONArray) {
|
if (source instanceof JSONArray) {
|
||||||
// 不支持集合类型转换为JSONObject
|
// 不支持集合类型转换为JSONObject
|
||||||
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
|
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
|
||||||
}
|
}
|
||||||
@ -91,6 +96,12 @@ public class ObjectMapper {
|
|||||||
} else if (source instanceof CharSequence) {
|
} else if (source instanceof CharSequence) {
|
||||||
// 可能为JSON字符串
|
// 可能为JSON字符串
|
||||||
mapFromStr((CharSequence) source, jsonObject, filter);
|
mapFromStr((CharSequence) source, jsonObject, filter);
|
||||||
|
} else if (source instanceof Reader) {
|
||||||
|
mapFromTokener(new JSONTokener((Reader) source, jsonObject.getConfig()), jsonObject, filter);
|
||||||
|
} else if (source instanceof InputStream) {
|
||||||
|
mapFromTokener(new JSONTokener((InputStream) source, jsonObject.getConfig()), jsonObject, filter);
|
||||||
|
} else if (source instanceof byte[]) {
|
||||||
|
mapFromTokener(new JSONTokener(IoUtil.toStream((byte[]) source), jsonObject.getConfig()), jsonObject, filter);
|
||||||
} else if (source instanceof JSONTokener) {
|
} else if (source instanceof JSONTokener) {
|
||||||
// JSONTokener
|
// JSONTokener
|
||||||
mapFromTokener((JSONTokener) source, jsonObject, filter);
|
mapFromTokener((JSONTokener) source, jsonObject, filter);
|
||||||
@ -128,6 +139,12 @@ public class ObjectMapper {
|
|||||||
} else if (source instanceof CharSequence) {
|
} else if (source instanceof CharSequence) {
|
||||||
// JSON字符串
|
// JSON字符串
|
||||||
mapFromStr((CharSequence) source, jsonArray, filter);
|
mapFromStr((CharSequence) source, jsonArray, filter);
|
||||||
|
}else if (source instanceof Reader) {
|
||||||
|
mapFromTokener(new JSONTokener((Reader) source, jsonArray.getConfig()), jsonArray, filter);
|
||||||
|
} else if (source instanceof InputStream) {
|
||||||
|
mapFromTokener(new JSONTokener((InputStream) source, jsonArray.getConfig()), jsonArray, filter);
|
||||||
|
} else if (source instanceof byte[]) {
|
||||||
|
mapFromTokener(new JSONTokener(IoUtil.toStream((byte[]) source), jsonArray.getConfig()), jsonArray, filter);
|
||||||
} else if (source instanceof JSONTokener) {
|
} else if (source instanceof JSONTokener) {
|
||||||
mapFromTokener((JSONTokener) source, jsonArray, filter);
|
mapFromTokener((JSONTokener) source, jsonArray, filter);
|
||||||
} else {
|
} else {
|
||||||
|
@ -28,7 +28,10 @@ import org.junit.Assert;
|
|||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.StringReader;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -143,6 +146,37 @@ public class JSONObjectTest {
|
|||||||
Assert.assertEquals(new JSONArray(), json.getJSONObject("data").getJSONArray("cards"));
|
Assert.assertEquals(new JSONArray(), json.getJSONObject("data").getJSONArray("cards"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parseBytesTest() {
|
||||||
|
String jsonStr = "{'msg':'这里还没有内容','data':{'cards':[]},'ok':0}";
|
||||||
|
//noinspection MismatchedQueryAndUpdateOfCollection
|
||||||
|
JSONObject json = new JSONObject(jsonStr.getBytes(StandardCharsets.UTF_8));
|
||||||
|
Assert.assertEquals(new Integer(0), json.getInt("ok"));
|
||||||
|
Assert.assertEquals(new JSONArray(), json.getJSONObject("data").getJSONArray("cards"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parseReaderTest() {
|
||||||
|
String jsonStr = "{'msg':'这里还没有内容','data':{'cards':[]},'ok':0}";
|
||||||
|
final StringReader stringReader = new StringReader(jsonStr);
|
||||||
|
|
||||||
|
//noinspection MismatchedQueryAndUpdateOfCollection
|
||||||
|
JSONObject json = new JSONObject(stringReader);
|
||||||
|
Assert.assertEquals(new Integer(0), json.getInt("ok"));
|
||||||
|
Assert.assertEquals(new JSONArray(), json.getJSONObject("data").getJSONArray("cards"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parseInputStreamTest() {
|
||||||
|
String jsonStr = "{'msg':'这里还没有内容','data':{'cards':[]},'ok':0}";
|
||||||
|
final ByteArrayInputStream in = new ByteArrayInputStream(jsonStr.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
//noinspection MismatchedQueryAndUpdateOfCollection
|
||||||
|
JSONObject json = new JSONObject(in);
|
||||||
|
Assert.assertEquals(new Integer(0), json.getInt("ok"));
|
||||||
|
Assert.assertEquals(new JSONArray(), json.getJSONObject("data").getJSONArray("cards"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void parseStringWithBomTest() {
|
public void parseStringWithBomTest() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user