Skip to content

Commit 3d192d0

Browse files
committed
issue-673 fix
Signed-off-by: redmitry <[email protected]>
1 parent a2cfd89 commit 3d192d0

File tree

4 files changed

+198
-2
lines changed

4 files changed

+198
-2
lines changed

src/main/java/org/eclipse/yasson/internal/deserializer/YassonParser.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
3-
*
3+
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
4+
*
45
* This program and the accompanying materials are made available under the
56
* terms of the Eclipse Public License v. 2.0 which is available at
67
* http://www.eclipse.org/legal/epl-2.0,
@@ -86,6 +87,11 @@ public Event next() {
8687
return next;
8788
}
8889

90+
@Override
91+
public Event currentEvent() {
92+
return context.getLastValueEvent();
93+
}
94+
8995
@Override
9096
public String getString() {
9197
return delegate.getString();

src/main/java/org/eclipse/yasson/internal/jsonstructure/JsonStructureToParserAdapter.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
34
*
45
* This program and the accompanying materials are made available under the
56
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -78,6 +79,11 @@ public Event next() {
7879
return next;
7980
}
8081

82+
@Override
83+
public Event currentEvent() {
84+
return iterators.peek().getValueEvent(getValue());
85+
}
86+
8187
@Override
8288
public String getString() {
8389
return iterators.peek().getString();
@@ -103,6 +109,11 @@ public BigDecimal getBigDecimal() {
103109
return getJsonNumberValue().bigDecimalValue();
104110
}
105111

112+
@Override
113+
public JsonValue getValue() {
114+
return iterators.peek().getValue();
115+
}
116+
106117
@Override
107118
public JsonObject getObject() {
108119
JsonStructureIterator current = iterators.peek();
@@ -115,6 +126,17 @@ public JsonObject getObject() {
115126
}
116127
}
117128

129+
@Override
130+
public JsonArray getArray() {
131+
JsonStructureIterator current = iterators.peek();
132+
if (current instanceof JsonArrayIterator) {
133+
iterators.pop();
134+
return getValue().asJsonArray();
135+
} else {
136+
throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR, "Outside of array context"));
137+
}
138+
}
139+
118140
private JsonNumber getJsonNumberValue() {
119141
JsonStructureIterator iterator = iterators.peek();
120142
JsonValue value = iterator.getValue();
@@ -123,7 +145,7 @@ private JsonNumber getJsonNumberValue() {
123145
}
124146
return (JsonNumber) value;
125147
}
126-
148+
127149
@Override
128150
public JsonLocation getLocation() {
129151
throw new JsonbException("Operation not supported");
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright (c) 2026 Contributors to the Eclipse Foundation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
package org.eclipse.yasson.jsonstructure;
14+
15+
import jakarta.json.Json;
16+
import jakarta.json.JsonArray;
17+
import jakarta.json.JsonObject;
18+
import jakarta.json.JsonString;
19+
import jakarta.json.JsonValue;
20+
import jakarta.json.bind.annotation.JsonbSubtype;
21+
import jakarta.json.bind.annotation.JsonbTypeDeserializer;
22+
import jakarta.json.bind.annotation.JsonbTypeInfo;
23+
import jakarta.json.bind.serializer.DeserializationContext;
24+
import jakarta.json.bind.serializer.JsonbDeserializer;
25+
import jakarta.json.stream.JsonParser;
26+
import java.lang.reflect.Type;
27+
import java.util.Collections;
28+
import java.util.stream.Collectors;
29+
30+
public class Issue673 {
31+
32+
public static interface Referenceable {
33+
34+
}
35+
36+
public static class Reference implements Referenceable {
37+
38+
private String description;
39+
40+
public String getDescription() {
41+
return description;
42+
}
43+
44+
public void setDescription(String description) {
45+
this.description = description;
46+
}
47+
}
48+
49+
public static class IRIReference implements Referenceable {
50+
51+
private String value;
52+
53+
public IRIReference() {}
54+
55+
public IRIReference(String value) {
56+
this.value = value;
57+
}
58+
59+
public String getValue() {
60+
return value;
61+
}
62+
63+
public void setValue(String value) {
64+
this.value = value;
65+
}
66+
}
67+
68+
@JsonbTypeInfo(key = "type", value = {
69+
@JsonbSubtype(alias = Location.TYPE,
70+
type = Location.class)
71+
})
72+
public static interface LocationInterface {
73+
74+
}
75+
76+
public static class Location implements LocationInterface {
77+
78+
public final static String TYPE = "Location";
79+
80+
private String tags;
81+
private Referenceable referenceable;
82+
83+
@JsonbTypeDeserializer(TagsDeserializer.class)
84+
public String getTags() {
85+
return tags;
86+
}
87+
88+
public void setTags(String tags) {
89+
this.tags = tags;
90+
}
91+
92+
@JsonbTypeDeserializer(ReferenceableDeserializer.class)
93+
public Referenceable getReference() {
94+
return referenceable;
95+
}
96+
97+
public void setReference(Referenceable referenceable) {
98+
this.referenceable = referenceable;
99+
}
100+
}
101+
102+
public static class TagsDeserializer implements JsonbDeserializer<String> {
103+
@Override
104+
public String deserialize(JsonParser jp, DeserializationContext dc, Type type) {
105+
final JsonValue v = jp.getArray();
106+
if (v instanceof JsonArray arr) {
107+
return arr.stream()
108+
.filter(JsonString.class::isInstance)
109+
.map(JsonString.class::cast)
110+
.map(JsonString::getString)
111+
.collect(Collectors.joining(", "));
112+
}
113+
return null;
114+
}
115+
116+
}
117+
118+
public static class ReferenceableDeserializer implements JsonbDeserializer<Referenceable> {
119+
120+
@Override
121+
public Referenceable deserialize(JsonParser jp, DeserializationContext dc, Type type) {
122+
final JsonValue v = jp.getValue();
123+
if (v instanceof JsonString str) {
124+
return new IRIReference(str.getString());
125+
}
126+
if (v instanceof JsonObject obj) {
127+
dc.deserialize(Reference.class,
128+
Json.createParserFactory(Collections.EMPTY_MAP)
129+
.createParser(obj));
130+
}
131+
return null;
132+
}
133+
}
134+
135+
}

src/test/java/org/eclipse/yasson/jsonstructure/JsonStructureToParserAdapterTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
34
*
45
* This program and the accompanying materials are made available under the
56
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -23,6 +24,7 @@
2324
import jakarta.json.JsonArrayBuilder;
2425
import jakarta.json.JsonObject;
2526
import jakarta.json.JsonObjectBuilder;
27+
import jakarta.json.bind.Jsonb;
2628
import jakarta.json.bind.JsonbBuilder;
2729
import jakarta.json.bind.JsonbConfig;
2830
import jakarta.json.spi.JsonProvider;
@@ -270,4 +272,35 @@ public void testCustomJsonbDeserializer() {
270272
assertEquals("String value 1", result.getInner().getInnerFirst());
271273
assertEquals("String value 2", result.getInner().getInnerSecond());
272274
}
275+
276+
@Test
277+
public void testGetValue() {
278+
final String json =
279+
"""
280+
{
281+
"type": "Location",
282+
"reference": "dummy reference"
283+
}
284+
""";
285+
286+
Jsonb jsonb = JsonbBuilder.create(
287+
new JsonbConfig().withDeserializers(new Issue673.ReferenceableDeserializer()));
288+
jsonb.fromJson(json, Issue673.LocationInterface.class);
289+
}
290+
291+
@Test
292+
public void testGetArray() {
293+
final String json =
294+
"""
295+
{
296+
"type": "Location",
297+
"tags": ["test1", "test2"]
298+
}
299+
""";
300+
301+
Jsonb jsonb = JsonbBuilder.create(
302+
new JsonbConfig().withDeserializers(new Issue673.ReferenceableDeserializer()));
303+
jsonb.fromJson(json, Issue673.LocationInterface.class);
304+
}
305+
273306
}

0 commit comments

Comments
 (0)