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

org.dominokit.jackson.JsonDeserializer Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
/*
 * Copyright 2013 Nicolas Morel
 *
 * 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 org.dominokit.jackson;

import org.dominokit.jackson.exception.JsonDeserializationException;
import org.dominokit.jackson.stream.JsonReader;
import org.dominokit.jackson.stream.JsonToken;

/**
 * Base class for all the deserializer. It handles null values and exceptions. The rest is delegated
 * to implementations.
 */
public abstract class JsonDeserializer {

  /**
   * Deserializes a JSON input into an object.
   *
   * @param reader {@link org.dominokit.jackson.stream.JsonReader} used to read the JSON input
   * @param ctx Context for the full deserialization process
   * @return the deserialized object
   * @throws org.dominokit.jackson.exception.JsonDeserializationException if an error occurs during
   *     the deserialization
   */
  public T deserialize(JsonReader reader, JsonDeserializationContext ctx)
      throws JsonDeserializationException {
    return deserialize(reader, ctx, ctx.defaultParameters());
  }

  /**
   * Deserializes a JSON input into an object.
   *
   * @param reader {@link org.dominokit.jackson.stream.JsonReader} used to read the JSON input
   * @param ctx Context for the full deserialization process
   * @param params Parameters for this deserialization
   * @return the deserialized object
   * @throws org.dominokit.jackson.exception.JsonDeserializationException if an error occurs during
   *     the deserialization
   */
  public T deserialize(
      JsonReader reader, JsonDeserializationContext ctx, JsonDeserializerParameters params)
      throws JsonDeserializationException {
    if (JsonToken.NULL.equals(reader.peek())) {
      return deserializeNullValue(reader, ctx, params);
    }
    return doDeserialize(reader, ctx, params);
  }

  /**
   * Deserialize the null value. This method allows children to override the default behaviour.
   *
   * @param reader {@link org.dominokit.jackson.stream.JsonReader} used to read the JSON input
   * @param ctx Context for the full deserialization process
   * @param params Parameters for this deserialization
   * @return the deserialized object
   */
  protected T deserializeNullValue(
      JsonReader reader, JsonDeserializationContext ctx, JsonDeserializerParameters params) {
    reader.skipValue();
    return null;
  }

  /**
   * Deserializes a non-null JSON input into an object.
   *
   * @param reader {@link org.dominokit.jackson.stream.JsonReader} used to read the JSON input
   * @param ctx Context for the full deserialization process
   * @param params Parameters for this deserialization
   * @return the deserialized object
   */
  protected abstract T doDeserialize(
      JsonReader reader, JsonDeserializationContext ctx, JsonDeserializerParameters params);

  /**
   * Set the back reference.
   *
   * @param referenceName name of the reference
   * @param reference reference to set
   * @param value value to set the reference to.
   * @param ctx Context for the full deserialization process
   */
  public void setBackReference(
      String referenceName, Object reference, T value, JsonDeserializationContext ctx) {
    throw new JsonDeserializationException(
        "Cannot set a back reference to the type managed by this deserializer");
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy