Skip to content

Commit

Permalink
JsonbTypeAdapter is now allowed on the creator paramters (#291)
Browse files Browse the repository at this point in the history
Signed-off-by: David Kral <david.k.kral@oracle.com>
  • Loading branch information
Verdent committed Nov 12, 2021
1 parent 2035373 commit 21809d7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -32,13 +32,14 @@
* <li> type </li>
* <li> field </li>
* <li> method </li>
* <li> parameter </li>
* </ul>
*
* @since JSON Binding 1.0
*/
@JsonbAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
public @interface JsonbTypeAdapter {

/**
Expand Down
4 changes: 3 additions & 1 deletion spec/src/main/asciidoc/jsonb.adoc
Expand Up @@ -706,6 +706,8 @@ There are two ways how to register `JsonbAdapter`:

`JsonbAdapter` registered via `JsonbConfig::withAdapters` is visible to all serialize/deserialize operations performed with given `JsonbConfig`. `JsonbAdapter` registered with annotation is visible to serialize/deserialize operation used only for annotated field.

It is possible to annotate `JsonbCreator` parameter with `JsonbTypeAdapter` and provide adapter for a parameter this way. However, if `JsonbTypeAdapter` annotation is provided to any other parameter (such as setter method parameter) it will be ignored.

Implementations must provide a CDI support in adapters to allow injection of CDI managed beans into it.

==== Serializers/Deserializers
Expand All @@ -722,7 +724,7 @@ There are two ways how to register `JsonbSerializer/JsonbDeserializer`:
. Using `JsonbConfig::withSerializers/ JsonbConfig::withDeserializers` method;
. Annotating a type with `JsonbSerializer/JsonbDeserializer` annotation.

It is possible to annotate `JsonbCreator` parameters with `JsonbTypeDeserializer` and provide deserializer for a parameter this way. However, if `JsonbTypeDeserializer` annotation is provided to any other parameter (such as setter method parameter) it will be ignored.
It is possible to annotate `JsonbCreator` parameter with `JsonbTypeDeserializer` and provide deserializer for a parameter this way. However, if `JsonbTypeDeserializer` annotation is provided to any other parameter (such as setter method parameter) it will be ignored.

Implementations must provide a CDI support in serializers/deserializers to allow injection of CDI managed beans into it.

Expand Down
Expand Up @@ -21,6 +21,7 @@
package jakarta.json.bind.tck.customizedmapping.instantiation;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.lang.invoke.MethodHandles;
Expand All @@ -36,6 +37,7 @@
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.JsonbException;
import jakarta.json.bind.tck.customizedmapping.instantiation.model.CreatorPlusFactoryContainer;
import jakarta.json.bind.tck.customizedmapping.instantiation.model.CreatorWithAdapterContainer;
import jakarta.json.bind.tck.customizedmapping.instantiation.model.CreatorWithDeserializerContainer;
import jakarta.json.bind.tck.customizedmapping.instantiation.model.IllegalInstanceFactoryCreatorContainer;
import jakarta.json.bind.tck.customizedmapping.instantiation.model.MultipleCreatorsContainer;
Expand Down Expand Up @@ -248,4 +250,14 @@ public void testJsonbDeserializerOnCreatorParameter() {
String expected = "Test String Deserialized";
assertEquals("JsonbDeserializer on the JsonbCreator parameter was not executed." , expected, c.getStringInstance());
}

@Test
public void testJsonbAdapterOnCreatorParameter() {
CreatorWithAdapterContainer c = jsonb.fromJson("{ \"instance\" : \"string value\" }",
CreatorWithAdapterContainer.class);
String expected = "string value";
assertNotNull("JsonbAdapter on the JsonbCreator parameter was not executed.", c.getStringWrapper());
assertEquals("JsonbAdapter on the JsonbCreator parameter was not executed.",
expected, c.getStringWrapper().getWrapped());
}
}
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package jakarta.json.bind.tck.customizedmapping.instantiation.model;

import jakarta.json.bind.adapter.JsonbAdapter;
import jakarta.json.bind.annotation.JsonbCreator;
import jakarta.json.bind.annotation.JsonbProperty;
import jakarta.json.bind.annotation.JsonbTypeAdapter;

public class CreatorWithAdapterContainer {

private final StringWrapper stringWrapper;

@JsonbCreator
public CreatorWithAdapterContainer(@JsonbProperty("instance")
@JsonbTypeAdapter(SimpleStringAdapter.class) StringWrapper stringWrapper) {
this.stringWrapper = stringWrapper;
}

public StringWrapper getStringWrapper() {
return stringWrapper;
}

public static class StringWrapper {

private final String wrapped;

private StringWrapper(String wrapped) {
this.wrapped = wrapped;
}

public String getWrapped() {
return wrapped;
}

}

public static class SimpleStringAdapter implements JsonbAdapter<StringWrapper, String> {

@Override
public String adaptToJson(StringWrapper obj) {
return obj.getWrapped();
}

@Override
public StringWrapper adaptFromJson(String obj) {
return new StringWrapper(obj);
}
}

}

0 comments on commit 21809d7

Please sign in to comment.