Jackson ObjectMapper treats “long” java types as “number” instead of “integer”

1,521 views
Skip to first unread message

Brian Trezise

unread,
Jan 13, 2015, 4:37:30 PM1/13/15
to jackso...@googlegroups.com

My previous attempt to post this seems to have failed so trying again.

Free points on StackOverflow if you answer this :)

http://stackoverflow.com/questions/27929514/jackson-objectmapper-treats-long-java-types-as-number-instead-of-integer

The question is below:

Using org.codehaus.jackson.map.ObjectMapper (NOT the databind version!) I'm generating a json schema from the following object:

public class MyModelObject {    
    private long fileSize;
    //... other properties, getters, setters etc
}

Using the following code:

ObjectMapper mapper = new ObjectMapper();
JsonSchema schema = mapper.generateJsonSchema(clazz);
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);

I get the following schema (stripped down to the relevant part):

{
  "type" : "object",
  "properties" : {
    "fileSize" : {
      "type" : "number"
    }
  }
}

"number" is not the best representation of a long, as when I elsewhere try to generate pojos FROM the schema, I get "double" rather than "long" (or even "int" would be ok).

According to this, there is an "integer" data type in json that would suit my needs much better:http://spacetelescope.github.io/understanding-json-schema/reference/numeric.html

But digging through the source code of the jackson-mapper module, I found that they're using a standard serializer called LongSerializer contained in this class: org.codehaus.jackson.map.ser.StdSerializers

The relevant part is this method:

@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint)
{
    return createSchemaNode("number", true);
}

Long story short: Is it possible to override the standard serializers, or do I need to go in and hack my generated output?


Tatu Saloranta

unread,
Jan 14, 2015, 2:13:28 PM1/14/15
to jackso...@googlegroups.com
Ok, first things first: ObjectMapper JSON Schema generation functionality will be deprecated, so the supported method is to use JSON Schema module at:

https://github.com/FasterXML/jackson-module-jsonSchema

so if you could verify whether things work better with it, and a recent Jackson version (2.4.4 or newer), that'd be good. in-mapper implementation has not been maintained and I need to add deprecation warnings there to steer users away from it.

-+ Tatu +-


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

Reply all
Reply to author
Forward
0 new messages