forked from eclipse-ee4j/yasson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eclipse-ee4j#616] Extract the common part of the Enum De / Serialize…
…r and Adapter in tests Signed-off-by: Anton Pinsky <[email protected]>
- Loading branch information
1 parent
ceabcf0
commit 49018c1
Showing
4 changed files
with
69 additions
and
63 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/test/java/org/eclipse/yasson/adapters/model/EnumJsonbPropertyMaps.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,57 @@ | ||
/* | ||
* Copyright (c) 2024 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.adapters.model; | ||
|
||
import static java.util.Optional.ofNullable; | ||
|
||
import java.util.EnumMap; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import jakarta.json.bind.annotation.JsonbProperty; | ||
|
||
public class EnumJsonbPropertyMaps<E extends Enum<E>> { | ||
private final Map<String, E> jsonToJavaMapping = new HashMap<>(); | ||
private final EnumMap<E, String> javaToJsonMapping; | ||
|
||
public EnumJsonbPropertyMaps(Class<E> enumType) { | ||
super(); | ||
|
||
javaToJsonMapping = new EnumMap<>(enumType); | ||
|
||
Stream.of(enumType.getEnumConstants()).forEach(constant -> { | ||
final String asString; | ||
try { | ||
asString = ofNullable( | ||
constant.getClass() | ||
.getDeclaredField(constant.name()) | ||
.getAnnotation(JsonbProperty.class)) | ||
.map(JsonbProperty::value) | ||
.orElseGet(constant::name); | ||
} catch (final NoSuchFieldException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
javaToJsonMapping.put(constant, asString); | ||
jsonToJavaMapping.put(asString, constant); | ||
}); | ||
} | ||
|
||
public EnumMap<E, String> getJavaToJsonMapping() { | ||
return javaToJsonMapping; | ||
} | ||
|
||
public Map<String, E> getJsonToJavaMapping() { | ||
return jsonToJavaMapping; | ||
} | ||
} |
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