-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added deserializers for Date and Calendar
Also added unit test to make sure we don't need deserializers for any other type.
- Loading branch information
1 parent
b47ce7e
commit 4d1dfb2
Showing
7 changed files
with
394 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/de/undercouch/bson4jackson/deserializers/BsonCalendarDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/de/undercouch/bson4jackson/deserializers/BsonDateDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/de/undercouch/bson4jackson/deserializers/BsonDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/de/undercouch/bson4jackson/deserializers/BsonDeserializers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.