Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLANNER-1041 Jackson support for non-subtyped Score field + jackson m…
…odule
- Loading branch information
Showing
38 changed files
with
923 additions
and
40 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
120 changes: 120 additions & 0 deletions
120
...ckson/src/main/java/org/optaplanner/persistence/jackson/api/OptaPlannerJacksonModule.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,120 @@ | ||
/* | ||
* Copyright 2018 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* 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 org.optaplanner.persistence.jackson.api; | ||
|
||
import com.fasterxml.jackson.databind.BeanDescription; | ||
import com.fasterxml.jackson.databind.BeanProperty; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.Module; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationConfig; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; | ||
import com.fasterxml.jackson.databind.ser.ContextualSerializer; | ||
import org.optaplanner.core.api.score.Score; | ||
import org.optaplanner.core.api.score.buildin.bendable.BendableScore; | ||
import org.optaplanner.core.api.score.buildin.bendablebigdecimal.BendableBigDecimalScore; | ||
import org.optaplanner.core.api.score.buildin.bendablelong.BendableLongScore; | ||
import org.optaplanner.core.api.score.buildin.hardmediumsoft.HardMediumSoftScore; | ||
import org.optaplanner.core.api.score.buildin.hardmediumsoftlong.HardMediumSoftLongScore; | ||
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore; | ||
import org.optaplanner.core.api.score.buildin.hardsoftbigdecimal.HardSoftBigDecimalScore; | ||
import org.optaplanner.core.api.score.buildin.hardsoftdouble.HardSoftDoubleScore; | ||
import org.optaplanner.core.api.score.buildin.hardsoftlong.HardSoftLongScore; | ||
import org.optaplanner.core.api.score.buildin.simple.SimpleScore; | ||
import org.optaplanner.core.api.score.buildin.simplebigdecimal.SimpleBigDecimalScore; | ||
import org.optaplanner.core.api.score.buildin.simpledouble.SimpleDoubleScore; | ||
import org.optaplanner.core.api.score.buildin.simplelong.SimpleLongScore; | ||
import org.optaplanner.persistence.jackson.api.score.PolymorphicScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.PolymorphicScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.ScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.bendable.BendableScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.bendable.BendableScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.bendablebigdecimal.BendableBigDecimalScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.bendablebigdecimal.BendableBigDecimalScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.bendablelong.BendableLongScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.bendablelong.BendableLongScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardmediumsoft.HardMediumSoftScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardmediumsoft.HardMediumSoftScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardmediumsoftlong.HardMediumSoftLongScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardmediumsoftlong.HardMediumSoftLongScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardsoft.HardSoftScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardsoft.HardSoftScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardsoftbigdecimal.HardSoftBigDecimalScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardsoftbigdecimal.HardSoftBigDecimalScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardsoftdouble.HardSoftDoubleScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardsoftdouble.HardSoftDoubleScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardsoftlong.HardSoftLongScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.hardsoftlong.HardSoftLongScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.simple.SimpleScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.simple.SimpleScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.simplebigdecimal.SimpleBigDecimalScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.simplebigdecimal.SimpleBigDecimalScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.simpledouble.SimpleDoubleScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.simpledouble.SimpleDoubleScoreJacksonJsonSerializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.simplelong.SimpleLongScoreJacksonJsonDeserializer; | ||
import org.optaplanner.persistence.jackson.api.score.buildin.simplelong.SimpleLongScoreJacksonJsonSerializer; | ||
|
||
/** | ||
* This class adds all Jackson serializers and deserializers. | ||
* | ||
*/ | ||
public class OptaPlannerJacksonModule { | ||
|
||
/** | ||
* @return never null, register it with {@link ObjectMapper#registerModule(Module)}. | ||
*/ | ||
public static Module createModule() { | ||
SimpleModule module = new SimpleModule("OptaPlanner"); | ||
|
||
// For non-subtype Score fields/properties, we also need to record the score type | ||
module.addSerializer(Score.class, new PolymorphicScoreJacksonJsonSerializer()); | ||
module.addDeserializer(Score.class, new PolymorphicScoreJacksonJsonDeserializer()); | ||
|
||
module.addSerializer(SimpleScore.class, new SimpleScoreJacksonJsonSerializer()); | ||
module.addDeserializer(SimpleScore.class, new SimpleScoreJacksonJsonDeserializer()); | ||
module.addSerializer(SimpleLongScore.class, new SimpleLongScoreJacksonJsonSerializer()); | ||
module.addDeserializer(SimpleLongScore.class, new SimpleLongScoreJacksonJsonDeserializer()); | ||
module.addSerializer(SimpleDoubleScore.class, new SimpleDoubleScoreJacksonJsonSerializer()); | ||
module.addDeserializer(SimpleDoubleScore.class, new SimpleDoubleScoreJacksonJsonDeserializer()); | ||
module.addSerializer(SimpleBigDecimalScore.class, new SimpleBigDecimalScoreJacksonJsonSerializer()); | ||
module.addDeserializer(SimpleBigDecimalScore.class, new SimpleBigDecimalScoreJacksonJsonDeserializer()); | ||
module.addSerializer(HardSoftScore.class, new HardSoftScoreJacksonJsonSerializer()); | ||
module.addDeserializer(HardSoftScore.class, new HardSoftScoreJacksonJsonDeserializer()); | ||
module.addSerializer(HardSoftLongScore.class, new HardSoftLongScoreJacksonJsonSerializer()); | ||
module.addDeserializer(HardSoftLongScore.class, new HardSoftLongScoreJacksonJsonDeserializer()); | ||
module.addSerializer(HardSoftDoubleScore.class, new HardSoftDoubleScoreJacksonJsonSerializer()); | ||
module.addDeserializer(HardSoftDoubleScore.class, new HardSoftDoubleScoreJacksonJsonDeserializer()); | ||
module.addSerializer(HardSoftBigDecimalScore.class, new HardSoftBigDecimalScoreJacksonJsonSerializer()); | ||
module.addDeserializer(HardSoftBigDecimalScore.class, new HardSoftBigDecimalScoreJacksonJsonDeserializer()); | ||
module.addSerializer(HardMediumSoftScore.class, new HardMediumSoftScoreJacksonJsonSerializer()); | ||
module.addDeserializer(HardMediumSoftScore.class, new HardMediumSoftScoreJacksonJsonDeserializer()); | ||
module.addSerializer(HardMediumSoftLongScore.class, new HardMediumSoftLongScoreJacksonJsonSerializer()); | ||
module.addDeserializer(HardMediumSoftLongScore.class, new HardMediumSoftLongScoreJacksonJsonDeserializer()); | ||
module.addSerializer(BendableScore.class, new BendableScoreJacksonJsonSerializer()); | ||
module.addDeserializer(BendableScore.class, new BendableScoreJacksonJsonDeserializer()); | ||
module.addSerializer(BendableLongScore.class, new BendableLongScoreJacksonJsonSerializer()); | ||
module.addDeserializer(BendableLongScore.class, new BendableLongScoreJacksonJsonDeserializer()); | ||
module.addSerializer(BendableBigDecimalScore.class, new BendableBigDecimalScoreJacksonJsonSerializer()); | ||
module.addDeserializer(BendableBigDecimalScore.class, new BendableBigDecimalScoreJacksonJsonDeserializer()); | ||
|
||
return module; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...rsistence-jackson/src/main/java/org/optaplanner/persistence/jackson/api/package-info.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,20 @@ | ||
/* | ||
* Copyright 2018 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Jackson binders. | ||
*/ | ||
package org.optaplanner.persistence.jackson.api; |
99 changes: 99 additions & 0 deletions
99
...rg/optaplanner/persistence/jackson/api/score/PolymorphicScoreJacksonJsonDeserializer.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,99 @@ | ||
/* | ||
* Copyright 2018 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* 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 org.optaplanner.persistence.jackson.api.score; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.optaplanner.core.api.score.Score; | ||
import org.optaplanner.core.api.score.buildin.bendable.BendableScore; | ||
import org.optaplanner.core.api.score.buildin.bendablebigdecimal.BendableBigDecimalScore; | ||
import org.optaplanner.core.api.score.buildin.bendablelong.BendableLongScore; | ||
import org.optaplanner.core.api.score.buildin.hardmediumsoft.HardMediumSoftScore; | ||
import org.optaplanner.core.api.score.buildin.hardmediumsoftlong.HardMediumSoftLongScore; | ||
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore; | ||
import org.optaplanner.core.api.score.buildin.hardsoftbigdecimal.HardSoftBigDecimalScore; | ||
import org.optaplanner.core.api.score.buildin.hardsoftdouble.HardSoftDoubleScore; | ||
import org.optaplanner.core.api.score.buildin.hardsoftlong.HardSoftLongScore; | ||
import org.optaplanner.core.api.score.buildin.simple.SimpleScore; | ||
import org.optaplanner.core.api.score.buildin.simplebigdecimal.SimpleBigDecimalScore; | ||
import org.optaplanner.core.api.score.buildin.simpledouble.SimpleDoubleScore; | ||
import org.optaplanner.core.api.score.buildin.simplelong.SimpleLongScore; | ||
import org.optaplanner.core.api.score.constraint.ConstraintMatch; | ||
import org.optaplanner.core.impl.score.definition.ScoreDefinition; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public class PolymorphicScoreJacksonJsonDeserializer extends JsonDeserializer<Score> { | ||
|
||
@Override | ||
public Score deserialize(JsonParser parser, DeserializationContext context) throws IOException { | ||
JsonNode node = parser.getCodec().readTree(parser); | ||
Iterator<Map.Entry<String, JsonNode>> iterator = node.fields(); | ||
if (!iterator.hasNext()) { | ||
throw new IllegalStateException("A polymorphic score field's JSON node (" + node.toString() | ||
+ ") doesn't have any field."); | ||
} | ||
Map.Entry<String, JsonNode> entry = iterator.next(); | ||
if (iterator.hasNext()) { | ||
throw new IllegalStateException("A polymorphic score field's JSON node (" + node.toString() | ||
+ ") has multiple fields."); | ||
} | ||
String scoreClassSimpleName = entry.getKey(); | ||
String scoreString = entry.getValue().asText(); | ||
if (scoreClassSimpleName.equals(SimpleScore.class.getSimpleName())) { | ||
return SimpleScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(SimpleLongScore.class.getSimpleName())) { | ||
return SimpleLongScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(SimpleDoubleScore.class.getSimpleName())) { | ||
return SimpleDoubleScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(SimpleBigDecimalScore.class.getSimpleName())) { | ||
return SimpleBigDecimalScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(HardSoftScore.class.getSimpleName())) { | ||
return HardSoftScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(HardSoftLongScore.class.getSimpleName())) { | ||
return HardSoftLongScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(HardSoftDoubleScore.class.getSimpleName())) { | ||
return HardSoftDoubleScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(HardSoftBigDecimalScore.class.getSimpleName())) { | ||
return HardSoftBigDecimalScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(HardMediumSoftScore.class.getSimpleName())) { | ||
return HardMediumSoftScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(HardMediumSoftLongScore.class.getSimpleName())) { | ||
return HardMediumSoftLongScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(BendableScore.class.getSimpleName())) { | ||
return BendableScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(BendableLongScore.class.getSimpleName())) { | ||
return BendableLongScore.parseScore(scoreString); | ||
} else if (scoreClassSimpleName.equals(BendableBigDecimalScore.class.getSimpleName())) { | ||
return BendableBigDecimalScore.parseScore(scoreString); | ||
} else { | ||
throw new IllegalArgumentException("Unrecognized scoreClassSimpleName (" + scoreClassSimpleName | ||
+ ") for scoreString (" + scoreString + ")."); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.