Skip to content

Commit

Permalink
FIX/345 : Serializing enums as property or as Map key has different b…
Browse files Browse the repository at this point in the history
…ehavior in case of toString overriding

Signed-off-by: Sébastien RIUS <sebastien.rius@capgemini.com>
  • Loading branch information
srius committed Oct 10, 2019
1 parent 5f869a7 commit e1e91e8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
******************************************************************************/
package org.eclipse.yasson.internal.serializer;

import java.util.Map;

import javax.json.bind.serializer.SerializationContext;
import javax.json.stream.JsonGenerator;
import java.util.Map;

/**
* Serialize {@link Map} with {@link String} keys as JSON Object:
Expand Down Expand Up @@ -77,8 +76,14 @@ public void writeStart(String key, JsonGenerator generator) {
*/
@Override
public void serializeContainer(Map<K,V> obj, JsonGenerator generator, SerializationContext ctx) {
for (Map.Entry<?,?> entry : obj.entrySet()) {
final String keyString = String.valueOf(entry.getKey());
for (Map.Entry<K,V> entry : obj.entrySet()) {
final String keyString;
K key = entry.getKey();
if (key instanceof Enum<?>) {
keyString = ((Enum<?>) key).name();
} else {
keyString = String.valueOf(key);
}
final Object value = entry.getValue();
if (value == null) {
if (serializer.isNullable()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2019 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 v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Sebastien Rius
******************************************************************************/
package org.eclipse.yasson.serializers;

import org.junit.Assert;
import org.junit.Test;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import java.util.EnumMap;
import java.util.Map;

/**
* Test various use-cases with {@code Map} serializer which
* stores Map as JSON object.
*/
public class MapToObjectSerializerTest {

/**
* Enum used as key in maps during tests
*/
enum TestEnum {
ONE, TWO;

@Override
/**
* Force to lower case to check toString is not used during serialization of maps
*/
public String toString() {
return this.name().toLowerCase();
}
}

/**
* Test serialization of Map with Number keys and String values.
*/
@Test
public void testSerializeEnumMapToObject() {
Map<TestEnum, Object> map = new EnumMap<>(TestEnum.class);
Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withFormatting(true));
map.put(TestEnum.ONE, "value1");
map.put(TestEnum.TWO, "value2");
String json = jsonb.toJson(map);
for (TestEnum e : TestEnum.values()) {
Assert.assertTrue("Enumeration not well serialized", json.contains(e.name()));
}
}
}

0 comments on commit e1e91e8

Please sign in to comment.