Search in sources :

Example 1 with RuleFactory

use of org.jsonschema2pojo.rules.RuleFactory in project useful-java-links by Vedenin.

the class JsonSchema2HelloWorld method main.

public static void main(String[] args) throws Exception {
    // Init json
    String source = "{\n" + "  \"type\":\"object\",\n" + "  \"properties\": {\n" + "    \"messageHiWorld\": {\n" + "      \"type\": \"string\"\n" + "    },\n" + "    \"bar\": {\n" + "      \"type\": \"integer\"\n" + "    },\n" + "    \"baz\": {\n" + "      \"type\": \"boolean\"\n" + "    }\n" + "  }\n" + "}";
    // Init config
    JCodeModel codeModel = new JCodeModel();
    GenerationConfig config = new DefaultGenerationConfig() {

        @Override
        public boolean isGenerateBuilders() {
            // set config option by overriding method
            return true;
        }
    };
    // Generate Java POJO from json
    SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(), new SchemaStore()), new SchemaGenerator());
    mapper.generate(codeModel, "HelloWorldClass", "com.github.vedenin", source);
    // Save generated class to file
    File directory = new File("helloworlds/3.8-json/jsonschema2pojo/output");
    directory.mkdirs();
    codeModel.build(directory);
    // Show generated class
    File cls = new File("helloworlds/3.8-json/jsonschema2pojo/output/com/github/vedenin/HelloWorldClass.java");
    String codeHelloWorld = Files.toString(cls, Charsets.UTF_8);
    System.out.println(codeHelloWorld);
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) JCodeModel(com.sun.codemodel.JCodeModel) File(java.io.File)

Example 2 with RuleFactory

use of org.jsonschema2pojo.rules.RuleFactory in project jsonschema2pojo by joelittlejohn.

the class Example method main.

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
}
Also used : DefaultGenerationConfig(org.jsonschema2pojo.DefaultGenerationConfig) RuleFactory(org.jsonschema2pojo.rules.RuleFactory) SchemaStore(org.jsonschema2pojo.SchemaStore) Jackson2Annotator(org.jsonschema2pojo.Jackson2Annotator) SchemaGenerator(org.jsonschema2pojo.SchemaGenerator) JCodeModel(com.sun.codemodel.JCodeModel) File(java.io.File) URL(java.net.URL) GenerationConfig(org.jsonschema2pojo.GenerationConfig) DefaultGenerationConfig(org.jsonschema2pojo.DefaultGenerationConfig) SchemaMapper(org.jsonschema2pojo.SchemaMapper)

Example 3 with RuleFactory

use of org.jsonschema2pojo.rules.RuleFactory in project jsonschema2pojo by joelittlejohn.

the class SchemaMapperTest method generateReadsSchemaAsObject.

@Test
public void generateReadsSchemaAsObject() throws IOException {
    final SchemaRule mockSchemaRule = mock(SchemaRule.class);
    final RuleFactory mockRuleFactory = mock(RuleFactory.class);
    when(mockRuleFactory.getSchemaRule()).thenReturn(mockSchemaRule);
    when(mockRuleFactory.getGenerationConfig()).thenReturn(new DefaultGenerationConfig());
    URL schemaContent = this.getClass().getResource("/schema/address.json");
    new SchemaMapper(mockRuleFactory, new SchemaGenerator()).generate(new JCodeModel(), "Address", "com.example.package", schemaContent);
    ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class);
    ArgumentCaptor<JsonNode> captureNode = ArgumentCaptor.forClass(JsonNode.class);
    verify(mockSchemaRule).apply(eq("Address"), captureNode.capture(), capturePackage.capture(), Mockito.isA(Schema.class));
    assertThat(capturePackage.getValue().name(), is("com.example.package"));
    assertThat(captureNode.getValue(), is(notNullValue()));
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) JPackage(com.sun.codemodel.JPackage) SchemaRule(org.jsonschema2pojo.rules.SchemaRule) JsonNode(com.fasterxml.jackson.databind.JsonNode) JCodeModel(com.sun.codemodel.JCodeModel) URL(java.net.URL) Test(org.junit.Test)

Example 4 with RuleFactory

use of org.jsonschema2pojo.rules.RuleFactory in project jsonschema2pojo by joelittlejohn.

the class SchemaMapperTest method generateCreatesSchemaFromSchemaAsStringInput.

@Test
public void generateCreatesSchemaFromSchemaAsStringInput() throws IOException {
    String schemaContent = IOUtils.toString(this.getClass().getResourceAsStream("/schema/address.json"));
    final SchemaRule mockSchemaRule = mock(SchemaRule.class);
    final RuleFactory mockRuleFactory = mock(RuleFactory.class);
    when(mockRuleFactory.getSchemaRule()).thenReturn(mockSchemaRule);
    when(mockRuleFactory.getGenerationConfig()).thenReturn(new DefaultGenerationConfig());
    new SchemaMapper(mockRuleFactory, new SchemaGenerator()).generate(new JCodeModel(), "Address", "com.example.package", schemaContent);
    ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class);
    ArgumentCaptor<JsonNode> captureNode = ArgumentCaptor.forClass(JsonNode.class);
    verify(mockSchemaRule).apply(eq("Address"), captureNode.capture(), capturePackage.capture(), Mockito.isA(Schema.class));
    assertThat(capturePackage.getValue().name(), is("com.example.package"));
    assertThat(captureNode.getValue(), is(notNullValue()));
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) JPackage(com.sun.codemodel.JPackage) SchemaRule(org.jsonschema2pojo.rules.SchemaRule) JsonNode(com.fasterxml.jackson.databind.JsonNode) JCodeModel(com.sun.codemodel.JCodeModel) Test(org.junit.Test)

Example 5 with RuleFactory

use of org.jsonschema2pojo.rules.RuleFactory in project jsonschema2pojo by joelittlejohn.

the class SchemaMapperTest method generateCreatesSchemaFromExampleJSONAsStringInput.

@Test
public void generateCreatesSchemaFromExampleJSONAsStringInput() throws IOException {
    String jsonContent = IOUtils.toString(this.getClass().getResourceAsStream("/example-json/user.json"));
    ObjectNode schemaNode = JsonNodeFactory.instance.objectNode();
    final SchemaRule mockSchemaRule = mock(SchemaRule.class);
    final GenerationConfig mockGenerationConfig = mock(GenerationConfig.class);
    when(mockGenerationConfig.getSourceType()).thenReturn(SourceType.JSON);
    final SchemaGenerator mockSchemaGenerator = mock(SchemaGenerator.class);
    when(mockSchemaGenerator.schemaFromExample(new ObjectMapper().readTree(jsonContent))).thenReturn(schemaNode);
    final RuleFactory mockRuleFactory = mock(RuleFactory.class);
    when(mockRuleFactory.getSchemaRule()).thenReturn(mockSchemaRule);
    when(mockRuleFactory.getGenerationConfig()).thenReturn(mockGenerationConfig);
    new SchemaMapper(mockRuleFactory, mockSchemaGenerator).generate(new JCodeModel(), "User", "com.example.package", jsonContent);
    ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class);
    verify(mockSchemaRule).apply(eq("User"), eq(schemaNode), capturePackage.capture(), Mockito.isA(Schema.class));
    assertThat(capturePackage.getValue().name(), is("com.example.package"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RuleFactory(org.jsonschema2pojo.rules.RuleFactory) JPackage(com.sun.codemodel.JPackage) SchemaRule(org.jsonschema2pojo.rules.SchemaRule) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

JCodeModel (com.sun.codemodel.JCodeModel)9 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)9 JPackage (com.sun.codemodel.JPackage)6 Test (org.junit.Test)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 URL (java.net.URL)4 SchemaRule (org.jsonschema2pojo.rules.SchemaRule)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 File (java.io.File)2 Schema (org.jsonschema2pojo.Schema)2 CodeWriter (com.sun.codemodel.CodeWriter)1 DefaultGenerationConfig (org.jsonschema2pojo.DefaultGenerationConfig)1 GenerationConfig (org.jsonschema2pojo.GenerationConfig)1 Jackson2Annotator (org.jsonschema2pojo.Jackson2Annotator)1 SchemaGenerator (org.jsonschema2pojo.SchemaGenerator)1 SchemaMapper (org.jsonschema2pojo.SchemaMapper)1 SchemaStore (org.jsonschema2pojo.SchemaStore)1 GenerationException (org.jsonschema2pojo.exception.GenerationException)1