I'm just getting started with jsonschema and an example under "Using jsonschema2pojo within your Java project (embedded)" in https://github.com/joelittlejohn/jsonschema2pojo/wiki/Getting-Started
having in mind data types of jsonschema listed here https://developers.google.com/discovery/v1/type-format?hl=en
my schema object can be described as
{
"$schema": "http://json-schema.org/draft-04/schema",
"description": "Document",
"type": "object",
"properties": {
"displayDate": { "type": "date" },
"displayName": { "type": "string" }
}
}
unfortunately a generated Pojo object will be
package com.example;
public interface Document {
java.lang.Object getDisplayDate();
void setDisplayDate(java.lang.Object arg0);
java.lang.String getDisplayName();
void setDisplayName(java.lang.String arg0);
}
has a member "displayDate" of a type Object instead of expected Date. Why?
The following is the list of formats specified in the JSON Schema specification. Dates and times are represented in RFC 3339, section 5.6. This is a subset of the date format also commonly known as ISO8601 format. "date-time": Date and time together, for example, 2018-11-13T20:20:39+00:00.
For now we concentrate on the specification for values. There are five basic value types supported by JSON Schema: One of the most basic forms of values that appear in JSON documents are strings of text. In JSON schema we can specify that a document is a string by using the keyword string as the value of the name type.
JSON - Schema 1 Describes your existing data format. 2 Clear, human- and machine-readable documentation. 3 Complete structural validation, useful for automated testing. 4 Complete structural validation, validating client-submitted data.
However, custom formats may also be used, as long as the parties exchanging the JSON documents also exchange information about the custom format types. A JSON Schema validator will ignore any format type that it does not understand.
date
is not a valid value for type
. displayDate
should be defined as
{ "type": "string", "format": "date" }
I don't know if jsonschema2pojo will convert that to a Date object like you want, but it seems that it is defaulting to Object instead of throwing an error when it encounters the invalid value for type
.
According to the latest jsonschema2pojo docs, for type Date
, you need to do the following:-
{ "type": "string", "format": "date-time" }
In the generated POJO, the property will be of type Date
object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With