https://github.com/itzg/jsonschema2pojo-rules-bettermaps
Custom RuleFactory for the jsonschema2pojo facility that identifies additionalProperties that only declare a simple type and generates a Map without an intermediate property POJO.
https://github.com/itzg/jsonschema2pojo-rules-bettermaps
java json-schema maven-plugin
Last synced: about 1 month ago
JSON representation
Custom RuleFactory for the jsonschema2pojo facility that identifies additionalProperties that only declare a simple type and generates a Map without an intermediate property POJO.
- Host: GitHub
- URL: https://github.com/itzg/jsonschema2pojo-rules-bettermaps
- Owner: itzg
- License: apache-2.0
- Created: 2017-03-21T14:17:10.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-21T15:46:57.000Z (about 8 years ago)
- Last Synced: 2025-03-25T08:38:29.512Z (2 months ago)
- Topics: java, json-schema, maven-plugin
- Language: Java
- Size: 25.4 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
This is a custom `RuleFactory` for the [jsonschema2pojo facility](https://github.com/joelittlejohn/jsonschema2pojo)
that identifies `additionalProperties` that only declare a simple type and generates a
`Map` where `TYPE` is the boxed type of the property, such as `String`, or `Integer`.To use it with the Maven plugin, add this artifact as a dependency of the plugin and set
the `customRuleFactory` to `me.itzg.jsonschema2pojo.bettermaps.BetterMapsRuleFactory`.For example,
```xml
org.jsonschema2pojo
jsonschema2pojo-maven-plugin
${jsonschema2pojo.version}
${basedir}/src/main/resources/schema
me.itzg.jsonschema2pojo.bettermaps.BetterMapsRuleFactory
generate
me.itzg
jsonschema2pojo-rules-bettermaps
1.0-SNAPSHOT
```
## Example
Given the following schema,
```json
{
"type":"object",
"properties": {
"foo": {
"type": "string"
},
"bars": {
"type": "object",
"additionalProperties": {
"type":"string"
}
},
"times": {
"type": "object",
"additionalProperties": {
"type":"integer"
}
},
"baz": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
}
}
}
}
}
```The root class, called `MyClass` in this example, contains direct `Map` fields for `bars` and `times`
without the need to reference or declare additional POJO types. The field `baz` still generates
a POJO that contains fields `key` and `value` since it itself is a complex structure.```java
public class MyClass {@JsonProperty("foo")
private String foo;
@JsonProperty("bars")
private Map bars;
@JsonProperty("times")
private Map times;
@JsonProperty("baz")
private Baz baz;
// ...snip
}public class Baz {
@JsonIgnore
private Map additionalProperties =
new HashMap();
// ...snip
}public class BazProperty {
@JsonProperty("key")
private String key;
@JsonProperty("value")
private String value;
// ...snip
}
```The `Baz` and `BazProperty` effectively demonstrate the benefit of this rule factory.