Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

调整jsonFilter到jsonFormat中.

  • Loading branch information...
commit b875a31e2c043d6f4850fd7d5ae9c92988fe4e0e 1 parent 3c2db87
@juqkai juqkai authored
View
96 src/org/nutz/json/Json.java
@@ -10,8 +10,6 @@
import java.util.WeakHashMap;
import org.nutz.json.entity.JsonEntity;
-import org.nutz.json.filter.JsonCompileFilter;
-import org.nutz.json.filter.JsonRenderingFilter;
import org.nutz.lang.Lang;
import org.nutz.lang.Mirror;
import org.nutz.lang.Objs;
@@ -84,8 +82,8 @@ public static Object fromJson(Reader reader) throws JsonException {
* @return JAVA 对象
* @throws JsonException
*/
- public static Object fromJson(Reader reader, JsonFilter filter) throws JsonException {
- return new JsonCompileFilter().parse(reader, filter);
+ public static Object fromJson(Reader reader, JsonFormat format) throws JsonException {
+ return new JsonCompile(format).parse(reader);
}
/**
@@ -125,7 +123,7 @@ public static Object fromJson(Reader reader, JsonFilter filter) throws JsonExcep
* @throws JsonException
*/
@SuppressWarnings("unchecked")
- public static <T> T fromJson(Class<T> type, Reader reader, JsonFilter filter) throws JsonException {
+ public static <T> T fromJson(Class<T> type, Reader reader, JsonFormat filter) throws JsonException {
return (T) parse(type, reader, filter);
}
@@ -159,21 +157,21 @@ public static Object fromJson(Type type, Reader reader) throws JsonException {
* 转换的类型
* @param cs
* JSON 字符串
- * @param filter
+ * @param format
* 过滤器
* @return JAVA 对象
* @throws JsonException
*/
@SuppressWarnings("unchecked")
- public static <T> T fromJson(Type type, Reader reader, JsonFilter filter) throws JsonException {
- return (T) parse(type, reader, filter);
+ public static <T> T fromJson(Type type, Reader reader, JsonFormat format) throws JsonException {
+ return (T) parse(type, reader, format);
}
private static Object parse(Type type, Reader reader) {
return Objs.convert(new JsonCompile().parse(reader), type);
}
- private static Object parse(Type type, Reader reader, JsonFilter filter) {
- return Objs.convert(new JsonCompileFilter().parse(reader, filter), type);
+ private static Object parse(Type type, Reader reader, JsonFormat format) {
+ return Objs.convert(new JsonCompile(format).parse(reader), type);
}
/**
@@ -329,17 +327,7 @@ public static Object fromJson(CharSequence cs) throws JsonException {
* @return JSON 字符串
*/
public static String toJson(Object obj) {
- return toJson(obj, null, null);
- }
- /**
- * 将一个 JAVA 对象转换成 JSON 字符串
- *
- * @param obj
- * JAVA 对象
- * @return JSON 字符串
- */
- public static String toJson(Object obj, JsonFilter filter) {
- return toJson(obj, null, filter);
+ return toJson(obj, null);
}
/**
@@ -356,22 +344,6 @@ public static String toJson(Object obj, JsonFormat format) {
toJson(Lang.opw(sb), obj, format);
return sb.toString();
}
- /**
- * 将一个 JAVA 对象转换成 JSON 字符串,并且可以设定 JSON 字符串的格式化方式
- *
- * @param obj
- * JAVA 对象
- * @param format
- * JSON 字符串格式化
- * @param filter
- * JSON 字符过滤器
- * @return JSON 字符串
- */
- public static String toJson(Object obj, JsonFormat format, JsonFilter filter) {
- StringBuilder sb = new StringBuilder();
- toJson(Lang.opw(sb), obj, format, filter);
- return sb.toString();
- }
/**
* 将一个 JAVA 对象写到一个文本输出流里
@@ -382,20 +354,7 @@ public static String toJson(Object obj, JsonFormat format, JsonFilter filter) {
* JAVA 对象
*/
public static void toJson(Writer writer, Object obj) {
- toJson(writer, obj, null, null);
- }
- /**
- * 将一个 JAVA 对象写到一个文本输出流里
- *
- * @param writer
- * 文本输出流
- * @param obj
- * JAVA 对象
- * @param filter
- * JSON 字符过滤器
- */
- public static void toJson(Writer writer, Object obj, JsonFilter filter) {
- toJson(writer, obj, null, filter);
+ toJson(writer, obj, null);
}
/**
@@ -409,34 +368,15 @@ public static void toJson(Writer writer, Object obj, JsonFilter filter) {
* JSON 字符串格式化 , 若format, 则定义为JsonFormat.nice()
*/
public static void toJson(Writer writer, Object obj, JsonFormat format) {
- toJson(writer, obj, format, null);
- }
- /**
- * 将一个 JAVA 对象写到一个文本输出流里,并且可以设定 JSON 字符串的格式化方式
- *
- * @param writer
- * 文本输出流
- * @param obj
- * JAVA 对象
- * @param format
- * JSON 字符串格式化 , 若format, 则定义为JsonFormat.nice()
- * @param filter
- * JSON 字符过滤器
- */
- public static void toJson(Writer writer, Object obj, JsonFormat format, JsonFilter filter) {
try {
- if (format == null)
- format = JsonFormat.nice();
- if(filter != null){
- new JsonRenderingFilter(writer, format, filter).render(obj);
- } else {
- new JsonRendering(writer, format).render(obj);
- }
- writer.flush();
- }
- catch (IOException e) {
- throw Lang.wrapThrow(e, JsonException.class);
- }
+ if (format == null)
+ format = JsonFormat.nice();
+ new JsonRendering(writer, format).render(obj);
+ writer.flush();
+ }
+ catch (IOException e) {
+ throw Lang.wrapThrow(e, JsonException.class);
+ }
}
@SuppressWarnings({ "rawtypes", "unchecked" })
View
17 src/org/nutz/json/JsonCompile.java
@@ -18,6 +18,7 @@
*/
public class JsonCompile {
+ private JsonFormat format;
private int cursor;
private Reader reader;
private int col;
@@ -25,6 +26,13 @@
private static final int END = -1;
+ public JsonCompile() {
+ this(new JsonFormat());
+ }
+ public JsonCompile(JsonFormat format) {
+ this.format = format;
+ }
+
private boolean skipOneChar = false;
public Object parse(Reader reader) {
@@ -302,7 +310,14 @@ private void parseMap(Map<String, Object> map) throws IOException{
* @throws IOException
*/
protected void parseMapItem(Map<String, Object> map) throws IOException {
- map.put(fetchKey(), parseFromHere());
+// map.put(fetchKey(), parseFromHere());
+ String key = fetchKey();
+ format.pushPath(key);
+ Object val = parseFromHere();
+ if(format.filter()){
+ map.put(key, val);
+ }
+ format.pollPath();
}
/**
* 找KEY
View
100 src/org/nutz/json/JsonFormat.java
@@ -1,5 +1,8 @@
package org.nutz.json;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
import java.util.regex.Pattern;
import org.nutz.castor.Castors;
@@ -51,6 +54,9 @@ public JsonFormat(boolean compact) {
this.quoteName = true;
this.castors = Castors.me();
this.separator = '\"';
+
+ this.mates = new ArrayList<String>();
+ this.filterType = FilterType.exclude;
}
/**
@@ -89,8 +95,86 @@ public JsonFormat(boolean compact) {
* 采用 Nutz JSON 特殊兼容模式,这样,循环引用的字段,会标记成特殊字符串表一个路径
*/
private boolean nutzJson;
-
- public boolean isNutzJson() {
+
+ /**
+ * 过滤匹配列表
+ */
+ private List<String> mates;
+ /**
+ * JSON层级路径
+ */
+ private LinkedList<String> path = new LinkedList<String>();
+ /**
+ * 过滤类型, true为包含, false为排除
+ */
+ private FilterType filterType;
+
+
+
+ /**
+ * 包含
+ * @return
+ */
+ public boolean filter() {
+ if (mates == null) {
+ return true;
+ }
+ String path = fetchPath();
+ for (String s : mates) {
+ if (filterType == FilterType.include) {
+ //包含
+ if (s.startsWith(path)) {
+ return true;
+ }
+ } else {
+ //排除
+ if (s.equals(path)) {
+ return false;
+ }
+ }
+ }
+ return filterType == FilterType.include ? false : true;
+ }
+ /**
+ * 获取路径
+ * @return
+ */
+ private String fetchPath(){
+ StringBuffer sb = new StringBuffer();
+ for(String s : path){
+ if(sb.length() > 0){
+ sb.append(".");
+ }
+ sb.append(s);
+ }
+ return sb.toString();
+ }
+ /**
+ * 压栈
+ * @param path
+ */
+ public void pushPath(String path){
+ this.path.addLast(path);
+ }
+ /**
+ * 退栈
+ */
+ public void pollPath(){
+ this.path.removeLast();
+ }
+
+
+
+
+ public void setMates(List<String> mates) {
+ this.mates = mates;
+ }
+
+ public void setFilterType(FilterType filterType) {
+ this.filterType = filterType;
+ }
+
+ public boolean isNutzJson() {
return nutzJson;
}
@@ -106,6 +190,7 @@ public boolean ignore(String name) {
return locked.matcher(name).find();
return false;
}
+
public boolean isCompact() {
return compact;
@@ -198,4 +283,15 @@ public JsonFormat setAutoUnicode(boolean autoUnicode) {
public boolean isAutoUnicode() {
return autoUnicode;
}
+
+
+ /**
+ * filter类型
+ * @author juqkai(juqkai@gmail.com)
+ */
+ public static enum FilterType{
+ include, exclude
+ }
+
+
}
View
25 src/org/nutz/json/JsonRendering.java
@@ -72,15 +72,22 @@ private void appendPairSep() throws IOException {
}
protected boolean appendPair(boolean first, String name, Object value) throws IOException {
- if(!first){
- appendPairEnd();
- }
- appendPairBegin();
- appendName(name);
- appendPairSep();
- path.push(name);
- render(value);
- return true;
+ format.pushPath(name);
+ boolean type = false;
+ if(format.filter()){
+ if(!first){
+ appendPairEnd();
+ }
+ appendPairBegin();
+ appendName(name);
+ appendPairSep();
+ path.push(name);
+ render(value);
+
+ type = true;
+ }
+ format.pollPath();
+ return type;
}
private boolean isIgnore(String name, Object value) {
View
2  test/org/nutz/json/AllJson.java
@@ -12,6 +12,8 @@
JsonCharsetTest.class,
JsonEntityTest.class,
GenericTest.class,
+ JsonCompileFilterTest.class,
+ JsonRenderingFilterTest.class,
JsonMergeTest.class
})
public class AllJson {}
View
32 test/org/nutz/json/JsonCompileFilterTest.java
@@ -7,6 +7,8 @@
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
+
+import org.nutz.json.JsonFormat.FilterType;
import org.nutz.lang.Streams;
/**
@@ -22,16 +24,20 @@ public void init(){
@Test
public void includeTest(){
mates.add("age");
- JsonFilter filter = new JsonFilter(mates, true);
- Map<?, ?> map = (Map<?, ?>) Json.fromJson(Streams.fileInr("org/nutz/json/person.txt"), filter);
+ JsonFormat format = new JsonFormat();
+ format.setMates(mates);
+ format.setFilterType(FilterType.include);
+ Map<?, ?> map = (Map<?, ?>) Json.fromJson(Streams.fileInr("org/nutz/json/person.txt"), format);
assertEquals(1, map.size());
}
@Test
public void includeTest2(){
mates.add("father.realname");
mates.add("father.name");
- JsonFilter filter = new JsonFilter(mates, true);
- Map<?, ?> map = (Map<?, ?>) Json.fromJson(Streams.fileInr("org/nutz/json/person.txt"), filter);
+ JsonFormat format = new JsonFormat();
+ format.setMates(mates);
+ format.setFilterType(FilterType.include);
+ Map<?, ?> map = (Map<?, ?>) Json.fromJson(Streams.fileInr("org/nutz/json/person.txt"), format);
Map<?, ?> father = (Map<?, ?>) map.get("father");
assertEquals(2, father.size());
}
@@ -39,8 +45,10 @@ public void includeTest2(){
public void excludeTest(){
mates.add("father.realname");
mates.add("father.name");
- JsonFilter filter = new JsonFilter(mates, false);
- Map<?, ?> map = (Map<?, ?>) Json.fromJson(Streams.fileInr("org/nutz/json/person.txt"), filter);
+ JsonFormat format = new JsonFormat();
+ format.setMates(mates);
+ format.setFilterType(FilterType.exclude);
+ Map<?, ?> map = (Map<?, ?>) Json.fromJson(Streams.fileInr("org/nutz/json/person.txt"), format);
Map<?, ?> father = (Map<?, ?>) map.get("father");
assertEquals(2, father.size());
assertEquals(69, father.get("age"));
@@ -50,8 +58,10 @@ public void excludeTest(){
@Test
public void listIncludeTest(){
mates.add("users.name");
- JsonFilter filter = new JsonFilter(mates, true);
- Map<?, ?> map = (Map<?, ?>) Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"), filter);
+ JsonFormat format = new JsonFormat();
+ format.setMates(mates);
+ format.setFilterType(FilterType.include);
+ Map<?, ?> map = (Map<?, ?>) Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"), format);
assertEquals(map.size(), 1);
List<?> users = (List<?>) map.get("users");
assertEquals(users.size(), 2);
@@ -62,8 +72,10 @@ public void listIncludeTest(){
@Test
public void listexcludeTest(){
mates.add("users.name");
- JsonFilter filter = new JsonFilter(mates, false);
- Map<?, ?> map = (Map<?, ?>) Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"), filter);
+ JsonFormat format = new JsonFormat();
+ format.setMates(mates);
+ format.setFilterType(FilterType.exclude);
+ Map<?, ?> map = (Map<?, ?>) Json.fromJson(Streams.fileInr("org/nutz/json/mateList.txt"), format);
assertEquals(map.size(), 2);
List<?> users = (List<?>) map.get("users");
assertEquals(users.size(), 2);
View
18 test/org/nutz/json/JsonRenderingFilterTest.java
@@ -4,6 +4,8 @@
import java.util.List;
import org.junit.Test;
+import org.nutz.json.JsonFormat.FilterType;
+
import static org.junit.Assert.*;
/**
@@ -17,17 +19,21 @@ public void includeTest(){
List<String> mates = new ArrayList<String>();
mates.add("name");
Abc a = new Abc(1, "juqkai");
- JsonFilter filter = new JsonFilter(mates, true);
- String s = Json.toJson(a, filter);
- assertEquals(s, "{\n \"name\" :\"juqkai\"\n}");
+ JsonFormat format = new JsonFormat();
+ format.setMates(mates);
+ format.setFilterType(FilterType.include);
+ String s = Json.toJson(a, format);
+ assertEquals(s, "{\"name\":\"juqkai\"}");
}
@Test
public void excludeTest(){
List<String> mates = new ArrayList<String>();
mates.add("name");
Abc a = new Abc(1, "juqkai");
- JsonFilter filter = new JsonFilter(mates, false);
- String s = Json.toJson(a, filter);
- assertEquals(s, "{\n \"id\" :1\n}");
+ JsonFormat format = new JsonFormat();
+ format.setMates(mates);
+ format.setFilterType(FilterType.exclude);
+ String s = Json.toJson(a, format);
+ assertEquals(s, "{\"id\":1}");
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.