Last active
December 1, 2021 12:20
-
-
Save senthilsivanath/2722da33c2c146d46b4f598b5e17a726 to your computer and use it in GitHub Desktop.
JSON Schema Any Of and All Of De-serialization In 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
package ai.tfs.jwtverify; | |
public class Check { | |
public String path; | |
public String threshold; | |
} |
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
package ai.tfs.jwtverify; | |
public class Condition { | |
public UnionCheckCondition allOf; | |
public UnionCheckCondition anyOf; | |
} | |
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
@GetMapping("verify") | |
public String validate() throws JwtVerificationException, ExecutionException, InterruptedException, JsonProcessingException { | |
// With checks | |
Condition condition = objectMapper.readValue("{ \"allOf\":[{\"path\":\"test\",\"threshold\":\"6\"}] }", Condition.class); | |
// With nested | |
Condition condition1 = objectMapper.readValue("{ \"allOf\":[ { \"allOf\":[{\"path\":\"test\",\"threshold\":\"6\"}] } ] }", Condition.class); | |
// With nested and anyOf | |
Condition condition2 = objectMapper.readValue("{ \"anyOf\":[ { \"allOf\":[{\"path\":\"test\",\"threshold\":\"6\"}] } ] }", Condition.class); | |
// Used to validate serializer | |
String data = objectMapper.writeValueAsString(condition2); | |
} |
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
package ai.tfs.jwtverify.configurations; | |
import ai.tfs.jwtverify.UnionCheckCondition; | |
import com.fasterxml.jackson.core.Version; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import com.fasterxml.jackson.databind.module.SimpleModule; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class ObjectMapperConfig { | |
@Bean | |
public ObjectMapper objectMapper(){ | |
SimpleModule module = new SimpleModule("UnionCheckConditionDeserializer", | |
new Version(1, 0, 0, null, "ai.tfs.jwtverify", "unioncheck-deserializer")); | |
UnionCheckConditionDeserializer exampleDeserializer = new UnionCheckConditionDeserializer(); | |
module.addDeserializer(UnionCheckCondition.class, exampleDeserializer); | |
module.addSerializer(UnionCheckCondition.class,exampleSerializer); | |
ObjectMapper mapper = new ObjectMapper() | |
.registerModule(module) | |
.enable(SerializationFeature.INDENT_OUTPUT); | |
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); | |
return mapper; | |
} | |
} |
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
package ai.tfs.jwtverify; | |
import com.fasterxml.jackson.annotation.JsonIgnore; | |
import java.util.List; | |
public class UnionCheckCondition { | |
@JsonIgnore | |
public List<Condition> conditions; | |
@JsonIgnore | |
public List<Check> checks; | |
} |
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
package ai.tfs.jwtverify.configurations; | |
import ai.tfs.jwtverify.Check; | |
import ai.tfs.jwtverify.Condition; | |
import ai.tfs.jwtverify.UnionCheckCondition; | |
import com.fasterxml.jackson.core.JsonParser; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.core.type.TypeReference; | |
import com.fasterxml.jackson.databind.DeserializationContext; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | |
import java.util.List; | |
import java.io.IOException; | |
public class UnionCheckConditionDeserializer extends StdDeserializer<UnionCheckCondition> { | |
public UnionCheckConditionDeserializer() { | |
super(UnionCheckCondition.class); | |
} | |
@Override | |
public UnionCheckCondition deserialize(JsonParser p, DeserializationContext ctxt) | |
throws IOException, JsonProcessingException { | |
ObjectMapper mapper = (ObjectMapper) p.getCodec(); | |
JsonNode tree = mapper.readTree(p); | |
UnionCheckCondition unionCheckCondition = new UnionCheckCondition(); | |
JsonNode typeNode = tree.get(0).get("path"); | |
if (typeNode == null || typeNode.asText().isEmpty()) { | |
// TO DO : replace with setter | |
unionCheckCondition.conditions = mapper.readValue(tree.toString(), new TypeReference<List<Condition>>(){}); | |
} | |
else{ | |
// TO DO : replace with setter | |
unionCheckCondition.checks = mapper.readValue(tree.toString(), new TypeReference<List<Check>>(){}); | |
} | |
return unionCheckCondition; | |
} | |
} |
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
package ai.tfs.jwtverify.configurations; | |
import ai.tfs.jwtverify.UnionCheckCondition; | |
import com.fasterxml.jackson.core.JsonGenerator; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import com.fasterxml.jackson.databind.SerializerProvider; | |
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | |
import java.io.IOException; | |
public class UnionCheckConditionSerializer extends StdSerializer<UnionCheckCondition> { | |
protected UnionCheckConditionSerializer() { | |
super(UnionCheckCondition.class, true); | |
} | |
@Override | |
public void serialize(UnionCheckCondition value, JsonGenerator gen, SerializerProvider provider) throws IOException { | |
ObjectMapper mapper = new ObjectMapper(); | |
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); | |
if(value.checks != null){ | |
gen.writeObject(value.checks); | |
} | |
else{ | |
gen.writeObject(value.conditions); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment