Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can probably use Mixin annotations to annotate your POJO and the <code>BasicDBObject</code> (or <code>DBObject</code>), so annotations is not a problem. Since <code>BasicDBOject</code> is a map, you can use <code>@JsonAnySetter</code> on the put method.</p> <pre><code>m_objectMapper.addMixInAnnotations(YourMixIn.class, BasicDBObject.class); public interface YourMixIn.class { @JsonAnySetter void put(String key, Object value); } </code></pre> <p>This is all I can come up with since I have zero experience with MongoDB Object.</p> <p><strong>Update:</strong> <a href="http://wiki.fasterxml.com/JacksonMixInAnnotations">MixIn</a> are basically a Jackson mechanism to add annotation to a class without modifying said class. This is a perfect fit when you don't have control over the class you want to marshal (like when it's from an external jar) or when you don't want to clutter your classes with annotation.</p> <p>In your case here, you said that <code>BasicDBObject</code> implements the <code>Map</code> interface, so that class has the method <code>put</code>, as defined by the map interface. By adding <a href="http://fasterxml.github.com/jackson-annotations/javadoc/2.1.0/com/fasterxml/jackson/annotation/JsonAnySetter.html">@JsonAnySetter</a> to that method, you tell Jackson that whenever he finds a property that he doesn't know after introspection of the class to use the method to insert the property to the object. The key is the name of the property and the value is, well, the value of the property.</p> <p>All this combined makes the intermediate map go away, since Jackson will directly convert to the <code>BasicDBOject</code> because it now knows how to deserialize that class from Json. With that configuration, you can do:</p> <pre><code>DBObject dbo = m_objectMapper.convertValue(pojo, BasicDBObject.class); </code></pre> <p>Note that I haven't tested this because I don't work with MongoDB, so there might be some loose ends. However, I have used the same mechanism for similar use cases without any problem. YMMV depending on the classes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload