Json schema to pojo java

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc

License

joelittlejohn/jsonschema2pojo

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Читайте также:  Delete new lines python

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Signed-off-by: Michael Edgar

Git stats

Files

Failed to load latest commit information.

README.md

jsonschema2pojo generates Java types from JSON Schema (or example JSON) and can annotate those types for data-binding with Jackson 2.x or Gson.

You can use jsonschema2pojo as a Maven plugin, an Ant task, a command line utility, a Gradle plugin or embedded within your own Java app. The Getting Started guide will show you how.

A very simple Maven example:

plugin> groupId>org.jsonschema2pojogroupId> artifactId>jsonschema2pojo-maven-pluginartifactId> version>1.2.1version> configuration> sourceDirectory>$/src/main/resources/schemasourceDirectory> targetPackage>com.example.typestargetPackage> configuration> executions> execution> goals> goal>generategoal> goals> execution> executions> plugin>

A very simple Gradle example:

plugins < id "java" id "org.jsonschema2pojo" version "1.2.1" > repositories < mavenCentral() >dependencies < implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.2' > jsonSchema2Pojo < targetPackage = 'com.example' >
  • unkish
  • Thach Hoang
  • Dan Cruver
  • Ben Manes
  • Sam Duke
  • Duane Zamrok
  • Christian Trimble
  • YourKit, who support this project through a free license for the YourKit Java Profiler.

About

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc

Источник

Generate Plain Old Java Objects (POJOs) from JSON Schema Definitions with Gradle

Featured image for sharing metadata for article

Table of Contents

It’s possible that you’re using JSON Schema as a means for describing and validating your data is formatted correctly.

If you’re on a Java project, it may be that you’re currently manually translating from JSON Schema types to Plain Old Java Objects (POJOs), which is time consuming and has the risk of drifting between your definitions and implementations.

One of the benefits of using a very structured format like JSON Schema is that you can programmatically generate the contents of your Java models, or any other languages you may be using, for that matter.

Example JSON Schema

As an example, we’ll use the JSON Schema noted on the jsonschema2pojo website:

This will be stored in the location src/main/resources/schema/data.json .

Generating the POJOs

There are then two options for how we want to generate the POJOs.

In both cases, we receive the following Java objects:

jsonschema2pojo-gradle-plugin

The official jsonschema2pojo project has a Gradle plugin that can be used.

As noted in Add to Gradle Plugin Portal, it’s likely that you’ve not seen this before, as it’s not on the Gradle plugin portal, so it means we need to use the legacy plugin configuration using our buildscript :

  When compiling the project, for instance as part of a build, it'll produce the objects into the targetDirectory , which should then be consumable by your project.

js2d-gradle

  When compiling the project, for instance as part of a build, it'll produce the objects into a location that can be consumed by your project.

While writing this article, I spotted an issue in the docs so until that’s merged, watch out!

How to generate POJOs really quickly and easily, with no manual work, using the Gradle jsonschema2pojo Plugin.

Jamie Tanna

Written by Jamie Tanna on Mon, 29 Nov 2021 09:23:37 UTC , and last updated on Mon, 14 Feb 2022 11:24:33 UTC .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

Has this content helped you? Did it solve that difficult-to-resolve issue you’ve been chasing for weeks? Or has it taught you something new you’ll be able to re-use daily?

Please consider supporting me so I can continue to create content like this!

This post was filed under articles.

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.

Want to get in touch? Drop me an email at hi@jamietanna.co.uk. I try to get back to everything, if I don’t reply in a couple of weeks, feel free to give me a nudge!

You can also check out a list of all the places /elsewhere you can find me.

© 2023 Jamie Tanna. Post content and code snippets licensing is detailed on respective pages, otherwise default to Creative Commons Attribution Non Commercial Share Alike 4.0 International and Apache License 2.0.
Site design released under the MIT License, based on Hack.css.
Proudly hosted on AWS using S3 + CloudFront, deployed by GitLab CI, and generated by d64e772843 on Mon, 14 Feb 2022 11:24:33 +0000..

Источник

Convert JSON-Schema to Java POJO

computer working

computer working

One of our previous journals showcases how you can easily convert JSON data to Java POJO. In case you have missed that journal then we would suggest you to have a look at “Easy way to convert JSON to Java POJO”. In this journal, we will be taking the journey of converting JSON-Schema to Java POJO. First though, we need a valid JSON Schema document. If you don’t have one to hand, you can use the Address example found here:

There are currently Six ways to achieving this target.

  • Online Version: You can use this online utility JSONSchema2Java. Copy/Paste your valid JSON-Schema in the box provided and change the settings as per your requirements. Once you are done with the settings click on the Preview button to have a look at how your POJO will look like. After satisfaction of the preview, you can go ahead and download the POJO for use in your projects.
  • as a maven plugin using jsonschema2pojo-maven-plugin
  • via the command line using jsonschema2pojo-cli
  • as an ant task using jsonschema2pojo-ant
  • as a gradle plugin using gradle-jsonschema2pojo-plugin
  • directly from your code (embedded) using jsonschema2pojo-core

#Maven Plugin

The Maven plugin provides a generate goal that attaches to the generate-sources phase by default.

For a quick example, place the address schema into src/main/resources/schema and edit your pom.xml to include the following in the section:

  org.jsonschema2pojo jsonschema2pojo-maven-plugin 0.4.37 $/src/main/resources/schema com.example.types    generate      

The generated types target Java 6, so if you haven’t already then you’ll need to set your source/target version to 1.6 or higher by adding the following to the section of your pom:

 org.apache.maven.plugins maven-compiler-plugin 1.6 1.6   

At present the generated types depend on Commons Lang for equals , hashCode and toString . Some schema constructs will also cause parser hints in the form of Jackson annotations. To compile your generated types, you’ll need to make the necessary additions to your :

 commons-lang commons-lang 2.4  com.fasterxml.jackson.core jackson-databind 2.5.4  

Finally, on running mvn package you’ll see a newly generated file target/java-gen/com/example/types/Address.java . You should also notice that the com.example.types.Address class has been compiled and included in your project bundle.

#Command Line

If you’re using OSX and brew, try brew install jsonschema2pojo . If not, then start by downloading the latest release from:

Extract the release, you should see some jsonschema2pojo jar files, a lib folder and two scripts (bash and bat).

We’ll use the example address schema to generate our types. Download the address schema, and invoke jsonschema2pojo like:

jsonschema2pojo —source address —target java-gen

For the full usage details, use the —help option:

#Ant Task

To invoke the jsonschema2pojo Ant task, you’ll need to download the latest task itself, and some dependencies:

Either place the downloaded jars in $ANT_HOME/lib , or place them in a folder of your choosing.

Now simply add a the taskdef task to your build.xml to make the jsonschema2pojo task available. Once the taskdef has been added, you can invoke the task. E.g.

#Within your Java project (embedded)

To use the jsonschema2pojo API directly from a Java application you’ll need to add the jsonschema2pojo-core jar to your build path. You can obtain this by downloading the latest jar or by adding the following dependency to your Maven project:

 org.jsonschema2pojo jsonschema2pojo-core 0.4.37  

If you’re not using Maven, you’ll need to gather some further dependencies:

Now you’re ready to use the jsonschema2pojo schema mapper in your code:

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"));

Источник

Оцените статью