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

com.streamsets.pipeline.api.ext.json.JsonDelegate Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021 StreamSets Inc.
 *
 * 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 com.streamsets.pipeline.api.ext.json;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * A Json delegate is an agent that can be injected into a Json (Jackson) serializer/deserializer to alter the way a
 * field is serialized/deserialized. Most times it is used to format in a human-readable way a field in an string
 * offset, but there is not real limitation about how to use it.
 *
 * @param  represents te class type that is able to habdle a json delegate.
 */
public interface JsonDelegate {

  /**
   * As json delegates act on fields, it is possible to pass necessary processing information to this delegate through
   * a {@link JsonRule} annotation. This annotation can serve to specify format patterns and other formatting info.
   *
   * @return
   */
  JsonRule annotation();

  /**
   * This method is invoked when one annotated field is marked as using this custom serialization procedure.
   *
   * @param value the value of the field to serialize.
   * @param options the runtime parameters used to serialize.
   * @return the serialized value.
   */
  String serialize(T value, Map options);

  /**
   * This method is invoked when one annotated field is marked as using this custom deserialization procedure.
   *
   * @param value the value of the string to deserialize.
   * @param options the runtime parameters used to deserialize.
   * @return the deserialized field value.
   */
  T deserialize(String value, Map options);

  /**
   * Method that feeds the names of the necessary runtime parameters needed to serialize/deserialize, as specified in
   * the field annotations.
   *
   * @return the list of the parameters names.
   */
  default List getParameters () {
    List parameters = new ArrayList();
    for(JsonRuleParameter parameter : annotation().parameters()) {
      parameters.add(parameter.name());
    }
    return parameters;
  }

  /**
   * Method that finds, for a given necessary runtime parameter, its actual value using its alias.
   *
   *
   * @param options
   * @param name
   * @return
   */
  default Object findOption (Map options, String name) {
    Object option = null;
    for(JsonRuleParameter parameter : annotation().parameters()) {
      if (parameter.key().equals(name)) {
        option = options.get(parameter.name());
        break;
      }
    }
    return option;
  }

  /**
   * Provides the pattern to use to serialize/deserialize one field based on its annotations. Mostly used for dates
   * and numbers, but also usable for other scenarios.
   *
   *
   * @param options
   * @return
   */
  default String getPattern(Map options) {
    Object option = findOption(options, Parameters.PATTERN);
    return option == null ? null : (String) option;
  }

  /**
   * Utility method to check if some string has no actual value.
   *
   * @param string the string to check.
   * @return the result to this question.
   */
  default boolean isEmpty(final String string) {
    return string == null || string.trim().length() == 0;
  }

  /**
   * System-wide runtime parameters.
   */
  abstract class Parameters {

    public static final String PATTERN = "pattern";

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy