开发者

java types in org.bson.BSONObject

开发者 https://www.devze.com 2023-01-16 15:08 出处:网络
I\'m currently learning the BSON java library for mongodb and I\'m trying to transform a org.bson.BSONObject into XML in order to transform it with a XSLT stylesheet.

I'm currently learning the BSON java library for mongodb and I'm trying to transform a org.bson.BSONObject into XML in order to transform it with a XSLT stylesheet.

What kind of java types can I find as values in a B开发者_高级运维SONObject from a Mongodb ? Of course there will be:

  • BSONObject (internal doc)
  • java.lang.String
  • ???

what are the others ? BigDecimal and BigInteger ? boolean, int, long, double ? Timestamp.. etc... ??

thanks,

Pierre


Had to search for it too, but according to this mongodb-dev post mapping is done like this:

 NULL            null
 UNDEFINED       null
 BOOLEAN         Boolean
 NUMBER          Double
 NUMBER_INT      Integer
 NUMBER_LONG     Long
 SYMBOL          String
 STRING          String
 OID             mongodb ObjectID
 REF             DBPointer
 DATE            Date
 REGEX           Pattern
 BINARY          DBBinary
 CODE            (exception)
 ARRAY           DBList
 OBJECT          DBObject or DBRef
 TIMESTAMP       DBTimestamp
 MINKEY          String: "MinKey"
 MAXKEY          String: "MaxKey" 

This article on mongodb.org is a good resource for it, too.

Edit: Had a look at the source: org.bson.types.* is having a number of classes for BSON types. org.bson.BSONDecoder is decoding a BSON string and does the mapping listed above.


One alternative way to operate on BSON would be to use Jackson JSON processor; although by default it operates on JSON, there are extensions to use it both on BSON and XML. Since Jackson does data binding, you can bind BSON data into Java POJOs (with bson4jackson) and write out as XML (with jackson-xml-databind). Transformation would be as simple as:

String xml = xmlMapper.writeValue(bsonMapper.readValue(bsonData, MyPojo.class));

if you have, or can create, MyPojo that maps all properties; or if not by specifying Map.class as the intermediate type to bind to.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号