Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BSON library for java? [closed]

Tags:

java

mongodb

bson

We have good support for JSON in java http://blog.locut.us/main/2009/10/14/which-is-the-best-java-json-library.html but what about BSON.

What library do you know that provides BSON support for java? It should obviously be efficient in runtime.

like image 996
Maxim Veksler Avatar asked Sep 07 '10 06:09

Maxim Veksler


2 Answers

You can use the MongoDB driver for Java to store a BSON object, then convert that to a String which you can then wrap with JSONObject.

For example, here's how I'll create a regular document:

BasicDBObject obj = new BasicDBObject();
obj.put("name", "Matt");
obj.put("date", new Date());

Then, to get a String representation of the object, simply call:

String bsonString = obj.toString();

Wrap it with a JSONObject and get the date attribute, which should return it in a BSON-compliant format.

JSONObject newObject = new JSONObject(bsonString);
System.out.println(newObject.get("date"));

The resulting output is something like:

{"$date":"2012-08-10T05:22:53.872Z"}
like image 169
MLQ Avatar answered Nov 08 '22 07:11

MLQ


The BSON site is pointing at this

If you want to use it from MongoDB, take a look at this example

like image 27
Bozho Avatar answered Nov 08 '22 06:11

Bozho