2

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?

StormeHawke
  • 5,987
  • 5
  • 45
  • 73

1 Answers1

0

It looks like you're using an old version of Jackson. Recommend you first try out Jackson2, with latest edition here: https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core/2.5.0

It looks like the newer json schema module may do the right thing for integers: https://github.com/FasterXML/jackson-module-jsonSchema/blob/master/src/main/java/com/fasterxml/jackson/module/jsonSchema/types/IntegerSchema.java

Zoe
  • 27,060
  • 21
  • 118
  • 148
Dan Halperin
  • 2,207
  • 1
  • 18
  • 25
  • The Jackson Schema module isn't working for generating schemas for classes with embedded enum types. It simply strips the enums out and leaves me with a simple String type. I filed a bug report. https://github.com/FasterXML/jackson-module-jsonSchema/issues/57 – StormeHawke Jan 14 '15 at 17:34
  • Also, http://stackoverflow.com/questions/27863689/generate-json-schema-from-pojo-with-a-twist?rq=1 – StormeHawke Jan 14 '15 at 17:47