Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsonschema and date type

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?

like image 352
hhg Avatar asked Oct 05 '15 15:10

hhg


People also ask

What is the JSON Schema format for date and time?

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.

What are the types of values supported by JSON Schema?

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.

What is JSON-Schema?

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.

Can I use custom formats in JSON Schema validation?

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.


2 Answers

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.

like image 107
Jason Desrosiers Avatar answered Sep 28 '22 09:09

Jason Desrosiers


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

like image 41
Mav55 Avatar answered Sep 28 '22 07:09

Mav55