title: Deserialize bson long primitive json in jackson
body: <p>I am using MongoDB as our data store, but we want to use Jackson for serialization/deserialization (the Mongo pojo classes don't handle nearly as many scenarios as Jackson - builders for example).</p>
  
  <p>We have this working using a custom CodecProvider - here's the codec itself:</p>
  
  <pre><code>class JacksonCodec&lt;T&gt; implements Codec&lt;T&gt; {
  
  
     private final ObjectMapper objectMapper;
      private final Codec&lt;RawBsonDocument&gt; rawBsonDocumentCodec;
      private final Class&lt;T&gt; type;
  
      public JacksonCodec(ObjectMapper objectMapper,
                          CodecRegistry codecRegistry,
                          Class&lt;T&gt; type) {
          this.objectMapper = objectMapper;
          this.rawBsonDocumentCodec = codecRegistry.get(RawBsonDocument.class);
          this.type = type;
      }
  
      @Override
      public T decode(BsonReader reader, DecoderContext decoderContext) {
          try {
  
              RawBsonDocument document = rawBsonDocumentCodec.decode(reader, decoderContext);
              String json = document.toJson();
              return objectMapper.readValue(json, type);
          } catch (IOException e) {
              throw new UncheckedIOException(e);
          }
      }
  
      @Override
      public void encode(BsonWriter writer, Object value, EncoderContext encoderContext) {
          try {
  
              String json = objectMapper.writeValueAsString(value);
  
              rawBsonDocumentCodec.encode(writer, RawBsonDocument.parse(json), encoderContext);
  
          } catch (IOException e) {
              throw new UncheckedIOException(e);
          }
      }
  
      @Override
      public Class&lt;T&gt; getEncoderClass() {
          return this.type;
      }
  }
  </code></pre>
  
  <p>this works fine, until we retrieve a document from Mongo that has a long that is greater than Integer.MAXVALUE.  When that happens, deserialization fails with the following message:</p>
  
  <p>Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of long out of START_OBJECT token.</p>
  
  <p>Looking at the bson, here's how the Mongo data is coming back to us:</p>
  
  <p>"dateStamp" : { "$numberLong" : "1514334498165" }</p>
  
  <p>so...  I'm thinking that I need to register an additional deserializer for Jackson to handle this case (check for a token type of ID_START_OBJECT, parse if it's there, otherwise delegate to the built-in deserializer).  I tried registering a simple Long deserializer with the ObjectMapper SimpleModule:</p>
  
  <pre><code>public class BsonLongDeserializer  extends JsonDeserializer&lt;Long&gt;{
  
      @Override
      public Class&lt;Long&gt; handledType() {
          return Long.class;
      }
  
      @Override
      public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
          if (p.currentTokenId() != JsonTokenId.ID_START_OBJECT){
              // have to figure out how to do this for real if we can get the deserilizer to actually get called
              return ctxt.readValue(p, Long.class);
          }
          return null;
      }
  }
  </code></pre>
  
  <p>and register it:</p>
  
  <pre><code>private static ObjectMapper createMapper(){
      SimpleModule module = new SimpleModule();
      module.addDeserializer(Long.class, new BsonLongDeserializer());
  
      ObjectMapper mapper = new ObjectMapper()
      .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
      .registerModule(module);
  
      return mapper;
  }
  </code></pre>
  
  <p>but the BsonLongDeserializer never gets called by Jackson (are primitives handled differently and short-circuit the registered deserializers maybe?).</p>
  
  <p>Jackson version 2.9.3.  MongoDB driver version 3.6.</p>
  
  <p>If anyone has any suggestions on angles to attack this, I would appreciate hearing them.</p>
  
  <p>Referenced articles that don't seem to help: <a href="https://stackoverflow.com/questions/42555757/mongodb-numberlong-numberlong-issue-while-converting-back-to-java-object">MongoDB &quot;NumberLong/$numberLong&quot; issue while converting back to Java Object</a></p>