1

I need to map a java.util.Map instance into a JSON-schema that is used by org.jsonschema2pojo maven plugin to create a POJO.

I didn't find a good and simple solution for this.

Could someone help me please?

This is my actual json-schema file

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Response",
    "description": "A Response object",
    "type": "object",
    "properties": {
        "result": {
            "type": "string",
            "description": "describes response status"
        },
        "msg": {
            "type": "string",
            "description": "user msgs"
        }
    },
    "required": ["result"],
    "additionalProperties": false
}

I need to add a field "errors" that is converted into a java.util.Map<String, String> in Java.

Andrew Lygin
  • 6,077
  • 1
  • 32
  • 37
Fabrizio P
  • 241
  • 3
  • 13
  • Could you please declare more about your question? From my understanding, you have a JSON schema as described, and you have a map probably looks like: "errors":{"type":"string"}, then you want to add the map into your JSON schema file and use it in jsonschema2pojo, right? If I have any misunderstanding please give some example, thank you! – Cong Wang Sep 28 '16 at 08:23

1 Answers1

0

AFAIK additionalProperties does the job. You can declare an errors property of type Map<String, Object> for example like this (this is yaml now):

...
properties:
  errors:
    type: object
    additionalProperties:
      type: object

You don't specify the type of the keys, since this describes a json document, which naturally has strings as keys on objects.

instead of type: object you can also do type: string for Map<String, String> or reference another definition if you have your own type as values in that map.

hagbard
  • 695
  • 3
  • 13
  • Hi! Thanks for reply. I found and tried additionalProperties but I can't change the name and then into the generated pojo, the field "additionalProperties" is mapped with @JsonIgnore and I don't want it. – Fabrizio P Aug 24 '16 at 14:28
  • Hm, maybe a quirk in the code generator. What name do you mean? Can you paste an excerpt of what you tried and what got generated from it? – hagbard Aug 24 '16 at 14:51