| <!-- |
| /*************************************************************************************************************************** |
| * 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. |
| ***************************************************************************************************************************/ |
| --> |
| |
| Bean Subtypes |
| |
| <p> |
| In addition to the bean type name support described above, simplified support is provided |
| for bean subtypes. |
| </p> |
| <p> |
| Bean subtypes are similar in concept to bean type names, except for the following differences: |
| </p> |
| <ul> |
| <li>You specify the list of possible subclasses through an annotation on a parent bean class. |
| <li>You do not need to register the subtype classes on the bean dictionary of the parser. |
| </ul> |
| <p> |
| In the following example, the abstract class has two subclasses: |
| </p> |
| <p class='bpcode w800'> |
| <jc>// Abstract superclass</jc> |
| <ja>@Bean</ja>( |
| dictionary={A1.<jk>class</jk>, A2.<jk>class</jk>} |
| ) |
| <jk>public abstract class</jk> A { |
| <jk>public</jk> String <jf>f0</jf> = <js>"f0"</js>; |
| } |
| |
| <jc>// Subclass 1</jc> |
| <ja>@Bean</ja>(typeName=<js>"A1"</js>) |
| <jk>public class</jk> A1 <jk>extends</jk> A { |
| <jk>public</jk> String <jf>f1</jf>; |
| } |
| |
| <jc>// Subclass 2</jc> |
| <ja>@Bean</ja>(typeName=<js>"A2"</js>) |
| <jk>public class</jk> A2 <jk>extends</jk> A { |
| <jk>public</jk> String <jf>f2</jf>; |
| } |
| </p> |
| <p> |
| When serialized, the subtype is serialized as a virtual <js>"_type"</js> property: |
| </p> |
| <p class='bpcode w800'> |
| JsonSerializer s = SimpleJsonSerializer.<jsf>DEFAULT</jsf>; |
| |
| A1 a1 = <jk>new</jk> A1(); |
| a1.<jf>f1</jf> = <js>"f1"</js>; |
| String r = s.serialize(a1); |
| <jsm>assertEquals</jsm>(<js>"{_type:'A1',f1:'f1',f0:'f0'}"</js>, r); |
| </p> |
| <p> |
| The following shows what happens when parsing back into the original object. |
| </p> |
| <p class='bpcode w800'> |
| JsonParser p = JsonParser.<jsf>DEFAULT</jsf>; |
| A a = p.parse(r, A.<jk>class</jk>); |
| <jsm>assertTrue</jsm>(a <jk>instanceof</jk> A1); |
| </p> |