public static void main(String[] args) throws IOException { // BEGIN EXAMPLE JCodeModel codeModel = new JCodeModel(); URL source = new URL("file:///path/to/my/schema.json"); GenerationConfig config = new DefaultGenerationConfig() { @Override public boolean isGenerateBuilders() { // set config option by overriding method return true; } }; SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator()); mapper.generate(codeModel, "ClassName", "com.example", source); codeModel.build(new File("output")); // END EXAMPLE }
public void createModel(File jsonFile, String modelName) throws IOException { //CapFirst for java classes modelName = modelName.substring(0, 1).toUpperCase() + modelName.substring(1); System.out.println("Model name :"+modelName); JCodeModel codeModel = new JCodeModel(); GenerationConfig config = new DefaultGenerationConfig() { @Override public boolean isIncludeConstructors() { return true; } @Override public boolean isUseDoubleNumbers() { return true; } @Override public boolean isUsePrimitives() { return true; } @Override public boolean isIncludeToString() { return false; } @Override public boolean isIncludeHashcodeAndEquals() { return false; } @Override public boolean isIncludeAdditionalProperties() { return false; } @Override public Class<? extends RuleFactory> getCustomRuleFactory() { return APXCustomRuleFactory.class; } }; SchemaMapper mapper = new SchemaMapper(new APXCustomRuleFactory(config, new ORMLiteAnotator(), new SchemaStore()), new SchemaGenerator()); JType m = mapper.generate(codeModel, modelName, PACKAGE_NAME + ".models", jsonFile.toURI().toURL()); File f = new File(PROJECT_SRC); codeModel.build(f); System.out.print("Model created at :"); System.out.println(m.fullName().replace('.', File.separatorChar) + ".java"); }
/** * Generates classes based on json/jsonschema. * * @return class name * @throws IOException */ public String generate() throws IOException { String schemaPath = this.config.getJsonSchemaPath(); if (schemas.containsKey(schemaPath)){ LOG.info("Schema already exists " + schemaPath); return schemas.get(schemaPath); } JCodeModel codeModel = new JCodeModel(); URL source = new File(config.getInputPath()).toURI().toURL(); GenerationConfig generationConfig = new DefaultGenerationConfig() { @Override public boolean isGenerateBuilders() { // set config option by overriding metho return true; } @Override public SourceType getSourceType() { if (JsonPath.from(source).get("$schema") != null) { return SourceType.JSONSCHEMA; } return SourceType.JSON; } @Override public boolean isUseLongIntegers() { return true; } @Override public boolean isUseCommonsLang3() { return true; } }; SchemaMapper mapper = new SchemaMapper( new RuleFactory(generationConfig, new GsonAnnotator(), new SchemaStore()), new SchemaGenerator()); mapper.generate(codeModel, getClassName(), config.getPackageName(), source); codeModel.build(new File(config.getOutputPath())); schemas.put(schemaPath, getClassName()); return getClassName(); }
/** * Returns a generation config with the supplied parameters. If any of these parameters are * supplied null, it will * use the value defined in the default configuration * * @param generateBuilders * Enables or disables {@link GenerationConfig#isGenerateBuilders()} * @param includeAdditionalProperties * Enables or disables {@link GenerationConfig#isIncludeAdditionalProperties()} * @param includeDynamicAccessors * Enables or disables {@link GenerationConfig#isIncludeDynamicAccessors()} * @param useLongIntegers * Enables or disables {@link GenerationConfig#isUseLongIntegers()} * @return The GenerationConfig */ public static GenerationConfig getGenerationConfig(Boolean generateBuilders, Boolean includeAdditionalProperties, Boolean includeDynamicAccessors, Boolean useLongIntegers) { return new DefaultGenerationConfig() { @Override public boolean isGenerateBuilders() { // set config option by overriding method if (generateBuilders != null) { return generateBuilders; } else { return true; } } @Override public boolean isIncludeAdditionalProperties() { if (includeAdditionalProperties != null) { return includeAdditionalProperties; } else { return false; } } @Override public boolean isIncludeDynamicAccessors() { if (includeDynamicAccessors != null) { return includeDynamicAccessors; } else { return false; } } @Override public boolean isUseLongIntegers() { if (useLongIntegers != null) { return useLongIntegers; } else { return super.isUseLongIntegers(); } } }; }
@Test public void schemaStoreIsReturned() { SchemaStore mockSchemaStore = mock(SchemaStore.class); RuleFactory ruleFactory = new RuleFactory(new DefaultGenerationConfig(), new NoopAnnotator(), mockSchemaStore); assertThat(ruleFactory.getSchemaStore(), is(sameInstance(mockSchemaStore))); }
/** * Create a rule factory with the default generation config options. * * @see DefaultGenerationConfig */ public RuleFactory() { this(new DefaultGenerationConfig(), new Jackson2Annotator(new DefaultGenerationConfig()), new SchemaStore()); }