-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
User defined deserializer/serializer in collection (#588)
User defined deserializer/serializer in collection Signed-off-by: David Kral <[email protected]>
- Loading branch information
Showing
7 changed files
with
196 additions
and
5 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
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
47 changes: 47 additions & 0 deletions
47
src/test/java/org/eclipse/yasson/serializers/model/Containee.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,47 @@ | ||
/* | ||
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* 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.serializers.model; | ||
|
||
import java.util.Objects; | ||
|
||
import jakarta.json.bind.annotation.JsonbTypeDeserializer; | ||
import jakarta.json.bind.annotation.JsonbTypeSerializer; | ||
|
||
@JsonbTypeDeserializer(ContaineeDeserializer.class) | ||
@JsonbTypeSerializer(ContaineeSerializer.class) | ||
public class Containee { | ||
final String key; | ||
final String value; | ||
|
||
public Containee(String key, String value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Containee containee = (Containee) o; | ||
return Objects.equals(key, containee.key) && Objects.equals(value, containee.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(key, value); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/org/eclipse/yasson/serializers/model/ContaineeDeserializer.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,41 @@ | ||
/* | ||
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* 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.serializers.model; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import jakarta.json.bind.serializer.DeserializationContext; | ||
import jakarta.json.bind.serializer.JsonbDeserializer; | ||
import jakarta.json.stream.JsonParser; | ||
|
||
public class ContaineeDeserializer implements JsonbDeserializer<Containee> { | ||
|
||
@Override | ||
public Containee deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { | ||
String key = null; | ||
String value = ""; | ||
while (parser.hasNext()) { | ||
var event = parser.next(); | ||
if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("key")) { | ||
parser.next(); // move to VALUE | ||
key = parser.getString(); | ||
} else if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("value")) { | ||
parser.next(); // move to VALUE | ||
value = parser.getString(); | ||
} | ||
} | ||
assert key != null; | ||
return new Containee(key, value); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/org/eclipse/yasson/serializers/model/ContaineeSerializer.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,29 @@ | ||
/* | ||
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* 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.serializers.model; | ||
|
||
import jakarta.json.bind.serializer.JsonbSerializer; | ||
import jakarta.json.bind.serializer.SerializationContext; | ||
import jakarta.json.stream.JsonGenerator; | ||
|
||
public class ContaineeSerializer implements JsonbSerializer<Containee> { | ||
|
||
@Override | ||
public void serialize(Containee obj, JsonGenerator generator, SerializationContext ctx) { | ||
generator.writeStartObject(); | ||
generator.write("key", obj.key); | ||
generator.write("value", obj.value); | ||
generator.writeEnd(); | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/test/java/org/eclipse/yasson/serializers/model/Container.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,55 @@ | ||
/* | ||
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* 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.serializers.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class Container { | ||
|
||
private List<Containee> containees; | ||
|
||
public Container() { | ||
containees = new ArrayList<>(); | ||
} | ||
|
||
public Container(List<Containee> containees) { | ||
this.containees = containees; | ||
} | ||
|
||
public void setContainees(List<Containee> containees) { | ||
this.containees = containees; | ||
} | ||
|
||
public List<Containee> getContainees() { | ||
return containees; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Container container = (Container) o; | ||
return Objects.equals(containees, container.containees); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(containees); | ||
} | ||
} |