Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support YAML syntax for JSON Schema #778

Closed
flemming-n-larsen opened this issue Sep 9, 2017 · 13 comments
Closed

Support YAML syntax for JSON Schema #778

flemming-n-larsen opened this issue Sep 9, 2017 · 13 comments
Milestone

Comments

@flemming-n-larsen
Copy link

I really love the jsonschema2pojo tool. :-)

It would be even greater, if you add support for YAML which uses the JSON Schema for defining Java POJOs.

So instead of using JSON for defining a "BotState" (in JSON Schema for format) with bot-state.json:

{
	"id": "#bot-state.json",
	"$schema": "http://json-schema.org/draft-04/schema#",
	"description": "Current state of a bot without an id. The id must be hidden for enemy bots",

	"javaType": "BotState",
	"properties": {
		"energy": {
			"description": "Energy level",
			"type": "number"
		},
		"position": {
			"description": "Position (x, y)",
			"$ref": "../types/point.json"
		},
		"direction": {
			"description": "Driving direction in degrees",
			"$ref": "../types/angle.json"
		},
		"gun-direction": {
			"description": "Gun direction in degrees",
			"$ref": "../types/angle.json"
		},
		"radar-direction": {
			"description": "Radar direction in degrees",
			"$ref": "../types/angle.json"
		},
		"radar-spread-angle": {
			"description": "Radar spread angle in degrees",
			"$ref": "../types/angle.json"
		},
		"speed": {
			"description": "Speed measured in units per turn",
			"type": "number"
		},
		"scan-field": {
			"description": "Scan field, where a positive arc angle means that scan is moving left and negative arc angle means that the scan is moving right",
			"$ref": "../types/scan-field.json"
		}
	},
	"required": [
		"energy",
		"position",
		"direction",
		"gun-direction",
		"radar-direction",
		"speed",
		"scan-field"
	]
}

... I can use YAML instead (bot-state.yaml):

---
id: "#bot-state.yaml"
"$schema": http://json-schema.org/draft-04/schema#
description: Current state of a bot without an id. The id must be hidden for enemy
  bots
javaType: BotState
properties:
  energy:
    description: Energy level
    type: number
  position:
    description: Position (x, y)
    "$ref": "../types/point.yaml"
  direction:
    description: Driving direction in degrees
    "$ref": "../types/angle.yaml"
  gun-direction:
    description: Gun direction in degrees
    "$ref": "../types/angle.yaml"
  radar-direction:
    description: Radar direction in degrees
    "$ref": "../types/angle.yaml"
  radar-spread-angle:
    description: Radar spread angle in degrees
    "$ref": "../types/angle.yaml"
  speed:
    description: Speed measured in units per turn
    type: number
  scan-field:
    description: Scan field, where a positive arc angle means that scan is moving
      left and negative arc angle means that the scan is moving right
    "$ref": "../types/scan-field.yaml"
required:
- energy
- position
- direction
- gun-direction
- radar-direction
- speed
- scan-field
@joelittlejohn
Copy link
Owner

Hi. It would be really simple to convert the YAML to json before sending it to jsonschema2pojo. Check out e.g. https://github.com/wildducktheories/y2j

@flemming-n-larsen
Copy link
Author

You are right. But the trick is to specify my model in YAML with JSON Schema syntax and then generate the POJOs in one single step using Maven. Using your cool tool, I am able to take JSON Schemas (in JSON) and generate the POJOs using the Maven plugin in one step.
I should like to avoid using external tools beside Maven, and also an additional (manual) step, where YAML files are converted to JSON files.
If I could find a Maven plugin, that takes my YAML files and convert these to JSON files, then I can use your Maven plugin for generating the POJOs, i.e. 2 steps. :-) However, I have not been able to find such plugin, and I would still be nice, if jsonschema2pojo could do it in one single step. ;-D

@joelittlejohn
Copy link
Owner

It's a shame Maven makes adding extra ad-hoc steps like this so annoying to do.

I don't see any problem in adding yaml and yamlschema source types, which correspond (after conversion) to the current json and jsonschema source types. This would probably also be a useful addition to jsonschema2pojo.org.

@joelittlejohn
Copy link
Owner

Are you actually planning to bind YAML data to your types, or are you using YAML to represent JSON Schema... to specify JSON data?

The good news is that I think Jackson's YAML support would be compatible with all the Jackson annotations that we're currently generating.

@flemming-n-larsen
Copy link
Author

I am considering to use YAML instead of JSON to represent JSON Schemas (easier to read). I use JSON Schema for defining all classes/entities/objects in my domain model. So when I need concrete Java objects (POJOs), I just generate these based on the JSON Schemas (YAML or JSON).
Moreover, I should like to specify my communication protocol between a server and clients using JSON Schemas, and let it be optional, if JSON or YAML is used as syntax, when sending messages. I want to support both formats, so it is up to the clients to choose, if YAML or JSON will be used. The server should support both formats. But JSON Schema will be defining the message structures etc.

I love Jackson. So I really appreciate the addition of the YAML support to Jackson. 👍

@flemming-n-larsen
Copy link
Author

It reminds me of Swagger/Open API, where it is possible to define and document a REST specification in either YAML or JSON. I believe that some schema (JSON Schema?) is being used for defining the Swagger/OpenAPI format.

@joelittlejohn joelittlejohn modified the milestones: 0.4.38, 0.5.0 Sep 11, 2017
@joelittlejohn
Copy link
Owner

I'm implementing this for the next release (0.5.0). You'll be able to choose a source type of 'yaml' (example yaml that you need to bind) or yamlschema (a JSON Schema represented as YAML). I think using these to generate types then Jackson's YAML support for actual data binding works nicely.

@joelittlejohn
Copy link
Owner

Thanks again for raising this btw, I think a lot of people will find this useful.

@flemming-n-larsen
Copy link
Author

That is really great news! I am sure a lot of people will appreciate this feature. :-)

As soon as you want it tested, don't hesitate with writing back and I will change the JSON stuff into YAML to see how it goes. :-)

@clipod
Copy link

clipod commented Oct 14, 2017

I had something like this in my mind but I see there is a drive going on to support YAML. Will be waiting for the feature.

@joelittlejohn
Copy link
Owner

@clipod it is implemented and released already.

@clipod
Copy link

clipod commented Oct 15, 2017

@joelittlejohn Thanks. I will test it out.

wigbam pushed a commit to wigbam/jsonschema2pojo that referenced this issue Nov 25, 2017
@HavenLin
Copy link

HavenLin commented Mar 14, 2018

do you guys have using YAML to present JSON schema sample guide?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants