-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1f9f9c
commit 0fb4d96
Showing
14 changed files
with
396 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
version=1.0.0-SNAPSHOT |
Binary file not shown.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Thu Jul 28 10:56:21 EDT 2016 | ||
#Sun Jul 31 02:03:03 EDT 2016 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-bin.zip |
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
62 changes: 62 additions & 0 deletions
62
src/main/java/com/github/jonpeterson/jackson/module/versioning/JsonVersionedModel.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,62 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Jon Peterson | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package com.github.jonpeterson.jackson.module.versioning; | ||
|
||
import com.fasterxml.jackson.annotation.JacksonAnnotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Specifies converter for used in resolving versioning for pre-deserialized JSON. | ||
*/ | ||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@JacksonAnnotation | ||
public @interface JsonVersionedModel { | ||
|
||
/** | ||
* @return the current version of the model; this will be added to the serialized model's {@link #propertyName()}. | ||
*/ | ||
String currentVersion(); | ||
|
||
/** | ||
* @return class of the converter to use when resolving versioning. | ||
*/ | ||
Class<? extends VersionedModelConverter> converterClass(); | ||
|
||
/** | ||
* @return whether to always send JSON data to converter, even when the data is the same version as the | ||
* {@link #currentVersion()}. | ||
*/ | ||
boolean alwaysConvert() default false; | ||
|
||
/** | ||
* @return name of property in which the model's version is stored in JSON. | ||
*/ | ||
String propertyName() default "modelVersion"; | ||
} |
10 changes: 0 additions & 10 deletions
10
src/main/java/com/github/jonpeterson/jackson/module/versioning/VersionedModel.java
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
src/main/java/com/github/jonpeterson/jackson/module/versioning/VersionedModelConverter.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,17 @@ | ||
package com.github.jonpeterson.jackson.module.versioning; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
/** | ||
* Converter used by {@link JsonVersionedModel} for resolving model versioning. | ||
*/ | ||
public interface VersionedModelConverter { | ||
|
||
/** | ||
* Updates JSON data before deserialization to model. | ||
* | ||
* @param modelVersion version of data | ||
* @param modelData JSON data to update | ||
*/ | ||
void convert(String modelVersion, ObjectNode modelData); | ||
} |
13 changes: 0 additions & 13 deletions
13
.../java/com/github/jonpeterson/jackson/module/versioning/VersionedModelDataTransformer.java
This file was deleted.
Oops, something went wrong.
61 changes: 32 additions & 29 deletions
61
...ain/java/com/github/jonpeterson/jackson/module/versioning/VersionedModelDeserializer.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 |
---|---|---|
@@ -1,59 +1,62 @@ | ||
package com.github.jonpeterson.jackson.module.versioning; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.TreeNode; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.deser.ResolvableDeserializer; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.databind.node.TreeTraversingParser; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.ParameterizedType; | ||
|
||
public class VersionedModelDeserializer extends StdDeserializer<VersionedModel> implements ResolvableDeserializer { | ||
private final JsonDeserializer<VersionedModel> delegateDeserializer; | ||
private final VersionedModelDataTransformer transformer; | ||
class VersionedModelDeserializer<T> extends StdDeserializer<T> implements ResolvableDeserializer { | ||
private final StdDeserializer<T> delegate; | ||
private final JsonVersionedModel jsonVersionedModel; | ||
private final VersionedModelConverter converter; | ||
|
||
protected VersionedModelDeserializer(JsonDeserializer<VersionedModel> delegateDeserializer, VersionedModelDataTransformer transformer) { | ||
super(VersionedModel.class); | ||
VersionedModelDeserializer(StdDeserializer<T> delegate, JsonVersionedModel jsonVersionedModel) { | ||
super(delegate.getValueType()); | ||
|
||
this.delegateDeserializer = delegateDeserializer; | ||
this.transformer = transformer; | ||
this.delegate = delegate; | ||
this.jsonVersionedModel = jsonVersionedModel; | ||
|
||
try { | ||
this.converter = jsonVersionedModel.converterClass().newInstance(); | ||
} catch(Exception e) { | ||
throw new RuntimeException("unable to create instance of converter '" + jsonVersionedModel.converterClass().getName() + "'", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void resolve(DeserializationContext context) throws JsonMappingException { | ||
((ResolvableDeserializer)delegateDeserializer).resolve(context); | ||
if(delegate instanceof ResolvableDeserializer) | ||
((ResolvableDeserializer)delegate).resolve(context); | ||
} | ||
|
||
@Override | ||
public VersionedModel deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { | ||
TreeNode treeNode = parser.readValueAsTree(); | ||
public T deserialize(JsonParser parser, DeserializationContext context) throws IOException { | ||
JsonNode jsonNode = parser.readValueAsTree(); | ||
|
||
if(!(treeNode instanceof ObjectNode)) | ||
if(!(jsonNode instanceof ObjectNode)) | ||
throw context.mappingException("value must be a JSON object"); | ||
|
||
ObjectNode objectNode = (ObjectNode)treeNode; | ||
ObjectNode modelData = (ObjectNode)jsonNode; | ||
|
||
JsonNode modelVersionNode = objectNode.remove(VersionedModel.MODEL_VERSION_PROPERTY); | ||
JsonNode modelVersionNode = modelData.remove(jsonVersionedModel.propertyName()); | ||
if(modelVersionNode == null) | ||
throw context.mappingException("'" + VersionedModel.MODEL_VERSION_PROPERTY + "' property was not present"); | ||
|
||
// DEBUG HERE | ||
//Class<?> abc = delegateDeserializer.handledType(); | ||
//((ParameterizedType)abc.getGenericInterfaces()[0]).getActualTypeArguments()[0] | ||
throw context.mappingException("'" + jsonVersionedModel.propertyName() + "' property was not present"); | ||
|
||
String modelVersion = modelVersionNode.asText(); | ||
if(modelVersion == null) | ||
throw context.mappingException("'" + VersionedModel.MODEL_VERSION_PROPERTY + "' property was null"); | ||
throw context.mappingException("'" + jsonVersionedModel.propertyName() + "' property was null"); | ||
|
||
if(transformer != null && !modelVersion.equals(transformer.targetVersion)) | ||
transformer.transform(modelVersion, objectNode); | ||
if(jsonVersionedModel.alwaysConvert() || !modelVersion.equals(jsonVersionedModel.currentVersion())) | ||
converter.convert(modelVersion, modelData); | ||
|
||
JsonParser objectNodeParser = new TreeTraversingParser(objectNode); | ||
objectNodeParser.nextToken(); | ||
return delegateDeserializer.deserialize(objectNodeParser, context); | ||
JsonParser postInterceptionParser = new TreeTraversingParser(jsonNode, parser.getCodec()); | ||
postInterceptionParser.nextToken(); | ||
return delegate.deserialize(postInterceptionParser, context); | ||
} | ||
} | ||
} |
Oops, something went wrong.