反序列化MongoDB BSON [英] Deserializing MongoDB BSON

查看:287
本文介绍了反序列化MongoDB BSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从查询MongoDB实例的REST服务中获取响应,并将响应解析为Java对象。 Web服务返回MIME类型为html的响应,并使用换行符分隔返回的每个记录(尽管我可以调整服务返回的内容)。将BSON响应转换为Java对象的最简单/最有效的方法是什么?我已经用Java创建了一个模板类来存储数据。

I attempting to take a response from a REST service that queries an instance of MongoDB and parse the response into a Java object. The web service returns the response with a MIME type of html with a newline character separating each record that is returned (although I have the ability to adjust what the service returns). What is the easiest/most efficient way for converting the BSON response into a Java object? I have already created a template class in Java to store the data.

提前致谢!

编辑:一位同事建议我在 webservice 中使用MongoDB Java驱动程序的BSON解析实用程序,然后返回格式良好的HTML字符串。这仍然让我在我的应用程序中进行解析,但暂时将作为一种解决方法。仍然在寻找一种方法来轻松地将BSON响应反序列化为Java对象。

edit: A colleague suggested to me using the MongoDB Java driver's BSON parsing utilities in the webservice itself and then returning a nicely formatted HTML string. This still leaves me with parsing to do in my application, but will function as a workaround for the time being. Still looking for a way to easily deserialize the BSON response to a Java object.

推荐答案

对于那些感兴趣的人,我找到了解决方案我的问题。事实证明,使用 Google的GSON驱动程序,可以像使用JSON一样解析BSON格式。我必须处理的棘手部分是想办法在我的模板类中存储嵌套字段。允许GSON解析嵌套文档的方法是在模板类中声明静态内部类。下面是一个例子:

For those interested, I found the solution to my problem. It turns out that the BSON format can be parsed just like JSON using Google's GSON driver. The one tricky part that I had to deal with was figuring a way to store nested fields in my template class. The way to allow GSON to parse nested documents is to declare static inner classes in your template class. Here is an example:

public BSONObject {
   // Private fields
   private int foo;
   private String bar;

  // Constructors
  public BSONObject() {}

  // Static inner subclasses
  private Widget widget;
  private Duck quack;

  // Getters & Setters for outer class
  public int getFoo() {...}
  public String getBar() {...}
  public Widget getWidget() {...}
  public Duck getDuck() {...}

  // Static inner class declarations
  public static Widget {
     // include vars & getters/setters
  }

等。

按照上面的框架声明模板类允许我使用GSON库中的几行代码轻松解析MongoDB的格式。请注意,当我从我的网络服务返回数据时,我将每个条目连接一个\ n,以便在Mongo的BSON响应中分隔每个文档:

Declaring the template class following the above framework allowed me to easily parse MongoDB's formatting using a few lines of code from the GSON library. Please note that I concatenated a "\n" to each entry when returning data from my webservice so as to separate each document in Mongo's BSON response:

public String getNestedField() {
   Gson gson = new Gson();
   String [] split = response.split("\n");
   JsonParser p = new JsonParser();
   String json = split[0];
   BSONObject b = gson.fromJson(p.parse(json), BSONObject.class);
   return b.getWidget().getField();
}

这篇关于反序列化MongoDB BSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆