Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
*
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* 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,
Expand Down Expand Up @@ -86,6 +87,11 @@ public Event next() {
return next;
}

@Override
public Event currentEvent() {
return context.getLastValueEvent();
}

@Override
public String getString() {
return delegate.getString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -78,6 +79,11 @@ public Event next() {
return next;
}

@Override
public Event currentEvent() {
return iterators.peek().getValueEvent(getValue());
}

@Override
public String getString() {
return iterators.peek().getString();
Expand All @@ -103,6 +109,11 @@ public BigDecimal getBigDecimal() {
return getJsonNumberValue().bigDecimalValue();
}

@Override
public JsonValue getValue() {
return iterators.peek().getValue();
}

@Override
public JsonObject getObject() {
JsonStructureIterator current = iterators.peek();
Expand All @@ -115,6 +126,17 @@ public JsonObject getObject() {
}
}

@Override
public JsonArray getArray() {
JsonStructureIterator current = iterators.peek();
if (current instanceof JsonArrayIterator) {
iterators.pop();
return getValue().asJsonArray();
} else {
throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR, "Outside of array context"));
}
}

private JsonNumber getJsonNumberValue() {
JsonStructureIterator iterator = iterators.peek();
JsonValue value = iterator.getValue();
Expand All @@ -123,7 +145,7 @@ private JsonNumber getJsonNumberValue() {
}
return (JsonNumber) value;
}

@Override
public JsonLocation getLocation() {
throw new JsonbException("Operation not supported");
Expand Down
135 changes: 135 additions & 0 deletions src/test/java/org/eclipse/yasson/jsonstructure/Issue673.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* 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.jsonstructure;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import jakarta.json.bind.annotation.JsonbSubtype;
import jakarta.json.bind.annotation.JsonbTypeDeserializer;
import jakarta.json.bind.annotation.JsonbTypeInfo;
import jakarta.json.bind.serializer.DeserializationContext;
import jakarta.json.bind.serializer.JsonbDeserializer;
import jakarta.json.stream.JsonParser;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.stream.Collectors;

public class Issue673 {

public static interface Referenceable {

}

public static class Reference implements Referenceable {

private String description;

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}

public static class IRIReference implements Referenceable {

private String value;

public IRIReference() {}

public IRIReference(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}

@JsonbTypeInfo(key = "type", value = {
@JsonbSubtype(alias = Location.TYPE,
type = Location.class)
})
public static interface LocationInterface {

}

public static class Location implements LocationInterface {

public final static String TYPE = "Location";

private String tags;
private Referenceable referenceable;

@JsonbTypeDeserializer(TagsDeserializer.class)
public String getTags() {
return tags;
}

public void setTags(String tags) {
this.tags = tags;
}

@JsonbTypeDeserializer(ReferenceableDeserializer.class)
public Referenceable getReference() {
return referenceable;
}

public void setReference(Referenceable referenceable) {
this.referenceable = referenceable;
}
}

public static class TagsDeserializer implements JsonbDeserializer<String> {
@Override
public String deserialize(JsonParser jp, DeserializationContext dc, Type type) {
final JsonValue v = jp.getArray();
if (v instanceof JsonArray arr) {
return arr.stream()
.filter(JsonString.class::isInstance)
.map(JsonString.class::cast)
.map(JsonString::getString)
.collect(Collectors.joining(", "));
}
return null;
}

}

public static class ReferenceableDeserializer implements JsonbDeserializer<Referenceable> {

@Override
public Referenceable deserialize(JsonParser jp, DeserializationContext dc, Type type) {
final JsonValue v = jp.getValue();
if (v instanceof JsonString str) {
return new IRIReference(str.getString());
}
if (v instanceof JsonObject obj) {
dc.deserialize(Reference.class,
Json.createParserFactory(Collections.EMPTY_MAP)
.createParser(obj));
}
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -23,6 +24,7 @@
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.JsonbConfig;
import jakarta.json.spi.JsonProvider;
Expand Down Expand Up @@ -270,4 +272,35 @@ public void testCustomJsonbDeserializer() {
assertEquals("String value 1", result.getInner().getInnerFirst());
assertEquals("String value 2", result.getInner().getInnerSecond());
}

@Test
public void testGetValue() {
final String json =
"""
{
"type": "Location",
"reference": "dummy reference"
}
""";

Jsonb jsonb = JsonbBuilder.create(
new JsonbConfig().withDeserializers(new Issue673.ReferenceableDeserializer()));
jsonb.fromJson(json, Issue673.LocationInterface.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
jsonb.fromJson(json, Issue673.LocationInterface.class);
LocationInterface result = jsonb.fromJson(json, Issue673.LocationInterface.class);
assertNotNull(result);
assertTrue(result instanceof Location);
Location location = (Location) result;
Referenceable refAble = location.getReference();
assertNotNull(refAble);
assertFalse(refAble instanceof Reference);
assertTrue(refAble instanceof IRIReference);
IRIReference ref = (IRIReference) refAble;
assertEquals("dummy reference", ref.getValue());

suggestion: this test just verifiers that no exception is thrown, ensure the correct object is returned.

}

@Test
public void testGetArray() {
final String json =
"""
{
"type": "Location",
"tags": ["test1", "test2"]
}
""";

Jsonb jsonb = JsonbBuilder.create(
new JsonbConfig().withDeserializers(new Issue673.ReferenceableDeserializer()));
jsonb.fromJson(json, Issue673.LocationInterface.class);
}

}