支持BSON格式的Jackson扩展:BSON for Jackson
jopen
9年前
一个插件式的BSON生成器和解析器,用于 Jackson JSON 处理器。
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()); } }