|
| 1 | +/* |
| 2 | + * This program and the accompanying materials are made available under the |
| 3 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 4 | + * http://www.eclipse.org/legal/epl-2.0, |
| 5 | + * or the Eclipse Distribution License v. 1.0 which is available at |
| 6 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 9 | + */ |
| 10 | + |
| 11 | +package org.eclipse.yasson.jsonstructure; |
| 12 | + |
| 13 | +import jakarta.json.Json; |
| 14 | +import jakarta.json.JsonObject; |
| 15 | +import jakarta.json.JsonString; |
| 16 | +import jakarta.json.JsonValue; |
| 17 | +import jakarta.json.bind.annotation.JsonbSubtype; |
| 18 | +import jakarta.json.bind.annotation.JsonbTypeDeserializer; |
| 19 | +import jakarta.json.bind.annotation.JsonbTypeInfo; |
| 20 | +import jakarta.json.bind.serializer.DeserializationContext; |
| 21 | +import jakarta.json.bind.serializer.JsonbDeserializer; |
| 22 | +import jakarta.json.stream.JsonParser; |
| 23 | +import java.lang.reflect.Type; |
| 24 | +import java.util.Collections; |
| 25 | + |
| 26 | +public class Issue673 { |
| 27 | + |
| 28 | + public static interface Referenceable { |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + public static class Reference implements Referenceable { |
| 33 | + |
| 34 | + private String description; |
| 35 | + |
| 36 | + public String getDescription() { |
| 37 | + return description; |
| 38 | + } |
| 39 | + |
| 40 | + public void setDescription(String description) { |
| 41 | + this.description = description; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public static class IRIReference implements Referenceable { |
| 46 | + |
| 47 | + private String value; |
| 48 | + |
| 49 | + public IRIReference() {} |
| 50 | + |
| 51 | + public IRIReference(String value) { |
| 52 | + this.value = value; |
| 53 | + } |
| 54 | + |
| 55 | + public String getValue() { |
| 56 | + return value; |
| 57 | + } |
| 58 | + |
| 59 | + public void setValue(String value) { |
| 60 | + this.value = value; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @JsonbTypeInfo(key = "type", value = { |
| 65 | + @JsonbSubtype(alias = Location.TYPE, |
| 66 | + type = Location.class) |
| 67 | + }) |
| 68 | + public static interface LocationInterface { |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + public static class Location implements LocationInterface { |
| 73 | + |
| 74 | + public final static String TYPE = "Location"; |
| 75 | + |
| 76 | + private Referenceable referenceable; |
| 77 | + |
| 78 | + @JsonbTypeDeserializer(ReferenceableDeserializer.class) |
| 79 | + public Referenceable getReference() { |
| 80 | + return referenceable; |
| 81 | + } |
| 82 | + |
| 83 | + public void setReference(Referenceable referenceable) { |
| 84 | + this.referenceable = referenceable; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public static class ReferenceableDeserializer implements JsonbDeserializer<Referenceable> { |
| 89 | + |
| 90 | + @Override |
| 91 | + public Referenceable deserialize(JsonParser jp, DeserializationContext dc, Type type) { |
| 92 | + final JsonValue v = jp.getValue(); |
| 93 | + if (v instanceof JsonString str) { |
| 94 | + return new IRIReference(str.getString()); |
| 95 | + } |
| 96 | + if (v instanceof JsonObject obj) { |
| 97 | + dc.deserialize(Reference.class, |
| 98 | + Json.createParserFactory(Collections.EMPTY_MAP) |
| 99 | + .createParser(obj)); |
| 100 | + } |
| 101 | + return null; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments