All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.pac4j.oauth.profile.converter.JsonConverter Maven / Gradle / Ivy

There is a newer version: 6.1.0
Show newest version
package org.pac4j.oauth.profile.converter;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.pac4j.core.profile.converter.AttributeConverter;
import org.pac4j.oauth.profile.JsonHelper;

import java.io.IOException;

/**
 * This class converts a JSON node (or string) into an object.
 *
 * @author Jerome Leleu
 * @since 1.9.0
 */
@Slf4j
public final class JsonConverter implements AttributeConverter {

    private final Class clazz;

    private TypeReference typeReference;

    /**
     * 

Constructor for JsonConverter.

* * @param clazz a {@link Class} object */ public JsonConverter(final Class clazz) { this.clazz = clazz; } /** *

Constructor for JsonConverter.

* * @param clazz a {@link Class} object * @param typeReference a {@link TypeReference} object */ public JsonConverter(final Class clazz, final TypeReference typeReference) { this(clazz); this.typeReference = typeReference; } /** {@inheritDoc} */ @Override public Object convert(final Object attribute) { if (attribute != null) { if (clazz.isAssignableFrom(attribute.getClass())) { return attribute; } else if (attribute instanceof String || attribute instanceof JsonNode) { final String s; if (attribute instanceof String) { s = (String) attribute; } else { s = JsonHelper.toJSONString(attribute); } try { if (typeReference != null) { return JsonHelper.getMapper().readValue(s, typeReference); } else { return JsonHelper.getMapper().readValue(s, clazz); } } catch (final IOException e) { LOGGER.error("Cannot read value", e); } } } return null; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy