Skip to content

Commit

Permalink
[eclipse-ee4j#616] Extract the common part of the Enum De / Serialize…
Browse files Browse the repository at this point in the history
…r and Adapter in tests

Signed-off-by: Anton Pinsky <[email protected]>
  • Loading branch information
api-from-the-ion committed Feb 12, 2024
1 parent ceabcf0 commit 49018c1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 63 deletions.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,20 @@

import java.lang.reflect.ParameterizedType;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

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

public class EnumWithJsonbPropertyAdapter<E extends Enum<E>> implements JsonbAdapter<E, String> {
private final Map<String, E> jsonToJavaMapping = new HashMap<>();
private final Map<String, E> jsonToJavaMapping;
private final EnumMap<E, String> javaToJsonMapping;

public EnumWithJsonbPropertyAdapter() {
super();

Class<E> enumType = getEnumType();
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);
});
EnumJsonbPropertyMaps<E> enumMappingMaps = new EnumJsonbPropertyMaps<>(getEnumType());
jsonToJavaMapping = enumMappingMaps.getJsonToJavaMapping();
javaToJsonMapping = enumMappingMaps.getJavaToJsonMapping();
}

private Class<E> getEnumType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import jakarta.json.bind.annotation.JsonbProperty;
import org.eclipse.yasson.adapters.model.EnumJsonbPropertyMaps;

import jakarta.json.bind.serializer.DeserializationContext;
import jakarta.json.bind.serializer.JsonbDeserializer;
import jakarta.json.stream.JsonParser;
Expand All @@ -32,23 +31,8 @@ public class EnumWithJsonbPropertyDeserializer<E extends Enum<E>> implements Jso
public EnumWithJsonbPropertyDeserializer() {
super();

Class<E> enumType = getEnumType();
jsonToJavaMapping = new HashMap<>();

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);
}
jsonToJavaMapping.put(asString, constant);
});
EnumJsonbPropertyMaps<E> enumMappingMaps = new EnumJsonbPropertyMaps<>(getEnumType());
jsonToJavaMapping = enumMappingMaps.getJsonToJavaMapping();
}

private Class<E> getEnumType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@

package org.eclipse.yasson.serializers.model;

import static java.util.Optional.ofNullable;

import java.lang.reflect.ParameterizedType;
import java.util.EnumMap;
import java.util.stream.Stream;

import jakarta.json.bind.annotation.JsonbProperty;
import org.eclipse.yasson.adapters.model.EnumJsonbPropertyMaps;

import jakarta.json.bind.serializer.JsonbSerializer;
import jakarta.json.bind.serializer.SerializationContext;
import jakarta.json.stream.JsonGenerator;
Expand All @@ -29,23 +27,8 @@ public class EnumWithJsonbPropertySerializer<E extends Enum<E>> implements Jsonb
public EnumWithJsonbPropertySerializer() {
super();

Class<E> enumType = getEnumType();
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);
});
EnumJsonbPropertyMaps<E> enumMappingMaps = new EnumJsonbPropertyMaps<>(getEnumType());
javaToJsonMapping = enumMappingMaps.getJavaToJsonMapping();
}

private Class<E> getEnumType() {
Expand Down

0 comments on commit 49018c1

Please sign in to comment.