change json serializer
parent
30ae6a0722
commit
7c35f0092f
|
|
@ -15,6 +15,7 @@ import com.mongodb.client.result.UpdateResult;
|
|||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.bson.types.ObjectId;
|
||||
import sdk.FastJSONUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
|
@ -59,7 +60,8 @@ public class Controller extends com.jfinal.core.Controller {
|
|||
|
||||
file.getFile().delete();
|
||||
|
||||
renderJson(doc.toJson());
|
||||
// renderJson(doc.toJson());
|
||||
renderJson(FastJSONUtil.bsonToJSON(doc));
|
||||
}
|
||||
|
||||
public void download() throws Throwable {
|
||||
|
|
@ -91,11 +93,12 @@ public class Controller extends com.jfinal.core.Controller {
|
|||
Filters.and(ops)
|
||||
).into(documents);
|
||||
|
||||
List<String> json = new ArrayList<>();
|
||||
documents.forEach(doc -> {
|
||||
json.add(doc.toJson());
|
||||
});
|
||||
renderText(Arrays.toString(json.toArray()), ContentType.JSON);
|
||||
// List<String> json = new ArrayList<>();
|
||||
// documents.forEach(doc -> {
|
||||
// json.add(doc.toJson());
|
||||
// });
|
||||
// renderText(Arrays.toString(json.toArray()), ContentType.JSON);
|
||||
renderJson(FastJSONUtil.bsonToJSON(documents));
|
||||
}
|
||||
|
||||
public void attachList() {
|
||||
|
|
@ -106,11 +109,12 @@ public class Controller extends com.jfinal.core.Controller {
|
|||
List<Document> documents = new ArrayList<>();
|
||||
collection.find().limit(pageSize).skip(pageNum * pageSize).into(documents);
|
||||
|
||||
List<String> json = new ArrayList<>();
|
||||
documents.forEach(doc -> {
|
||||
json.add(doc.toJson());
|
||||
});
|
||||
renderText(Arrays.toString(json.toArray()), ContentType.JSON);
|
||||
// List<String> json = new ArrayList<>();
|
||||
// documents.forEach(doc -> {
|
||||
// json.add(doc.toJson());
|
||||
// });
|
||||
// renderText(Arrays.toString(json.toArray()), ContentType.JSON);
|
||||
renderJson(FastJSONUtil.bsonToJSON(documents));
|
||||
}
|
||||
|
||||
public void addAttach() {
|
||||
|
|
@ -128,7 +132,8 @@ public class Controller extends com.jfinal.core.Controller {
|
|||
}
|
||||
});
|
||||
getCollection().insertOne(attach);
|
||||
renderJson(attach.toJson());
|
||||
// renderJson(attach.toJson());
|
||||
renderJson(FastJSONUtil.bsonToJSON(attach));
|
||||
}
|
||||
|
||||
@Before(IdInterceptor.class)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
package sdk;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.SerializeConfig;
|
||||
|
||||
/**
|
||||
* Created by lyf66 on 2017/2/18.
|
||||
*/
|
||||
public class FastJSONUtil {
|
||||
private static ObjectIdSerializer serializer = new ObjectIdSerializer();
|
||||
|
||||
public static String bsonToJSON(Object object) {
|
||||
SerializeConfig mapping = new SerializeConfig();
|
||||
mapping.put(org.bson.types.ObjectId.class, serializer);
|
||||
return JSON.toJSONString(object, mapping);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue