Skip to content

Commit 7038260

Browse files
authored
Wrapping object and class name handling removed (#303)
Wrapping object option removed Class name handling removed Signed-off-by: David Kral <david.k.kral@oracle.com>
1 parent 3f6d7d4 commit 7038260

File tree

9 files changed

+121
-719
lines changed

9 files changed

+121
-719
lines changed

api/src/main/java/jakarta/json/bind/annotation/JsonbPolymorphicType.java

Lines changed: 0 additions & 151 deletions
This file was deleted.

api/src/main/java/jakarta/json/bind/annotation/JsonbSubtype.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,26 +16,22 @@
1616

1717
package jakarta.json.bind.annotation;
1818

19-
import java.lang.annotation.ElementType;
20-
import java.lang.annotation.Repeatable;
2119
import java.lang.annotation.Retention;
2220
import java.lang.annotation.RetentionPolicy;
2321
import java.lang.annotation.Target;
2422

2523
/**
26-
* Subtype is tightly bound to the {@link JsonbPolymorphicType}.
24+
* Subtype is tightly bound to the {@link JsonbTypeInfo}.
2725
* <br>
2826
* Type defines class which instance will be created when processing specific alias, or when processing
2927
* instance of the specified type, to determine which alias should be used.
3028
* <br>
3129
* Alias is used instead of a class name. It has to be unique value among all the defined subtypes
32-
* bound to the specific {@link JsonbPolymorphicType}. An exception should be thrown when processing and
30+
* bound to the specific {@link JsonbTypeInfo}. An exception should be thrown when processing and
3331
* validating aliases and duplicate alias is found.
34-
* <br>
35-
* An exception have to be thrown when processing unknown alias and
3632
* <pre><code>
3733
* // Example
38-
* {@literal @}JsonbPolymorphicType({
34+
* {@literal @}JsonbTypeInfo({
3935
* {@literal @}JsonbSubtype(alias = "dog", type = Dog.class)
4036
* {@literal @}JsonbSubtype(alias = "cat", type = Cat.class)
4137
* })
@@ -53,7 +49,7 @@
5349
*
5450
* jsonb.toJson(new Dog());// {"@type":"dog","isDog":true}
5551
* jsonb.toJson(new Cat());// {"@type":"cat","isCat":true}
56-
* jsonb.toJson(new Rat());// An exception thrown
52+
* jsonb.toJson(new Rat());// {"isRat":true}
5753
* </code></pre>
5854
*/
5955
@JsonbAnnotation
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package jakarta.json.bind.annotation;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* Configuration annotation of the type information handling.
26+
* <br>
27+
* This annotation is required on the most common parent of all classes when type information will be applied.
28+
* <pre><code>
29+
* // Example
30+
* {@literal @}JsonbTypeInfo(key = "@key")
31+
* interface Animal {}
32+
*
33+
* class Dog implements Animal {}
34+
* class Cat implements Animal {}
35+
* </code></pre>
36+
* This annotation is tightly bound to {@link JsonbSubtype}. It is recommended to use
37+
* {@link JsonbSubtype} annotations to specify all the possible classes and their aliases.
38+
*/
39+
@JsonbAnnotation
40+
@Retention(RetentionPolicy.RUNTIME)
41+
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})
42+
public @interface JsonbTypeInfo {
43+
44+
/**
45+
* Default type information key name.
46+
*/
47+
String DEFAULT_KEY_NAME = "@type";
48+
49+
/**
50+
* Key used for keeping the type information (alias).
51+
* Default value is {@code @type}.
52+
*
53+
* @return key name
54+
*/
55+
String key() default DEFAULT_KEY_NAME;
56+
57+
/**
58+
* Allowed aliases of the handled type.
59+
*
60+
* @return list of allowed aliases
61+
*/
62+
JsonbSubtype[] value() default {};
63+
64+
}

spec/src/main/asciidoc/jsonb.adoc

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -318,27 +318,22 @@ Deserialization into anonymous classes is not supported. Serialization of anonym
318318

319319
=== Polymorphic Types
320320

321-
Polymorphic type handling is supported for deserialization and serialization. Polymorphic handling is ensured by annotation `@JsonbPolymorphicType` and `@JsonbSubtype`. `@JsonbPolymorphicType` defines how the overall polymorphic information strategy will be serialized/deserialized and defines all the supported aliases using `@JsonbSubtype` annotations. `@JsonbSubtype` ensures proper and safe mapping between class alias and type. Implementation must validate mapped types if they are assignable from the annotated type. If not, an exception must be thrown.
321+
Polymorphic type handling is supported for deserialization and serialization. Polymorphic handling is ensured by annotation `JsonbTypeInfo` and `@JsonbSubtype`. `JsonbTypeInfo` defines key name of the property to store type information in it and defines all the supported aliases using `@JsonbSubtype` annotations. `@JsonbSubtype` ensures proper and safe mapping between class alias and type. Implementation must validate mapped types if they are assignable from the annotated type. If not, an exception must be thrown.
322322

323-
Polymorphic information is obtained from `@JsonbSubtype` annotation as a type alias mapped to the type or if `classNames` property of the `@JsonbPolymorphicType` is enabled, it corresponds to the fully qualified class name, if no suitable alias is found. Implementation must ensure to support `allowedPackages` option of the `@JsonbPolymorphicType` which is only used when `classNames` option is enabled, it is ignored otherwise. It is not allowed to create instances of the classes outside the allowed packages. In such cases an exception must be thrown. It is also required to have `allowedPackages` option set if `classNames` option is enabled. If the `allowedPackages` is null or empty an exception must be thrown.
323+
Type information is obtained from `@JsonbSubtype` annotation as a type alias mapped to the type. If no matching class is found for obtained alias during deserialization, an exception must be thrown.
324324

325-
Available serialization formats:
325+
New property with type information is added to the serialized object. The property key name is taken from the `key` property of the annotation `JsonbTypeInfo`. This type information property key name has to be unique in the resulting JSON document. If any naming collision with class or any other `JsonbTypeInfo` properties occurs, an exception must be thrown. It is required for all polymorphism fields to be serialized as the first properties in the JSON and any actual object properties are serialized after.
326326

327-
* Property - new property with polymorphic information is added to the serialized object. The property key name is taken from the `key` property of the annotation `@JsonbPolymorphicType`. This polymorphic information property key name has to be unique in the resulting JSON document. If any naming collision with class properties occurs, an exception must be thrown. It is required for all polymorphism fields to be serialized as the first properties in the JSON and any actual object properties are serialized after.
328-
* Wrapping object - serialized object is wrapped with another object with a single property. The key corresponds to the polymorphic information of the type and the value is the serialized object
327+
If no `JsonbTypeInfo` is used on handled class or its predecessors, it is not possible to ensure proper polymorphic type handling and in such cases deserialization is not supported.
329328

330-
If no `@JsonbPolymorphicType` is used on handled class or its predecessors, it is not possible to ensure proper polymorphic handling and in such cases deserialization is not supported for polymorphic types.
331-
332-
If the type predecessor has set `@JsonbPolymorphicType` and the current type also, but with the different format, an exception must be thrown.
333-
334-
If there are multiple different polymorphic customizations that need to be merged, an exception must be thrown. For example, if class has a predecessor with `@JsonbPolymorphicType` and one of its interfaces also has `@JsonbPolymorphicType` set, or two or more interfaces do have `@JsonbPolymorphicType` set, then an exception must be thrown.
329+
If there are multiple different type polymorphic customizations that need to be merged, an exception must be thrown. Multiple inheritance of this customization is not supported.
335330

336331
[source,java]
337332
----
338-
@JsonbPolymorphicType({...})
333+
@JsonbTypeInfo({...})
339334
interface Vehicle {}
340335
341-
@JsonbPolymorphicType({...})
336+
@JsonbTypeInfo({...})
342337
interface Machine {}
343338
344339
class Car implements Vehicle, Machine {}
@@ -347,15 +342,15 @@ class Car implements Vehicle, Machine {}
347342
In case of the following example:
348343
[source,java]
349344
----
350-
@JsonbPolymorphicType(key = "@vehicle", value = {@JsonbSubtype(alias = "car", type = Car.class)})
345+
@JsonbTypeInfo(key = "@vehicle", value = {@JsonbSubtype(alias = "car", type = Car.class)})
351346
class Vehicle {...}
352347
353-
@JsonbPolymorphicType(key = "@car", value = {@JsonbSubtype(alias = "myCar", type = MyCar.class)})
348+
@JsonbTypeInfo(key = "@car", value = {@JsonbSubtype(alias = "myCar", type = MyCar.class)})
354349
class Car extends Vehicle {...}
355350
356351
class MyCar extends Car {...}
357352
----
358-
No exception is thrown since there is just one parent `@JsonbPolymorphicType` and one on the currently handled type. The order of the polymorphic information properties must be the same in which they appear in the polymorphic definition chain. Resulting JSON when serializing MyCar class would look like this:
353+
The order of the type information properties must be the same in which they appear in the polymorphic type chain. Resulting JSON when serializing MyCar class would look like this:
359354
[source,json]
360355
----
361356
{

0 commit comments

Comments
 (0)