BigInteger support in jsonschema2pojo

874 views
Skip to first unread message

gopikrishn...@gmail.com

unread,
Apr 17, 2014, 8:41:06 AM4/17/14
to jsonschema...@googlegroups.com
Hi Joe,

I want to generate a property whose type should be java.math.BigInteger. I've tried setting "type":"BigInteger" and "javaType":"java.math.BigInteger", but still the property is being generated as of type Object. Am I doing something wrong ?

{
     "properties":{
        "number": {
            "type": "BigInteger",
            "javaType": "java.math.BigInteger"
        }
    }
}

public class MyObject {
    ...
    private Object number;
    ...
}

Joe Littlejohn

unread,
Apr 17, 2014, 9:48:16 AM4/17/14
to jsonschema...@googlegroups.com
If you want to use BigIntegers for all your integer types you may be interested in this enhancement request: https://github.com/joelittlejohn/jsonschema2pojo/issues/161

If you just want this to apply to one property then you should be able to achieve what you want using:

{
     "properties":{
        "number": {
            "type": "object",
            "javaType": "java.math.BigInteger"
        }
    }
}

The reason you are currently seeing java.lang.Object is that the rule "type": "BigInteger" is not understood as "BigInteger" is not a valid json schema type. When the type value is not understood, the system falls back to java.lang.Object.

Cheers


--
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-...@googlegroups.com.
Visit this group at http://groups.google.com/group/jsonschema2pojo-users.
For more options, visit https://groups.google.com/d/optout.

GopiKrishnan Ganesan

unread,
Apr 17, 2014, 12:36:30 PM4/17/14
to jsonschema...@googlegroups.com
Huge thanks for the quick response, Joe. It worked like a charm !!!


--
You received this message because you are subscribed to a topic in the Google Groups "jsonschema2pojo-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jsonschema2pojo-users/EUSuOI5Ymxg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jsonschema2pojo-...@googlegroups.com.

Andrew Todd

unread,
Apr 18, 2014, 11:47:11 AM4/18/14
to jsonschema...@googlegroups.com
On Thu, Apr 17, 2014 at 9:48 AM, Joe Littlejohn <joelit...@gmail.com> wrote:
If you just want this to apply to one property then you should be able to achieve what you want using:

{
     "properties":{
        "number": {
            "type": "object",
            "javaType": "java.math.BigInteger"
        }
    }
}

Joe,

This is valid JSON Schema, but it will consider numbers to be invalid.

If I go to https://json-schema-validator.herokuapp.com/ and paste in the schema above, and then attempt to pass this as the data:

{
    "number": 5
}

it will fail, quite correctly.

So this is not a solution.

Joe Littlejohn

unread,
Apr 19, 2014, 1:24:12 PM4/19/14
to jsonschema...@googlegroups.com

Andrew, many people use schemas as a simple way to create Java types. They don't use the schemas for validation and don't much care if semantics of the schema are correct.

Yes, the suggestion above is definitely a hack. A workaround that achieves a Java type structure that isn't currently supported. The real solution here is GitHub #161.

--

Andrew Todd

unread,
Apr 21, 2014, 10:15:07 AM4/21/14
to jsonschema...@googlegroups.com
On Sat, Apr 19, 2014 at 1:24 PM, Joe Littlejohn <joelit...@gmail.com> wrote:

Andrew, many people use schemas as a simple way to create Java types. They don't use the schemas for validation and don't much care if semantics of the schema are correct.

Why would you do that when you could just write a Java class? I fail to see what a JSON Schema is, if not a description of document structure.
 

Joe Littlejohn

unread,
Apr 21, 2014, 5:00:20 PM4/21/14
to jsonschema...@googlegroups.com

Why would you do that when you could just write a Java class?

My guess is that it has something to do with the verbosity of Java.

Tatu Saloranta

unread,
Apr 21, 2014, 5:45:43 PM4/21/14
to jsonschema...@googlegroups.com
On Mon, Apr 21, 2014 at 2:00 PM, Joe Littlejohn <joelit...@gmail.com> wrote:

Why would you do that when you could just write a Java class?

My guess is that it has something to do with the verbosity of Java.

As opposed to "brevity" of JSON Schema? :-D

I think one common reason is that similar to WSDL, it is seen that providing a schema for others to use for generating things is more flexible than providing classes or lib in specific language.
It is bit of win-some-lose-some with JSON Schema, since its support for OOD concepts is limited. But I have seen adoption for this approach.

-+ Tatu +-


 
Reply all
Reply to author
Forward
0 new messages