modify fastjson serialize rule
parent
3588cfb822
commit
4fb07bbb56
4
pom.xml
4
pom.xml
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
<groupId>cn.cloudowr</groupId>
|
<groupId>cn.cloudowr</groupId>
|
||||||
<artifactId>sdk</artifactId>
|
<artifactId>sdk</artifactId>
|
||||||
<version>1.1.1</version>
|
<version>1.1.2</version>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|
@ -42,7 +42,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
<artifactId>fastjson</artifactId>
|
<artifactId>fastjson</artifactId>
|
||||||
<version>1.2.6</version>
|
<version>1.2.24</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,41 @@
|
||||||
package cn.cloudowr.sdk;
|
package cn.cloudowr.sdk;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.serializer.SerializeConfig;
|
import com.alibaba.fastjson.serializer.*;
|
||||||
|
import org.bson.types.ObjectId;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lyf66 on 2017/2/18.
|
* Created by lyf66 on 2017/2/18.
|
||||||
*/
|
*/
|
||||||
public class FastJSONUtil {
|
public class FastJSONUtil {
|
||||||
private static ObjectIdSerializer serializer = new ObjectIdSerializer();
|
private static ObjectSerializer serializer = new ObjectSerializer() {
|
||||||
|
@Override
|
||||||
|
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
|
||||||
|
if (object == null) {
|
||||||
|
serializer.getWriter().writeNull();
|
||||||
|
} else {
|
||||||
|
serializer.write(((ObjectId) object).toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
private static NameFilter nameFilter = new NameFilter() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String process(Object object, String name, Object value) {
|
||||||
|
if ("_id".equals(name)) {
|
||||||
|
return "id";
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public static String bsonToJSON(Object object) {
|
public static String bsonToJSON(Object object) {
|
||||||
SerializeConfig mapping = new SerializeConfig();
|
SerializeConfig mapping = new SerializeConfig();
|
||||||
mapping.put(org.bson.types.ObjectId.class, serializer);
|
mapping.put(org.bson.types.ObjectId.class, serializer);
|
||||||
return JSON.toJSONString(object, mapping);
|
|
||||||
|
return JSON.toJSONString(object, mapping, nameFilter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
package cn.cloudowr.sdk;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.serializer.JSONSerializer;
|
|
||||||
import com.alibaba.fastjson.serializer.ObjectSerializer;
|
|
||||||
import org.bson.types.ObjectId;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by lisai on 17/2/9.
|
|
||||||
*/
|
|
||||||
public class ObjectIdSerializer implements ObjectSerializer {
|
|
||||||
@Override
|
|
||||||
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
|
|
||||||
if(object == null) {
|
|
||||||
serializer.getWriter().writeNull();
|
|
||||||
} else {
|
|
||||||
serializer.write(((ObjectId)object).toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package cn.cloudowr.sdk.mongo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyf66 on 2017/2/19.
|
||||||
|
*/
|
||||||
|
public abstract class DBModel {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package cn.cloudowr.sdk.mongo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyf66 on 2017/2/19.
|
||||||
|
*/
|
||||||
|
public class MongoQueryModel extends QueryModel{
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,103 @@
|
||||||
|
package cn.cloudowr.sdk.mongo;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.TypeReference;
|
||||||
|
import com.mongodb.client.model.Filters;
|
||||||
|
import com.mongodb.client.model.Updates;
|
||||||
|
import org.bson.Document;
|
||||||
|
import org.bson.conversions.Bson;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyf66 on 2017/2/19.
|
||||||
|
*/
|
||||||
|
public class MongoQueryModelBuilder {
|
||||||
|
private static Map<String, List<String>> cachedRequiedFieldNames = new HashMap<>();
|
||||||
|
private String key;
|
||||||
|
|
||||||
|
public MongoQueryModel build(String json, Class<? extends DBModel> klass) {
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MongoQueryModel build(String json, List<String> requiedFieldNames) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Document build(String json, boolean throwOnException) {
|
||||||
|
Bson filter;
|
||||||
|
Bson update;
|
||||||
|
Bson proj;
|
||||||
|
|
||||||
|
HashMap<String, Object> map = JSON.parseObject(json, new TypeReference<HashMap<String, Object>>() {});
|
||||||
|
List<String> f = (List<String>) map.get("f");
|
||||||
|
List<String> upd = (List<String>) map.get("upd");
|
||||||
|
|
||||||
|
|
||||||
|
filter = getFilter(f);
|
||||||
|
update = getUpdate(upd);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bson getUpdate(List<String> upd) {
|
||||||
|
List<Bson> ops = new ArrayList<>();
|
||||||
|
Bson update = null;
|
||||||
|
for (String s : upd) {
|
||||||
|
List<String> list = JSON.parseObject(s, new TypeReference<List<String>>() {});
|
||||||
|
String opString = list.get(0);
|
||||||
|
bbb(ops, list, opString);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bson getFilter(List<String> f) {
|
||||||
|
List<Bson> ops = new ArrayList<>();
|
||||||
|
Bson filter = null;
|
||||||
|
|
||||||
|
for (String s : f) {
|
||||||
|
List<String> list = JSON.parseObject(s, new TypeReference<List<String>>() {});
|
||||||
|
String opString = list.get(1);
|
||||||
|
bbb(ops, list, opString);
|
||||||
|
|
||||||
|
filter = Filters.and(ops);
|
||||||
|
}
|
||||||
|
return filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bbb(List<Bson> ops, List<String> list, String opString) {
|
||||||
|
String key = list.get(1);
|
||||||
|
// Object val =
|
||||||
|
switch (opString) {
|
||||||
|
case "=":
|
||||||
|
ops.add(Filters.eq(list.get(1), list.get(2)));
|
||||||
|
break;
|
||||||
|
case ">":
|
||||||
|
ops.add(Filters.gt(list.get(1), list.get(2)));
|
||||||
|
break;
|
||||||
|
case "<":
|
||||||
|
ops.add(Filters.lt(list.get(1), list.get(2)));
|
||||||
|
break;
|
||||||
|
case ">=":
|
||||||
|
ops.add(Filters.gte(list.get(1), list.get(2)));
|
||||||
|
break;
|
||||||
|
case "<=":
|
||||||
|
|
||||||
|
ops.add(Filters.lte(key, list.get(2)));
|
||||||
|
break;
|
||||||
|
case "$set":
|
||||||
|
ops.add(Updates.set(list.get(1), list.get(2)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Document typeCheck(boolean throwOnException, String msg) {
|
||||||
|
if (throwOnException) {
|
||||||
|
throw new ClassCastException(msg);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
package cn.cloudowr.sdk.mongo;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyf66 on 2017/2/19.
|
||||||
|
*/
|
||||||
|
public interface QueryBuilder<T extends QueryModel> {
|
||||||
|
T build(String json, Class<? extends DBModel> klass);
|
||||||
|
T build(String json, List<String> requiedFieldNames);
|
||||||
|
T build(String json);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package cn.cloudowr.sdk.mongo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyf66 on 2017/2/19.
|
||||||
|
*/
|
||||||
|
public abstract class QueryModel {
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue