eclipse-ee4j / eclipse-ee4j/yasson
Yasson requires its own/internal JSON-P implementation to work ... ???
- Dominant language
- Java
- Stars
- 218
- Forks
- 109
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 9
Description
I am implementing polymorphism mechanism with JSON-B, similar to Jackson one.
So I need to scan for a "@type" attribute and then deserialize again with the correct class.
```java
public class IdentifiableSerializer implements JsonbDeserializer {
private final Reflections reflections;
public IdentifiableSerializer(Reflections reflections) {
this.reflections = reflections;
}
@Override
public FindCriteriaDTO deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
JsonObject preview = ctx.deserialize(JsonObject.class, parser);
if (preview.keySet().contains("@type")){
Optional> type = reflections.getSubTypesOf(FindCriteriaDTO.class).stream().filter(e -> StringUtils.equals(preview.getString("@type"), e.getSimpleName())).findFirst();
if (type.isPresent()){
StringWriter stringWriter = new StringWriter();
Json.createWriter(stringWriter).writeObject(preview);
JsonParser newParser = Json.createParser(new StringReader(stringWriter.toString()));
FindCriteriaDTO deserialize = ctx.deserialize(type.get(), newParser);
return deserialize;
}
}
return ctx.deserialize(FindCriteriaDTO.class, parser);
}
```
If I create a new JsonParser calling the JSON-P method a different implementation is returned (and this should not be an issue! json-b and json-p are different specifications!). But then I see this exception :
org.glassfish.json.JsonParserImpl cannot be cast to org.eclipse.yasson.internal.JsonbParser
This casting is the reason (org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer:58) :
```java
@Override
public final T deserialize(JsonParser parser, DeserializationContext context, Type rtType) {
Unmarshaller ctx = (Unmarshaller) context;
ctx.setCurrent(this);
deserializeInternal((JsonbParser) parser, ctx);
ctx.setCurrent(getWrapper());
return getInstance((Unmarshaller) context);
}
```
I don't think this is the proper behavior.
Am I wrong ?
Contributor guide
Assessment
This issue has not been assessed yet.