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#617]User defined deserializer always should be used bef…
…ore standard ones
- Loading branch information
1 parent
f371eb5
commit 444d929
Showing
7 changed files
with
158 additions
and
8 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
15 changes: 15 additions & 0 deletions
15
src/test/java/org/eclipse/yasson/serializers/model/Colors.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,15 @@ | ||
package org.eclipse.yasson.serializers.model; | ||
|
||
import jakarta.json.bind.annotation.JsonbProperty; | ||
import jakarta.json.bind.annotation.JsonbTypeDeserializer; | ||
import jakarta.json.bind.annotation.JsonbTypeSerializer; | ||
|
||
@JsonbTypeSerializer(ColorsSerializer.class) | ||
@JsonbTypeDeserializer(ColorsDeserializer.class) | ||
public enum Colors { | ||
@JsonbProperty("Red") | ||
RED, | ||
@JsonbProperty("Green") | ||
GREEN | ||
} | ||
|
8 changes: 8 additions & 0 deletions
8
src/test/java/org/eclipse/yasson/serializers/model/ColorsDeserializer.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,8 @@ | ||
package org.eclipse.yasson.serializers.model; | ||
|
||
public class ColorsDeserializer extends EnumWithJsonbPropertyDeserializer<Colors> { | ||
|
||
public ColorsDeserializer() { | ||
super(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/test/java/org/eclipse/yasson/serializers/model/ColorsSerializer.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,7 @@ | ||
package org.eclipse.yasson.serializers.model; | ||
|
||
public class ColorsSerializer extends EnumWithJsonbPropertySerializer<Colors> { | ||
public ColorsSerializer() { | ||
super(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/test/java/org/eclipse/yasson/serializers/model/EnumWithJsonbPropertyDeserializer.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 @@ | ||
package org.eclipse.yasson.serializers.model; | ||
|
||
import static java.util.Optional.ofNullable; | ||
|
||
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 jakarta.json.bind.serializer.DeserializationContext; | ||
import jakarta.json.bind.serializer.JsonbDeserializer; | ||
import jakarta.json.stream.JsonParser; | ||
|
||
public class EnumWithJsonbPropertyDeserializer<E extends Enum<E>> implements JsonbDeserializer<E> { | ||
|
||
private final Map<String, E> jsonToJavaMapping; | ||
|
||
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); | ||
}); | ||
} | ||
|
||
private Class<E> getEnumType() { | ||
return Class.class.cast(ParameterizedType.class.cast( | ||
getClass().getGenericSuperclass()) | ||
.getActualTypeArguments()[0]); | ||
} | ||
|
||
@Override | ||
public E deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { | ||
String key = parser.getString(); | ||
|
||
assert key != null; | ||
|
||
return ofNullable(jsonToJavaMapping.get(key)) | ||
.orElseThrow(() -> new IllegalArgumentException("Unknown enum value: '" + key + "'")); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/org/eclipse/yasson/serializers/model/EnumWithJsonbPropertySerializer.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,48 @@ | ||
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 jakarta.json.bind.serializer.JsonbSerializer; | ||
import jakarta.json.bind.serializer.SerializationContext; | ||
import jakarta.json.stream.JsonGenerator; | ||
|
||
public class EnumWithJsonbPropertySerializer<E extends Enum<E>> implements JsonbSerializer<E> { | ||
private final EnumMap<E, String> javaToJsonMapping; | ||
|
||
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); | ||
}); | ||
} | ||
|
||
private Class<E> getEnumType() { | ||
return Class.class.cast(ParameterizedType.class.cast( | ||
getClass().getGenericSuperclass()) | ||
.getActualTypeArguments()[0]); | ||
} | ||
|
||
@Override public void serialize(E obj, JsonGenerator generator, SerializationContext ctx) { | ||
generator.write(javaToJsonMapping.get(obj)); | ||
} | ||
} |