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

io.camunda.common.json.SdkObjectMapper Maven / Gradle / Ivy

There is a newer version: 8.7.0-alpha1
Show newest version
/*
 * Copyright © 2017 camunda services GmbH ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.camunda.common.json;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.camunda.common.exception.SdkException;
import java.io.IOException;
import java.util.Map;

public class SdkObjectMapper 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 SdkObjectMapper() {
    this(new ObjectMapper());
  }

  public SdkObjectMapper(final ObjectMapper objectMapper) {
    this.objectMapper = objectMapper;
    this.objectMapper
        .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        .setSerializationInclusion(JsonInclude.Include.NON_NULL);
  }

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

  @Override
  public  T fromJson(
      final String json, final Class resultType, final Class parameterType) {
    try {
      final JavaType javaType =
          objectMapper.getTypeFactory().constructParametricType(resultType, parameterType);
      return objectMapper.readValue(json, javaType);
    } catch (final IOException e) {
      throw new SdkException(
          String.format(
              "Failed to deserialize json '%s' to class '%s' with parameter '%s",
              json, resultType, parameterType),
          e);
    }
  }

  @Override
  public Map fromJsonAsMap(final String json) {
    try {
      return objectMapper.readValue(json, MAP_TYPE_REFERENCE);
    } catch (final IOException e) {
      throw new SdkException(
          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 SdkException(
          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 SdkException(String.format("Failed to serialize object '%s' to json", value), e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy