jsonschema和日期类型

时间:2015-10-05 15:24:14

标签: java json pojo jsonschema

我刚刚开始使用jsonschema和一个例子 "在Java项目中使用jsonschema2pojo(嵌入式)" 在 https://github.com/joelittlejohn/jsonschema2pojo/wiki/Getting-Started

考虑到此处列出的jsonschema数据类型 https://developers.google.com/discovery/v1/type-format?hl=en

我的架构对象可以描述为

{    
    "$schema": "http://json-schema.org/draft-04/schema",
    "description": "Document",
    "type": "object",

    "properties": {
        "displayDate": { "type": "date" },
        "displayName": { "type": "string" }
    }
}

不幸的是,生成的Pojo对象将是

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);

}

有一个成员" displayDate"类型为Object而不是预期Date。为什么呢?

2 个答案:

答案 0 :(得分:2)

date不是type的有效值。 displayDate应定义为

{ "type": "string", "format": "date" }

我不知道jsonschema2pojo是否会将其转换为您想要的Date对象,但它似乎默认为Object而不是在遇到type的无效值时抛出错误。

答案 1 :(得分:1)

根据最新的jsonschema2pojo文档,对于类型getX(),您需要执行以下操作:-

Date

在生成的POJO中,属性的类型为{ "type": "string", "format": "date-time" } 对象