-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for records added Signed-off-by: David Kral <david.k.kral@oracle.com>
- Loading branch information
Showing
9 changed files
with
252 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/org/eclipse/yasson/internal/ClassMultiReleaseExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
package org.eclipse.yasson.internal; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Method; | ||
|
||
import org.eclipse.yasson.internal.model.JsonbCreator; | ||
|
||
/** | ||
* Search for instance creator from other sources. | ||
* Mainly intended to add extensibility for different java versions and new features. | ||
*/ | ||
class ClassMultiReleaseExtension { | ||
|
||
private ClassMultiReleaseExtension() { | ||
throw new IllegalStateException("This class cannot be instantiated"); | ||
} | ||
|
||
static boolean shouldTransformToPropertyName(Method method) { | ||
return true; | ||
} | ||
|
||
static boolean isGetAccessorMethod(Method method) { | ||
return false; | ||
} | ||
|
||
static JsonbCreator findJsonbCreator(Class<?> clazz, Constructor<?>[] declaredConstructors, AnnotationIntrospector introspector) { | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java16/org/eclipse/yasson/internal/ClassMultiReleaseExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
package org.eclipse.yasson.internal; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Method; | ||
import java.util.Set; | ||
|
||
import org.eclipse.yasson.internal.model.JsonbCreator; | ||
|
||
/** | ||
* Search for instance creator from other sources. | ||
* Mainly intended to add extensibility for different java versions and new features. | ||
*/ | ||
class ClassMultiReleaseExtension { | ||
|
||
private static final Set<String> NOT_VALID_ACCESSOR_METHODS = Set.of("equals", "hashCode", "toString"); | ||
|
||
private ClassMultiReleaseExtension() { | ||
throw new IllegalStateException("This class cannot be instantiated"); | ||
} | ||
|
||
static boolean shouldTransformToPropertyName(Method method) { | ||
return !method.getDeclaringClass().isRecord(); | ||
} | ||
|
||
static boolean isGetAccessorMethod(Method method) { | ||
if (method.getDeclaringClass().isRecord()) { | ||
return !NOT_VALID_ACCESSOR_METHODS.contains(method.getName()); | ||
} | ||
return false; | ||
} | ||
|
||
static JsonbCreator findJsonbCreator(Class<?> clazz, Constructor<?>[] declaredConstructors, AnnotationIntrospector introspector) { | ||
if (clazz.isRecord()) { | ||
return introspector.createJsonbCreator(declaredConstructors[0], null, clazz); | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* 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, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
package org.eclipse.yasson.records; | ||
|
||
import jakarta.json.bind.annotation.JsonbProperty; | ||
|
||
public record Car(@JsonbProperty("typeChanged") String type, @JsonbProperty("colorChanged") String color) { | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java16/org/eclipse/yasson/records/CarWithoutAnnotations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* 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, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
package org.eclipse.yasson.records; | ||
|
||
public record CarWithoutAnnotations(String type, String color) { | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java16/org/eclipse/yasson/records/RecordTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
package org.eclipse.yasson.records; | ||
|
||
import org.eclipse.yasson.Jsonbs; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class RecordTest { | ||
|
||
@Test | ||
public void testRecordProcessing() { | ||
Car car = new Car("skoda", "green"); | ||
String expected = "{\"colorChanged\":\"green\",\"typeChanged\":\"skoda\"}"; | ||
|
||
String json = Jsonbs.defaultJsonb.toJson(car); | ||
assertEquals(expected, json); | ||
Car deserialized = Jsonbs.defaultJsonb.fromJson(expected, Car.class); | ||
assertEquals(car, deserialized); | ||
} | ||
|
||
@Test | ||
public void testRecordProcessingWithoutJsonbProperties() { | ||
CarWithoutAnnotations car = new CarWithoutAnnotations("skoda", "green"); | ||
String expected = "{\"color\":\"green\",\"type\":\"skoda\"}"; | ||
|
||
String json = Jsonbs.defaultJsonb.toJson(car); | ||
assertEquals(expected, json); | ||
CarWithoutAnnotations deserialized = Jsonbs.defaultJsonb.fromJson(expected, CarWithoutAnnotations.class); | ||
assertEquals(car, deserialized); | ||
} | ||
|
||
} |