Defining a map with javaType keyword

1,932 views
Skip to first unread message

Poornima Soundararajan

unread,
Sep 21, 2012, 6:01:19 AM9/21/12
to jsonschema...@googlegroups.com
Hello,

I am trying to define a schema for the below JSON. The "properties" is basically a map. The key/value pairs and number of items can change.

{
    "name": "test-schema",
    "properties": {
        prop1": 5673.434,
        prop2": 234,
        prop3": false,
        prop4": "testing"
    }
}

I could not find a proper way to define this in json schema notation. Hence I tried to use the "javaType" keyword provided by jsonschema2pojo library.
testmap.json
{
    "id": "testmap",
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "properties": {
            "$ref": "propertyList.json"
        }
    }
}

propertyList.json
{
    "id": "propertyList",
    "type": "object",
    "javaType": "java.util.HashMap"
}
The resultant POJO gets created with HashMap as defined by javaType however compiler complains about type checking.

Can you please let me know if there is a way to specify the generic types for the Map ?

Also, please let me know if there is a better way to define the schema.


Thanks in advance
Poornima

Joe Littlejohn

unread,
Sep 22, 2012, 7:31:15 AM9/22/12
to jsonschema...@googlegroups.com

Hi Poornima,

Could you give more details on what exactly the compiler complains about? Is it giving warnings about the missing generic type information? I think your solution should work (at least compile).

Jackson can represent this kind of structure in Java either as a Map (like you have done) or as a Java bean that as an 'additional properties' map attached to it. When a value is encountered that has no explicit setter then Jackson puts the value into the 'additional properties' map.

If you remove the javaType property, you'll see that a new type is generated to represent the properties. This type has methods getAdditionalProperties and setAdditionalProperties, this is the map where all your property values will be held. This is an alternative to what you have already but not necessarily better. You can constrain what additional property values must look like by supplying an additionalProperties schema. Check the JSON schema spec for more details.

Cheers

poornima

unread,
Sep 24, 2012, 7:39:19 AM9/24/12
to jsonschema...@googlegroups.com
Thank you Joe for the quick reply.

You are right, compile warns me about the missing generic type information for the hashmap. Is there a way to mention the generic type for the hashmap in the json schema ?
Something like
    "javaType": "java.util.HashMap<String,Object>"

I did not know of additionalProperties functionality up till now. I will also evaluate the feasibility of using additionalProperties().

Thanks,
Poornima

Joe Littlejohn

unread,
Sep 24, 2012, 12:06:10 PM9/24/12
to jsonschema...@googlegroups.com
Unfortunately there's no way to supply this information in the 'javaType' property.

If you use additionalProperties you can supply a schema. This will result in generic types being added to the additionalProperties map. For example:

{
  "type" : "object",
  "additionalProperties" : {
    "type" : "integer"
  }
}

Will result in an additionalProperties map like this:

private Map<String,Integer> additionalProperties = new HashMap<String,Integer>

If you provide a more complex schema for additionalProperties, jsonschema2pojo will generate a new type and use this as the generic type value. You can even supply nest collections like:

{
  "type" : "object",
  "additionalProperties" : {
    "type" : "array"
    "items" : {
      "type" : "integer"
    }
  }
}

to produce:

private Map<String,List<Integer>> additionalProperties = new HashMap<String,List<Integer>>

bhalwank...@gmail.com

unread,
Aug 6, 2017, 9:02:30 AM8/6/17
to jsonschema2pojo-users
Hi,

Is the JavaType for Map available now? If not then can you please tell me how can a Map of Map<integer, string> be generated using additionalProperties

--
Many Thanks,
Nikhil

Joe Littlejohn

unread,
Aug 6, 2017, 9:04:41 AM8/6/17
to jsonschema...@googlegroups.com
Yes, you can use javaType to define a property with type java.util.Map, including generic type args. 

--
You received this message because you are subscribed to the Google Groups "jsonschema2pojo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/jsonschema2pojo-users.
For more options, visit https://groups.google.com/d/optout.

bhalwank...@gmail.com

unread,
Aug 6, 2017, 9:12:35 AM8/6/17
to jsonschema2pojo-users
Hi Joe,

Thanks for the update. Can you give me an example of it please? I am stuck up at this point in my code.

--
Many Thanks,
Nikhil

Joe Littlejohn

unread,
Aug 6, 2017, 9:21:21 AM8/6/17
to jsonschema...@googlegroups.com
Just use something like:

    "type": "object",
    "javaType": "java.util.Map<String, String>"

When you define your property.

To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsubscri...@googlegroups.com.

bhalwank...@gmail.com

unread,
Aug 6, 2017, 9:23:35 AM8/6/17
to jsonschema2pojo-users
Hi Joe,

Thanks. I will test it out. Also, can I generate Map<integer, List<Object>> in this case? Or only generic type args are supported?
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/jsonschema2pojo-users.
For more options, visit https://groups.google.com/d/optout.

Joe Littlejohn

unread,
Aug 6, 2017, 9:25:06 AM8/6/17
to jsonschema...@googlegroups.com
Yes, I think that will be fine (is recursive).

Hi Joe,

To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsubscri...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "jsonschema2pojo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsubscri...@googlegroups.com.

bhalwank...@gmail.com

unread,
Aug 6, 2017, 9:51:28 AM8/6/17
to jsonschema2pojo-users
Hi,

Thanks. I will try it out and let you know.

Hi Joe,

To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/jsonschema2pojo-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "jsonschema2pojo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/jsonschema2pojo-users.
For more options, visit https://groups.google.com/d/optout.

bhalwank...@gmail.com

unread,
Aug 8, 2017, 7:36:07 AM8/8/17
to jsonschema2pojo-users, bhalwank...@gmail.com
Hi Joe,

It worked. Thanks !
Reply all
Reply to author
Forward
0 new messages