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

org.dominokit.jacksonapt.AbstractObjectMapper Maven / Gradle / Ivy

The 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.jacksonapt;

import org.dominokit.jacksonapt.deser.array.ArrayJsonDeserializer;
import org.dominokit.jacksonapt.exception.JsonDeserializationException;
import org.dominokit.jacksonapt.exception.JsonSerializationException;
import org.dominokit.jacksonapt.stream.JsonReader;
import org.dominokit.jacksonapt.stream.JsonToken;
import org.dominokit.jacksonapt.stream.JsonWriter;

/**
 * Base implementation of {@link org.dominokit.jacksonapt.ObjectMapper}. It delegates the
 * serialization/deserialization to a serializer/deserializer.
 */
public abstract class AbstractObjectMapper implements ObjectMapper {

  private final String rootName;

  private JsonDeserializer deserializer;

  private JsonSerializer serializer;

  /**
   * Constructor for AbstractObjectMapper.
   *
   * @param rootName a {@link java.lang.String} object.
   */
  protected AbstractObjectMapper(String rootName) {
    this.rootName = rootName;
  }

  /** {@inheritDoc} */
  @Override
  public T read(String in) throws JsonDeserializationException {
    return read(in, DefaultJsonDeserializationContext.builder().build());
  }

  /** {@inheritDoc} */
  public T read(String in, JsonDeserializationContext ctx) throws JsonDeserializationException {
    JsonReader reader = ctx.newJsonReader(in);

    try {

      if (ctx.isUnwrapRootValue()) {

        if (JsonToken.BEGIN_OBJECT != reader.peek()) {
          throw ctx.traceError(
              "Unwrap root value is enabled but the input is not a JSON Object", reader);
        }
        reader.beginObject();
        if (JsonToken.END_OBJECT == reader.peek()) {
          throw ctx.traceError("Unwrap root value is enabled but the JSON Object is empty", reader);
        }
        String name = reader.nextName();
        if (!name.equals(rootName)) {
          throw ctx.traceError(
              "Unwrap root value is enabled but the name '"
                  + name
                  + "' don't match the expected rootName "
                  + "'"
                  + rootName
                  + "'",
              reader);
        }
        T result = getDeserializer().deserialize(reader, ctx);
        reader.endObject();
        return result;

      } else {

        return getDeserializer().deserialize(reader, ctx);
      }

    } catch (JsonDeserializationException e) {
      // already logged, we just throw it
      throw e;
    } catch (RuntimeException e) {
      throw ctx.traceError(e, reader);
    }
  }

  /** {@inheritDoc} */
  @Override
  public T[] readArray(String input, ArrayJsonDeserializer.ArrayCreator arrayCreator)
      throws JsonDeserializationException {
    return readArray(input, DefaultJsonDeserializationContext.builder().build(), arrayCreator);
  }

  /** {@inheritDoc} */
  @Override
  public T[] readArray(
      String input,
      JsonDeserializationContext ctx,
      ArrayJsonDeserializer.ArrayCreator arrayCreator)
      throws JsonDeserializationException {
    ArrayJsonDeserializer jsonDeserializer =
        ArrayJsonDeserializer.newInstance(getDeserializer(), arrayCreator);
    return jsonDeserializer.deserialize(ctx.newJsonReader(input), ctx);
  }

  /**
   * {@inheritDoc}
   *
   * 

Getter for the field deserializer. */ @Override public JsonDeserializer getDeserializer() { if (null == deserializer) { deserializer = newDeserializer(); } return deserializer; } /** * Instantiates a new deserializer * * @return a new deserializer */ protected abstract JsonDeserializer newDeserializer(); /** {@inheritDoc} */ @Override public String write(T value) throws JsonSerializationException { return write(value, DefaultJsonSerializationContext.builder().build()); } /** {@inheritDoc} */ public String write(T value, JsonSerializationContext ctx) throws JsonSerializationException { JsonWriter writer = ctx.newJsonWriter(); try { if (ctx.isWrapRootValue()) { writer.beginObject(); writer.name(rootName); getSerializer().serialize(writer, value, ctx); writer.endObject(); } else { getSerializer().serialize(writer, value, ctx); } return writer.getOutput(); } catch (JsonSerializationException e) { // already logged, we just throw it throw e; } catch (RuntimeException e) { throw ctx.traceError(value, e, writer); } } /** * {@inheritDoc} * *

Getter for the field serializer. */ @Override public JsonSerializer getSerializer() { if (null == serializer) { serializer = (JsonSerializer) newSerializer(); } return serializer; } /** * Instantiates a new serializer * * @return a new serializer */ protected abstract JsonSerializer newSerializer(); }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy