blob: 375ff4cce04cd5252ed676e660fb1a6f16254f1f [file] [log] [blame]
<!--
/***************************************************************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
***************************************************************************************************************************/
-->
JSON-Schema Support
<p>
Juneau provides the {@link oaj.json.JsonSchemaSerializer} class for generating JSON-Schema
documents that describe the output generated by the {@link oaj.json.JsonSerializer} class.
This class shares the same properties as <c>JsonSerializer</c>.
For convenience the {@link oaj.json.JsonSerializer#getSchemaSerializer()} method has been
added for creating instances of schema serializers from the regular serializer instance.
</p>
<h5 class='figure'>Sample Beans</h5>
<p class='bpcode w800'>
<jk>public class</jk> Person {
<jc>// Bean properties</jc>
<jk>public</jk> String <jf>name</jf>;
<jk>public</jk> Calendar <jf>birthDate</jf>;
<jk>public</jk> List&lt;Address&gt; <jf>addresses</jf>;
<jc>// Getters/setters omitted</jc>
}
<jk>public class</jk> Address {
<jc>// Bean properties</jc>
<jk>public</jk> String <jf>street</jf>, <jf>city</jf>;
<jk>public</jk> StateEnum <jf>state</jf>;
<jk>public int</jk> <jf>zip</jf>;
<jk>public boolean</jk> <jf>isCurrent</jf>;
<jc>// Getters/setters omitted</jc>
}
</p>
<p>
The code for creating our POJO model and generating JSON-Schema is shown below:
</p>
<p class='bpcode w800'>
<jc>// Get the one of the default schema serializers.</jc>
JsonSchemaSerializer s = JsonSchemaSerializer.<jsf>DEFAULT_SIMPLE_READABLE</jsf>;
<jc>// Get the JSON Schema for the POJO.</jc>
String jsonSchema = s.serialize(<jk>new</jk> Person());
<jc>// This also works.</jc>
jsonSchema = s.serialize(Person.<jk>class</jk>);
</p>
<h5 class='figure'>JSON Schema</h5>
<p class='bpcode w800'>
{
<jok>type: <jov>'object'</jov>,
<jok>description</jok>: <jov>'org.apache.juneau.sample.Person'</jov>,
<jok>properties</jok>: {
<jok>name</jok>: {
<jok>type</jok>: <jov>'string'</jov>,
<jok>description</jok>: <jov>'java.lang.String'</jov>
},
<jok>birthDate</jok>: {
<jok>type</jok>: <jov>'string'</jov>,
<jok>description</jok>: <jov>'java.util.Calendar'</jov>
},
<jok>addresses</jok>: {
<jok>type</jok>: <jov>'array'</jov>,
<jok>description</jok>: <jov>'java.util.LinkedList&lt;org.apache.juneau.sample.Address&gt;'</jov>,
<jok>items</jok>: {
<jok>type</jok>: <jov>'object'</jov>,
<jok>description</jok>: <jov>'org.apache.juneau.sample.Address'</jov>,
<jok>properties</jok>: {
<jok>street</jok>: {
<jok>type</jok>: <jov>'string'</jov>,
<jok>description</jok>: <jov>'java.lang.String'</jov>
},
<jok>city</jok>: {
<jok>type</jok>: <jov>'string'</jov>,
<jok>description</jok>: <jov>'java.lang.String'</jov>
},
<jok>state</jok>: {
<jok>type</jok>: <jov>'string'</jov>,
<jok>description</jok>: <jov>'java.lang.String'</jov>
},
<jok>zip</jok>: {
<jok>type</jok>: <jov>'number'</jov>,
<jok>description</jok>: <jov>'int'</jov>
},
<jok>isCurrent</jok>: {
<jok>type</jok>: <jov>'boolean'</jov>,
<jok>description</jok>: <jov>'boolean'</jov>
}
}
}
}
}
}
</p>