JsonNode schemaNode = mapper.generateJsonSchema( cls ).getSchemaNode(); if ( schemaNode != null ) { JsonNode properties = schemaNode.get( "properties" );
try{ ObjectMapper json=new ObjectMapper(); json.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); System.out.println(json.generateJsonSchema(University.class).toString()); }catch(Exception e){ throw new RuntimeException(e); }
ObjectMapper objectMapper = new ObjectMapper(); // this line will generate JSON schema from your class JsonNode schemaNode = objectMapper.generateJsonSchema(StageDetail.class).getSchemaNode(); // make your JSON to JsonNode JsonNode jsonToValidate = JsonLoader.fromString(JSON_TO_VALIDATE); // validate it against the schema ProcessingReport validate = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode).validate(jsonToValidate); // validate.messages contains error massages System.out.println("Valid? " + validate.isSuccess());
import ...TargetClass; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.jsonschema.JsonSchema; import java.io.IOException; public final class JsonSchemaGenerator { private JsonSchemaGenerator() { }; public static void main(String[] args) throws IOException { System.out.println(JsonSchemaGenerator.getJsonSchema(TargetClass.class)); } public static String getJsonSchema(Class clazz) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); JsonSchema schema = mapper.generateJsonSchema(clazz); return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema); } }
public String getSchemaForClass(Class<?> clazz) { LOG.info("Looking up schema for " + clazz.getCanonicalName()); String name = clazz.getName(); try { ObjectWriter writer = mapper.writer().with(new FourSpacePrettyPrinter()); return writer.writeValueAsString(mapper.generateJsonSchema(clazz)); } catch (Exception e) { LOG.log(Level.FINEST, "Failed to generate JSON schema for class " + name, e); return ""; } }
public String getSchemaForClass(Class<?> clazz) { LOG.debug("Looking up schema for {}", clazz.getCanonicalName()); String name = clazz.getName(); try { ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter(); JsonSchema jsonSchema = mapper.generateJsonSchema(clazz); customizeSchema(clazz, jsonSchema); return writer.writeValueAsString(jsonSchema); // SchemaFactoryWrapper schemaFactoryWrapper = new SchemaFactoryWrapper(); // mapper.acceptJsonFormatVisitor(mapper.constructType(clazz), schemaFactoryWrapper); // com.fasterxml.jackson.module.jsonSchema.JsonSchema jsonSchema = schemaFactoryWrapper.finalSchema(); // return writer.writeValueAsString(jsonSchema); } catch (Exception e) { LOG.warn("Failed to generate JSON schema for class " + name, e); throw new RuntimeException(e); } }
private JsonSchema createSchema(Class<?> clazz) { ObjectMapper mapper = new ObjectMapper(); try { mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); return mapper.generateJsonSchema(clazz); } catch (JsonMappingException e) { LOGGER.error(e.getMessage(), e); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } return null; } }
private JsonSchema createSchema(Class<?> clazz) { ObjectMapper mapper = new ObjectMapper(); try { mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); return mapper.generateJsonSchema(clazz); } catch (JsonMappingException e) { LOGGER.error(e.getMessage(), e); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } return null; } }
s = s.replace("\",}", "\"}").replace("},}", "}}"); System.out.println(s); s = mapper.generateJsonSchema(cls).getSchemaNode().put("type", "array") .put("classnames", s).toString(); s = s.replace("\\", "").replace("\"{", "{").replace("}\"", "}");
s = s.replace("\",}", "\"}").replace("},}", "}}"); System.out.println(s); s = mapper.generateJsonSchema(cls).getSchemaNode().put("type", "array") .put("classnames", s).toString(); s = s.replace("\\", "").replace("\"{", "{").replace("}\"", "}");