blob: 6f014535c200ae35c89a474c9eda5724cd8c1426 [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 Details
<p>
Juneau supports converting arbitrary POJOs to and from JSON using ultra-efficient serializers and parsers.
The JSON serializer converts POJOs directly to JSON without the need for intermediate DOM objects using a
highly-efficient state machine.
Likewise, the JSON parser creates POJOs directly from JSON without the need for intermediate DOM objects.
</p>
<p>
The following example shows JSON for a typical bean:
</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>;
<ja>@Swap</ja>(TemporalCalendarSwap.IsoInstant.<jk>class</jk>) <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>
<h5 class='figure'>Sample Code</h5>
<p class='bpcode w800'>
Person p = <jk>new</jk> Person()
.name(<js>"John Smith"</js>)
.birthDate(<js>"1946-08-12T00:00:00Z"</js>)
.addresses(
<jk>new</jk> Address()
.street(<js>"100 Main Street"</js>)
.city(<js>"Anywhereville"</js>)
.state(<jsf>NY</jsf>)
.zip(12345)
.isCurrent(<jk>true</jk>);
);
</p>
<h5 class='figure'>Normal JSON</h5>
<p class='bpcode w800'>
{
<js>"name"</js>: <js>"John Smith"</js>,
<js>"birthDate"</js>: <js>"1946-08-12T00:00:00Z"</js>,
<js>"addresses"</js>: [
{
<js>"street"</js>: <js>"100 Main Street"</js>,
<js>"city"</js>: <js>"Anywhereville"</js>,
<js>"state"</js>: <js>"NY"</js>,
<js>"zip"</js>: 12345,
<js>"isCurrent"</js>: <jk>true</jk>
}
]
}
</p>
<h5 class='figure'>Simplified JSON</h5>
<p class='bpcode w800'>
{
name: <js>'John Smith'</js>,
birthDate: <js>'1946-08-12T00:00:00Z'</js>,
addresses: [
{
street: <js>'100 Main Street'</js>,
city: <js>'Anywhereville'</js>,
state: <js>'NY'</js>,
zip: 12345,
isCurrent: <jk>true</jk>
}
]
}
</p>