可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have Java app that takes data from external app. Incoming JSONs are in Strings. I would like to parse that Strings and create BSON objects.
Unfortunate I can't find API for that in Java's BSON implementation.
Do I have use external parser for that like GSON?
回答1:
The easiest way seems to be to use a JSON library to parse the JSON strings into a Map
and then use the putAll
method to put those values into a BSONObject
.
This answer shows how to use Jackson to parse a JSON string into a Map
.
回答2:
Official MongoDB Java Driver comes with utility methods for parsing JSON to BSON and serializing BSON to JSON.
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
DBObject dbObj = ... ;
String json = JSON.serialize( dbObj );
DBObject bson = ( DBObject ) JSON.parse( json );
The driver can be found here: https://mongodb.github.io/mongo-java-driver/
回答3:
... And, since 3.0.0, you can:
import org.bson.Document;
final Document doc = new Document("myKey", "myValue");
final String jsonString = doc.toJson();
final Document doc = Document.parse(jsonString);
Official docs:
- Document.parse(String)
- Document.toJson()
回答4:
To convert a string json to bson, do:
import org.bson.BasicBSONEncoder;
import org.bson.BSONObject;
BSONObject bson = (BSONObject)com.mongodb.util.JSON.parse(string_json);
BasicBSONEncoder encoder = new BasicBSONEncoder();
byte[] bson_byte = encoder.encode(bson);
To convert a bson to json, do:
import org.bson.BasicBSONDecoder;
import org.bson.BSONObject;
BasicBSONDecoder decoder = new BasicBSONDecoder();
BSONObject bsonObject = decoder.readObject(out);
String json_string = bsonObject.toString();
回答5:
You might be interested in bson4jackson project, which allows you to use Jackson data binding to work with BSON (create POJOs from BSON, write as BSON) -- especially since Jackson also work with JSON. So it will allow conversion like you mention, just use different ObjectMapper instanstaces (one that works with JSON, other with BSON).
With Jackson you can either work with full POJOs (declare structure you want) or with simple Maps, Lists and so on. You just need to declare what to type to bind to when reading data (when writing, type is defined by object you pass).
回答6:
You'll find the answer to your question in the source code of https://github.com/mongodb/mongo/blob/master/src/mongo/db/jsobj.cpp
Which has the BSON to JSON conversion.
Basically, stuff like
ObjectId("XXX")
-> { "$oid" : "XXX" }
/XXX/gi
-> { "$regex" : "XXX", "$options" : "gi" }
and so on...
回答7:
I am not sure about java but the mongoDB CPP driver has a function type
BSONObj fromjson(string)
which returns a BSONObj according to the string passed. There should be a same function in Java too.
回答8:
I would suggest using the toJson() and parse(String) methods of the BasicDBObject, because the JSON utility class has been @Depricated.
import com.mongodb.BasicDBObject;
public static BasicDBObject makeBsonObject(String json) {
return BasicDBObject.parse(json);
}
public static String makeJsonObject(BasicDBObject dbObj) {
return dbObj.toJson();
}
回答9:
Use Document.parse(String json)
from org.bson.Document
. It returns Document
object which is type of Bson
.