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

io.github.matteobertozzi.rednaco.data.DataFormatMapperString Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.github.matteobertozzi.rednaco.data;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;

import io.github.matteobertozzi.rednaco.data.json.JsonObject;

public abstract class DataFormatMapperString implements DataFormatMapper {
  @Override
  public JsonNode toTreeNode(final Object value) {
    return JsonFormat.INSTANCE.toTreeNode(value);
  }

  @Override
  public  T fromTreeNode(final JsonNode node, final Class valueType)
      throws JsonProcessingException, IllegalArgumentException {
    return JsonFormat.INSTANCE.fromTreeNode(node, valueType);
  }

  @Override
  public  T convert(final Object value, final Class valueType) {
    return JsonFormat.INSTANCE.convert(value, valueType);
  }

  @Override
  public  T convert(final Object value, final TypeReference valueType) {
    return JsonFormat.INSTANCE.convert(value, valueType);
  }

  @Override
  public  T fromStream(final InputStream stream, final Class valueType) throws IOException {
    return fromBytes(stream.readAllBytes(), valueType);
  }

  @Override
  public  T fromStream(final InputStream stream, final TypeReference valueType) throws IOException {
    return fromBytes(stream.readAllBytes(), valueType);
  }

  @Override
  public  T fromBytes(final byte[] data, final Class valueType) {
    return fromString(new String(data), valueType);
  }

  @Override
  public  T fromBytes(final byte[] data, final TypeReference valueType) {
    return fromString(new String(data), valueType);
  }

  @Override
  public  T fromBytes(final byte[] data, final int off, final int len, final Class valueType) {
    return fromString(new String(data, off, len), valueType);
  }

  @Override
  public  T fromString(final String data, final Class valueType) {
    return JsonFormat.INSTANCE.convert(parseFormatString(data), valueType);
  }

  @Override
  public  T fromString(final String data, final TypeReference valueType) {
    return JsonFormat.INSTANCE.convert(parseFormatString(data), valueType);
  }

  @Override
  public void addToStream(final OutputStream stream, final Object obj) throws IOException {
    stream.write(asBytes(obj));
  }

  @Override
  public void addToPrettyPrintStream(final OutputStream stream, final Object obj) throws IOException {
    addToStream(stream, obj);
  }

  @Override
  public String asPrettyPrintString(final Object value) {
    return asString(value);
  }

  @Override
  public String asString(final Object value) {
    return toFormatString(JsonFormat.INSTANCE.convert(value, JsonObject.class));
  }

  @Override
  public byte[] asBytes(final Object value) {
    return asString(value).getBytes(StandardCharsets.UTF_8);
  }

  protected abstract JsonObject parseFormatString(String data);
  protected abstract String toFormatString(JsonObject object);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy