| <!-- |
| /*************************************************************************************************************************** |
| * 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. |
| ***************************************************************************************************************************/ |
| --> |
| |
| Serializers |
| |
| <p> |
| One of the goals of Juneau was to make serialization as simple as possible. |
| In a single line of code, you should be able to serialize and parse most POJOs. |
| Despite this simplicity, Juneau provides lots of extensibility and configuration properties for tailoring how |
| POJOs are serialized and parsed. |
| </p> |
| <p> |
| The built-in serializers in Juneau are fast, efficient, and highly configurable. |
| They work by serializing POJOs directly to streams instead of using intermediate Document Object Model |
| objects. |
| </p> |
| <p> |
| In most cases, you can serialize objects in one line of code by using one of the default serializers: |
| </p> |
| <p class='bpcode w800'> |
| <jc>// A simple bean</jc> |
| <jk>public class</jk> Person { |
| <jk>public</jk> String <jf>name</jf> = <js>"John Smith"</js>; |
| <jk>public int</jk> <jf>age</jf> = 21; |
| } |
| |
| <jc>// Serialize to JSON, XML, or HTML</jc> |
| Person p = <jk>new</jk> Person(); |
| |
| <jc>// Produces: |
| // "{\"name\":\"John Smith\",\"age\":21}"</jc> |
| String json = JsonSerializer.<jsf>DEFAULT</jsf>.serialize(p); |
| |
| <jc>// Produces: |
| // "{name:'John Smith',age:21}"</jc> |
| String json = SimpleJsonSerializer.<jsf>DEFAULT</jsf>.serialize(p); |
| |
| <jc>// Produces: |
| // <object> |
| // <name>John Smith</name> |
| // <age>21</age> |
| // </object></jc> |
| String xml = XmlSerializer.<jsf>DEFAULT</jsf>.serialize(p); |
| |
| <jc>// Produces: |
| // <table> |
| // <tr><th>key</th><th>value</th></tr> |
| // <tr><td>name</td><td>John Smith</td></tr> |
| // <tr><td>age</td><td>21</td></tr> |
| // </table></jc> |
| String html = HtmlSerializer.<jsf>DEFAULT</jsf>.serialize(p); |
| |
| <jc>// Produces: |
| // "(name='John Smith',age=21)"</jc> |
| String uon = UonSerializer.<jsf>DEFAULT</jsf>.serialize(p); |
| |
| <jc>// Produces: |
| // "name='John+Smith'&age=21"</jc> |
| String urlencoding = UrlEncodingSerializer.<jsf>DEFAULT</jsf>.serialize(p); |
| |
| <jc>// Produces: |
| // 82 A4 6E 61 6D 65 AA 4A 6F 68 6E 20 53 6D 69 74 68 A3 61 67 65 15 </jc> |
| <jk>byte</jk>[] b = MsgPackSerializer.<jsf>DEFAULT</jsf>.serialize(p); |
| </p> |
| |
| <p> |
| In addition to the default serializers, customized serializers can be created using various built-in options: |
| </p> |
| |
| <p class='bpcode w800'> |
| <jc>// Use one of the default serializers to serialize a POJO</jc> |
| String json = JsonSerializer.<jsf>DEFAULT</jsf>.serialize(someObject); |
| |
| <jc>// Create a custom serializer for lax syntax using single quote characters</jc> |
| JsonSerializer serializer = JsonSerializer.<jsm>create</jsm>().simple().sq().build(); |
| |
| <jc>// Clone an existing serializer and modify it to use single-quotes</jc> |
| JsonSerializer serializer = JsonSerializer.<jsf>DEFAULT</jsf>.builder().sq().build(); |
| |
| <jc>// Serialize a POJO to JSON</jc> |
| String json = serializer.serialize(someObject); |
| </p> |
| <p> |
| Default serialization support is provided for Java primitives, <c>Maps</c>, <c>Collections</c>, |
| beans, and arrays. |
| Extensible support for other data types such as <c>Calendars</c>, <c>Dates</c>, |
| <c>Iterators</c> is available through the use of POJO swaps (described later). |
| </p> |
| <p> |
| The class hierarchy for the serializers (excluding specialized subclasses) are: |
| </p> |
| <ul class='javatree'> |
| <li class='jac'>{@link oaj.serializer.Serializer} |
| <ul> |
| <li class='jac'>{@link oaj.serializer.WriterSerializer} |
| <ul> |
| <li class='jc'>{@link oaj.json.JsonSerializer} |
| <ul> |
| <li class='jc'>{@link oaj.json.JsonSchemaSerializer} |
| <li class='jc'>{@link oaj.json.SimpleJsonSerializer} |
| </ul> |
| <li class='jc'>{@link oaj.plaintext.PlainTextSerializer} |
| <li class='jc'>{@link oaj.jena.RdfSerializer} |
| <ul> |
| <li class='jc'>{@link oaj.jena.N3Serializer} |
| <li class='jc'>{@link oaj.jena.NTripleSerializer} |
| <li class='jc'>{@link oaj.jena.RdfXmlSerializer} |
| <li class='jc'>{@link oaj.jena.RdfXmlAbbrevSerializer} |
| <li class='jc'>{@link oaj.jena.TurtleSerializer} |
| </ul> |
| <li class='jc'>{@link oaj.uon.UonSerializer} |
| <ul> |
| <li class='jc'>{@link oaj.oapi.OpenApiSerializer} |
| <li class='jc'>{@link oaj.urlencoding.UrlEncodingSerializer} |
| </ul> |
| <li class='jc'>{@link oaj.xml.XmlSerializer} |
| <ul> |
| <li class='jc'>{@link oaj.html.HtmlSerializer} |
| <ul> |
| <li class='jc'>{@link oaj.html.HtmlSchemaSerializer} |
| <li class='jc'>{@link oaj.html.HtmlStrippedDocSerializer} |
| <ul> |
| <li class='jc'>{@link oaj.html.HtmlDocSerializer} |
| <ul> |
| <li class='jc'>{@link oaj.html.HtmlSchemaDocSerializer} |
| </ul> |
| </ul> |
| </ul> |
| <li class='jc'>{@link oaj.soap.SoapXmlSerializer} |
| <li class='jc'>{@link oaj.xml.XmlDocSerializer} |
| <li class='jc'>{@link oaj.xmlschema.XmlSchemaSerializer} |
| <ul> |
| <li class='jc'>{@link oaj.xmlschema.XmlSchemaDocSerializer} |
| </ul> |
| </ul> |
| </ul> |
| <li class='jac'>{@link oaj.serializer.OutputStreamSerializer} |
| <ul> |
| <li class='jc'>{@link oaj.jso.JsoSerializer} |
| <li class='jc'>{@link oaj.msgpack.MsgPackSerializer} |
| </ul> |
| </ul> |
| </ul> |
| |