This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add convenience method to JsonToSchemaConverter API
- Loading branch information
Showing
9 changed files
with
106 additions
and
63 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
42 changes: 0 additions & 42 deletions
42
catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverter.java
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonToSchemaConverter.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,57 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
import static java.lang.String.format; | ||
|
||
import org.everit.json.schema.Schema; | ||
import org.everit.json.schema.loader.SchemaClient; | ||
import org.everit.json.schema.loader.SchemaLoader; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.json.JSONTokener; | ||
|
||
/** | ||
* Converts a raw Json Schema into an Everit {@link Schema} resolving any referred Schemas | ||
* in the raw Json Schema to their location on the classpath | ||
*/ | ||
public class JsonToSchemaConverter { | ||
|
||
/** | ||
* Converts a raw Json Schema into an Everit {@link Schema} resolving any referred Schemas | ||
* in the raw Json Schema to their location on the classpath | ||
* | ||
* @param schemaJson The raw Json Schema as a {@link String} | ||
* @param schemaClient An implementation of Everit's {@link SchemaClient} that | ||
* rather than returning a schema found at the Schema id, | ||
* returns a Schema found on the classpath mapped to that | ||
* Schema id | ||
* @return A fully resolved Everit {@link Schema} | ||
*/ | ||
public Schema convert(final String schemaJson, final SchemaClient schemaClient) { | ||
try { | ||
final JSONObject schemaJsonObject = new JSONObject(new JSONTokener(schemaJson)); | ||
return convert(schemaJsonObject, schemaClient); | ||
} catch (final JSONException e) { | ||
throw new SchemaCatalogException(format("Failed to convert schema json to Schema Object. Schema json:%n%s", schemaJson), e); | ||
} | ||
} | ||
|
||
/** | ||
* Converts a raw Json Schema into an Everit {@link Schema} resolving any referred Schemas | ||
* in the raw Json Schema to their location on the classpath | ||
* | ||
* @param schemaJsonObject The raw Json Schema as a {@link JSONObject} | ||
* @param schemaClient An implementation of Everit's {@link SchemaClient} that | ||
* rather than returning a schema found at the Schema id, | ||
* returns a Schema found on the classpath mapped to that | ||
* Schema id | ||
* @return A fully resolved Everit {@link Schema} | ||
*/ | ||
public Schema convert(final JSONObject schemaJsonObject, final SchemaClient schemaClient) { | ||
return SchemaLoader.builder() | ||
.schemaJson(schemaJsonObject) | ||
.httpClient(schemaClient) | ||
.build() | ||
.load() | ||
.build(); | ||
} | ||
} |
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
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
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