Creating BSON object from JSON string

喜你入骨 提交于 2019-11-26 10:41:58

问题


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:


Use Document.parse(String json) from org.bson.Document. It returns Document object which is type of Bson.




回答6:


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




回答7:


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




回答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:


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.



来源:https://stackoverflow.com/questions/3117167/creating-bson-object-from-json-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!