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 BSONObject 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.