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

cn.dustlight.flow.zeebe.converters.ZeebeObjectMapperCover Maven / Gradle / Ivy

package cn.dustlight.flow.zeebe.converters;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.zeebe.client.api.JsonMapper;
import io.camunda.zeebe.client.api.command.InternalClientException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class ZeebeObjectMapperCover implements JsonMapper {

    private static final TypeReference> MAP_TYPE_REFERENCE =
            new TypeReference>() {};

    private static final TypeReference> STRING_MAP_TYPE_REFERENCE =
            new TypeReference>() {};

    private final ObjectMapper objectMapper;

    public ZeebeObjectMapperCover(ObjectMapper mapper) {
        objectMapper = mapper;
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    @Override
    public  T fromJson(final String json, final Class typeClass) {
        try {
            return objectMapper.readValue(json, typeClass);
        } catch (final IOException e) {
            throw new InternalClientException(
                    String.format("Failed to deserialize json '%s' to class '%s'", json, typeClass), e);
        }
    }

    @Override
    public Map fromJsonAsMap(final String json) {
        try {
            return objectMapper.readValue(json, MAP_TYPE_REFERENCE);
        } catch (final IOException e) {
            throw new InternalClientException(
                    String.format("Failed to deserialize json '%s' to 'Map'", json), e);
        }
    }

    @Override
    public Map fromJsonAsStringMap(final String json) {
        try {
            return objectMapper.readValue(json, STRING_MAP_TYPE_REFERENCE);
        } catch (final IOException e) {
            throw new InternalClientException(
                    String.format("Failed to deserialize json '%s' to 'Map'", json), e);
        }
    }

    @Override
    public String toJson(final Object value) {
        try {
            return objectMapper.writeValueAsString(value);
        } catch (final JsonProcessingException e) {
            throw new InternalClientException(
                    String.format("Failed to serialize object '%s' to json", value), e);
        }
    }

    @Override
    public String validateJson(final String propertyName, final String jsonInput) {
        try {
            return objectMapper.readTree(jsonInput).toString();
        } catch (final IOException e) {
            throw new InternalClientException(
                    String.format(
                            "Failed to validate json input '%s' for property '%s'", jsonInput, propertyName),
                    e);
        }
    }

    @Override
    public String validateJson(final String propertyName, final InputStream jsonInput) {
        try {
            return objectMapper.readTree(jsonInput).toString();
        } catch (final IOException e) {
            throw new InternalClientException(
                    String.format("Failed to validate json input stream for property '%s'", propertyName), e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy