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

Add support for primitive types via the javaType property #41

Closed
joelittlejohn opened this issue Jun 23, 2013 · 2 comments
Closed

Add support for primitive types via the javaType property #41

joelittlejohn opened this issue Jun 23, 2013 · 2 comments

Comments

@joelittlejohn
Copy link
Owner

Original author: [email protected] (February 24, 2012 20:57:23)

I want to generate some pojos with 64bit integer (i.e. long) fields. I can't use type=number (java type Double) because of the loss of precision.

Refer to this json schema:

{
"type":"object",
"properties":{
"prop1":{"type":"object", "javaType":"java.lang.Long"},
"prop2":{"type":"object", "javaType":"long"},
"prop3":{"type":"integer", "javaType":"java.lang.Long"},
"prop4":{"type":"integer", "javaType":"long"}
}
}

prop1 works as expected (but is limited to non-primitive).

prop2 works partly. The generated pojo is as expected, but a "long.java" class is also generated that I have to manually delete. This is because jsonschema2pojo uses ClassLoader.loadClass to check for existing classes, but this method doesn't work for primitives. A common way to handle primitive class lookup is to just make your own Map<String,Class> lookup. See http://stackoverflow.com/questions/180097/dynamically-find-the-class-that-represents-a-primitive-java-type

prop3 and prop4 don't work (the pojo has int/Integer) because javaType is only used for type:object. But I wish they would work because my field is really a (64-bit) integer, not an object.

Original issue: http://code.google.com/p/jsonschema2pojo/issues/detail?id=41

@joelittlejohn
Copy link
Owner Author

From [email protected] on February 24, 2012 22:03:35
Christopher, thanks for taking the time to raise this.

I think you're definitely right to identify a gap here. Integers as described by JSON Schema aren't limited to 32-bits (like Java ints), but for most people int is an efficient and aesthetically pleasing type to use. I think we should support those who need wider integer types though.

I'll definitely add support for primitives via javaType (probably using the Map<String,Class> approach as you suggest). For the occasional wide integer, "javaType":"long" would be very useful. I can see that some people might find this tedious if it has to be repeated for all their properties though so I think adding a new configurable property would also be a good idea.

Something along the lines of:

useLongIntegers (default false)

Which will cause all "type":"integer" properties to have a the Java primitive type "long" or "Long" (depending on the usePrimitives property value).

@joelittlejohn
Copy link
Owner Author

From [email protected] on April 18, 2012 22:59:22
Fixed.

javaType now supports primitives (rcd225332b328), and there is also a new config option 'useLongIntegers' to universally switch all Integers to Longs in generated POJOs (r0685bcf9474a).

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

No branches or pull requests

1 participant