Skip to content

Commit

Permalink
Added deserializers for Date and Calendar
Browse files Browse the repository at this point in the history
Also added unit test to make sure we don't need deserializers
for any other type.
  • Loading branch information
michel-kraemer committed May 3, 2014
1 parent b47ce7e commit 4d1dfb2
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/de/undercouch/bson4jackson/BsonModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.Module;

import de.undercouch.bson4jackson.deserializers.BsonDeserializers;
import de.undercouch.bson4jackson.serializers.BsonSerializers;

/**
Expand All @@ -39,5 +40,6 @@ public Version version() {
@Override
public void setupModule(SetupContext context) {
context.addSerializers(new BsonSerializers());
context.addDeserializers(new BsonDeserializers());
}
}
10 changes: 10 additions & 0 deletions src/main/java/de/undercouch/bson4jackson/BsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,16 @@ public String getCurrentName() throws IOException, JsonParseException {
}
return _currentContext.fieldName;
}

/**
* @return the BSON type of the current element
*/
public byte getCurrentBsonType() {
if (_currentContext == null) {
return BsonConstants.TYPE_UNDEFINED;
}
return _currentContext.type;
}

@Override
public JsonReadContext getParsingContext() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2010-2014 Michel Kraemer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package de.undercouch.bson4jackson.deserializers;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;

import de.undercouch.bson4jackson.BsonConstants;
import de.undercouch.bson4jackson.BsonParser;

/**
* Deserializes BSON date type objects to calendars
* @author Michel Kraemer
* @since 2.3.2
*/
public class BsonCalendarDeserializer extends BsonDeserializer<Calendar> {
@Override
public Calendar deserialize(BsonParser bsonParser, DeserializationContext ctxt)
throws IOException {
if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT ||
bsonParser.getCurrentBsonType() != BsonConstants.TYPE_DATETIME) {
ctxt.mappingException(Date.class);
}

Object obj = bsonParser.getEmbeddedObject();
if (obj == null) {
return null;
}

Calendar cal = Calendar.getInstance();
cal.setTime((Date)obj);
return cal;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2010-2014 Michel Kraemer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package de.undercouch.bson4jackson.deserializers;

import java.io.IOException;
import java.util.Date;

import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;

import de.undercouch.bson4jackson.BsonConstants;
import de.undercouch.bson4jackson.BsonParser;

/**
* Deserializes BSON date type objects to dates
* @author Michel Kraemer
* @since 2.3.2
*/
public class BsonDateDeserializer extends BsonDeserializer<Date> {
@Override
public Date deserialize(BsonParser bsonParser, DeserializationContext ctxt)
throws IOException {
if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT ||
bsonParser.getCurrentBsonType() != BsonConstants.TYPE_DATETIME) {
ctxt.mappingException(Date.class);
}

Object obj = bsonParser.getEmbeddedObject();
if (obj == null) {
return null;
}

return (Date)obj;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2010-2014 Michel Kraemer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package de.undercouch.bson4jackson.deserializers;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import de.undercouch.bson4jackson.BsonParser;

/**
* Base class for BSON deserializers
* @author Michel Kraemer
* @since 2.3.2
*/
public abstract class BsonDeserializer<T> extends JsonDeserializer<T> {
@Override
public T deserialize(JsonParser jsonParser, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
if (!(jsonParser instanceof BsonParser)) {
throw new JsonGenerationException("BsonDeserializer can " +
"only be used with BsonParser");
}
return deserialize((BsonParser)jsonParser, ctxt);
}

/**
* Deserialize an object using the given BsonParser
* @param bp the BsonParser read from
* @param ctxt context that can be used to access information about
* this deserialization activity
* @return the deserialized object
*/
public abstract T deserialize(BsonParser bp, DeserializationContext ctxt)
throws IOException, JsonProcessingException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2010-2014 Michel Kraemer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package de.undercouch.bson4jackson.deserializers;

import java.util.Calendar;
import java.util.Date;

import com.fasterxml.jackson.databind.module.SimpleDeserializers;

/**
* BSON deserializers
* @author Michel Kraemer
* @since 2.3.2
*/
public class BsonDeserializers extends SimpleDeserializers {
private static final long serialVersionUID = 261492073508673840L;

/**
* Default constructor
*/
public BsonDeserializers() {
addDeserializer(Date.class, new BsonDateDeserializer());
addDeserializer(Calendar.class, new BsonCalendarDeserializer());
}
}
Loading

0 comments on commit 4d1dfb2

Please sign in to comment.