change json serializer
parent
65ec5d605f
commit
13b2d0f4d4
|
|
@ -12,6 +12,7 @@ import com.mongodb.client.result.UpdateResult;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.bson.conversions.Bson;
|
import org.bson.conversions.Bson;
|
||||||
import org.bson.types.ObjectId;
|
import org.bson.types.ObjectId;
|
||||||
|
import sdk.FastJSONUtil;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
@ -44,11 +45,11 @@ public class Controller extends com.jfinal.core.Controller {
|
||||||
Filters.and(ops)
|
Filters.and(ops)
|
||||||
).into(documents);
|
).into(documents);
|
||||||
|
|
||||||
List<String> json = new ArrayList<>();
|
// List<String> json = new ArrayList<>();
|
||||||
documents.forEach(doc -> {
|
// documents.forEach(doc -> {
|
||||||
json.add(doc.toJson());
|
// json.add(FastJSONUtil.bsonToJSON(doc));
|
||||||
});
|
// });
|
||||||
renderText(Arrays.toString(json.toArray()), ContentType.JSON);
|
renderJson(FastJSONUtil.bsonToJSON(documents));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dictList() {
|
public void dictList() {
|
||||||
|
|
@ -59,11 +60,12 @@ public class Controller extends com.jfinal.core.Controller {
|
||||||
List<Document> documents = new ArrayList<>();
|
List<Document> documents = new ArrayList<>();
|
||||||
collection.find().limit(pageSize).skip(pageNum * pageSize).into(documents);
|
collection.find().limit(pageSize).skip(pageNum * pageSize).into(documents);
|
||||||
|
|
||||||
List<String> json = new ArrayList<>();
|
// List<String> json = new ArrayList<>();
|
||||||
documents.forEach(doc -> {
|
// documents.forEach(doc -> {
|
||||||
json.add(doc.toJson());
|
// json.add(doc.toJson());
|
||||||
});
|
// });
|
||||||
renderText(Arrays.toString(json.toArray()), ContentType.JSON);
|
// renderText(Arrays.toString(json.toArray()), ContentType.JSON);
|
||||||
|
renderJson(FastJSONUtil.bsonToJSON(documents));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addDict() {
|
public void addDict() {
|
||||||
|
|
@ -81,7 +83,8 @@ public class Controller extends com.jfinal.core.Controller {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
getCollection().insertOne(dict);
|
getCollection().insertOne(dict);
|
||||||
renderJson(dict.toJson());
|
// renderJson(dict.toJson());
|
||||||
|
renderJson(FastJSONUtil.bsonToJSON(dict));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before(IdInterceptor.class)
|
@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