Skip to content

Instantly share code, notes, and snippets.

@afranzi
Created March 13, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afranzi/603b24c9e431f418b111469501ebdf48 to your computer and use it in GitHub Desktop.
Save afranzi/603b24c9e431f418b111469501ebdf48 to your computer and use it in GitHub Desktop.
JSON Schema builder with LoadingCache
val schemaCache: LoadingCache[String, Schema] = CacheBuilder
.newBuilder
.maximumSize(MaximumCacheSize)
.expireAfterAccess(CacheMinutes, TimeUnit.MINUTES)
.build(new CacheLoader[String, Schema]() {
override def load(schemaRef: String): Schema = {
logger.info(s"Loading schema $schemaRef")
loadSchema(schemaRef: String)
}
})
def buildSchema(schema: JSONObject): Schema = {
SchemaLoader.builder()
.schemaJson(schema)
.schemaClient(new ResourceSchemaClient)
.draftV7Support()
.useDefaults(true)
.build()
.load()
.build()
}
def loadSchema(schemaRef: String): Schema = {
val schemaObject = Try(readJsonObject(schemaRef)) match {
case Success(reader) => reader
case Failure(_: NullPointerException) => throw SchemaNotFoundException(schemaRef)
case Failure(exception) => throw exception
}
buildSchema(schemaObject)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment