public static void main(String[] args) throws ParseException, InterruptedException, IOException { /* * Parse cli arguments */ Options options = new Options(); options.addOption(Option.builder().longOpt("out-file").hasArg() .desc("Filename to output schema to. Default: schema.json").build()); options.addOption(Option.builder().longOpt("docson").hasArg(false) .desc("Create a schema that is able to be read by docson").build()); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); String filename = cmd.getOptionValue("out-file", "schema.json"); /* * Write schema */ BenderSchema schema = new BenderSchema(); ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); JsonNode node = schema.getSchema(); if (cmd.hasOption("docson")) { modifyNode(node); } mapper.writeValue(new File(filename), node); }
public static boolean validate(String data, ObjectMapper objectMapper, BenderSchema benderSchema) throws ConfigurationException { ProcessingReport report; try { /* * Create object */ JsonNode node = objectMapper.readTree(data); /* * Create JSON schema */ JsonNode jsonSchema = benderSchema.getSchema(); /* * Validate */ final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); final JsonSchema schema = factory.getJsonSchema(jsonSchema); report = schema.validate(node); } catch (IOException | ProcessingException ioe) { throw new ConfigurationException("unable to validate config", ioe); } if (report.isSuccess()) { return true; } else { throw new ConfigurationException("invalid config file", report.iterator().next().asException()); } }
public static boolean validate(String data, ObjectMapper objectMapper, BenderSchema benderSchema) throws ConfigurationException { ProcessingReport report; try { /* * Create object */ JsonNode node = objectMapper.readTree(data); /* * Create JSON schema */ JsonNode jsonSchema = benderSchema.getSchema(); /* * Validate */ final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); final JsonSchema schema = factory.getJsonSchema(jsonSchema); report = schema.validate(node); } catch (IOException | ProcessingException ioe) { throw new ConfigurationException("unable to validate config", ioe); } if (report.isSuccess()) { return true; } else { throw new ConfigurationException("invalid config file", report.iterator().next().asException()); } }
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException { String filename = args[0]; new File(filename).getParentFile().mkdirs(); BenderSchema schema = new BenderSchema(); ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); JsonNode node = schema.getSchema(); mapper.writeValue(new File(filename), node); } }
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException { String filename = args[0]; new File(filename).getParentFile().mkdirs(); BenderSchema schema = new BenderSchema(); ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); JsonNode node = schema.getSchema(); mapper.writeValue(new File(filename), node); } }