![JAR search and dependency download from the Maven repository](/logo.png)
org.eclipse.yasson.internal.serializer.MapSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yasson Show documentation
Show all versions of yasson Show documentation
Eclipse Yasson. Reference implementation of JSR-367 (JSON-B).
/*******************************************************************************
* Copyright (c) 2016, 2018 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:
* Roman Grigoriadi
******************************************************************************/
package org.eclipse.yasson.internal.serializer;
import org.eclipse.yasson.internal.ReflectionUtils;
import javax.json.bind.serializer.SerializationContext;
import javax.json.stream.JsonGenerator;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Optional;
/**
* Serializer for maps.
*
* @author Roman Grigoriadi
*/
public class MapSerializer> extends AbstractContainerSerializer implements EmbeddedItem {
protected MapSerializer(SerializerBuilder builder) {
super(builder);
}
@SuppressWarnings("unchecked")
@Override
protected void serializeInternal(T obj, JsonGenerator generator, SerializationContext ctx) {
for (Map.Entry,?> entry : obj.entrySet()) {
final String keysString = String.valueOf(entry.getKey());
final Object value = entry.getValue();
if (value == null) {
generator.writeNull(keysString);
continue;
}
generator.writeKey(keysString);
serializeItem(value, generator, ctx);
}
}
@Override
protected void writeStart(JsonGenerator generator) {
generator.writeStartObject();
}
@Override
protected void writeStart(String key, JsonGenerator generator) {
generator.writeStartObject(key);
}
@Override
protected Type getValueType(Type valueType) {
if (valueType instanceof ParameterizedType) {
Optional runtimeTypeOptional = ReflectionUtils.resolveOptionalType(this, ((ParameterizedType) valueType).getActualTypeArguments()[1]);
return runtimeTypeOptional.orElse(Object.class);
}
return Object.class;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy