BSON for Jackson支持 BSON 格式的 Jackson 扩展
BSON for Jackson 顾名思义,是 Jackson 的扩展,实现对 BSON 格式的支持。
Maven
<dependencies> <dependency> <groupId>de.undercouch</groupId> <artifactId>bson4jackson</artifactId> <version>2.5.0</version> </dependency> </dependencies>
示例代码:
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import com.fasterxml.jackson.databind.ObjectMapper; import de.undercouch.bson4jackson.BsonFactory; public class ObjectMapperSample { public static void main(String[] args) throws Exception { //create dummy POJO Person bob = new Person(); bob.setName("Bob"); //serialize data ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectMapper mapper = new ObjectMapper(new BsonFactory()); mapper.writeValue(baos, bob); //deserialize data ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray()); Person clone_of_bob = mapper.readValue(bais, Person.class); assert bob.getName().equals(clone_of_bob.getName()); } }
评论