5

I am having a json which is somethink like {"Header" : {"name" : "TestData", "contactNumber" : 8019071740}}

If i insert this to mongoDB it will be something like

{"_id" : ObjectId("58b7e55097989619e4ddb0bb"),"Header" : {"name" : "TestData","contactNumber" : NumberLong(8019071743)}

When i read this data back and try to convert to java object using Gson it throws exception com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a long but was BEGIN_OBJECT at line 1 column 109 path $.Header.contactNumber

I have found this, But i was wondering if i have very complex json structure then i might need to manipulate many json nodes in this approach.

Do anyone has any better alternatives on this.

Edit:1 I am reading querying and converting json as below

Document MongoDocument = mycollection.find(searchCondition);
String resultJson =  MongoDocument.toJson();
Gson gson = new Gson();
Model model= gson.fromJson(resultJson, ItemList.class);
Community
  • 1
  • 1
Geek
  • 1,214
  • 5
  • 14
  • 27

5 Answers5

7

We can use below code:

Document doc = documentCursor.next();
JsonWriterSettings relaxed = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();
CustomeObject obj = gson.fromJson(doc.toJson(relaxed), CustomeObject.class);
maahl
  • 547
  • 3
  • 17
Deepak Gusain
  • 101
  • 2
  • 5
2

Mongo db uses Bson format with its own types which follows json standards but it can't be parsed by json library without writing the custom wrapper/codec.

You can use third party framework/plugins to make the library take care of converting between document and pojo.

If that is not an option for you, you will have to do mapping yourself.

Document mongoDocument = mycollection.find(searchCondition);
Model model= new Model();
model.setProperty(mongoDocument.get("property");
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • 1
    Which third party framework/plugins you suggest for this scenario – Geek Mar 02 '17 at 12:57
  • You have many of them. I have used Morphia and Spring Mongo Db. – s7vr Mar 02 '17 at 12:58
  • So in that case i see a huge drawback on using Java driver for MongoDB. I this this issue will arise in case of Date object also. – Geek Mar 02 '17 at 13:04
  • Yes you will the same issue with date too, but this is not a drawback as this is just a driver just like your sql driver. You have to take care of mapping at Client side. – s7vr Mar 02 '17 at 13:11
  • So in case of complex json where individual mapping is not possible, only way handle this is by start using framework ? – Geek Mar 02 '17 at 13:14
  • I'm not sure if framework is going to help here. I think you should revise your structure, may be denormalize it. Try to query them separately and move the complexity into business logic. – s7vr Mar 02 '17 at 13:53
2

Take a look at: converting Document objects in MongoDB 3 to POJOS

I had the same problem. The workaround with com.mongodb.util.JSON.serialize(document) does the trick.

Community
  • 1
  • 1
joerno
  • 911
  • 2
  • 10
  • 19
1

Document.toJson() gives bson but not json.

I.e. for Long field myLong equals to xxx of Document it produces json like:

 "myLong" : { "$numberLong" : "xxx"}

Parsing of such with Gson will not give myLong=xxx evidently.

To convert Document to Pojo using Gson you may do next:

Gson gson = new Gson();
MyPojo pojo = gson.fromJson(gson.toJson(document), MyPojo.class);
0
val mongoJsonWriterSettings: JsonWriterSettings = JsonWriterSettings.builder.int64Converter((value, writer) => writer.writeNumber(value.toString)).build
def bsonToJson(document: Document): String = document toJson mongoJsonWriterSettings
F. P. Freely
  • 1,026
  • 14
  • 24