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

io.kazuki.v0.internal.helper.EncodingHelper Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2014 Sunny Gleason and original author or authors
 *
 * 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.kazuki.v0.internal.helper;

import io.kazuki.v0.internal.v2schema.types.UTCDateSecsTransform;
import io.kazuki.v0.store.KazukiException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.fasterxml.jackson.dataformat.smile.SmileGenerator;
import com.fasterxml.jackson.dataformat.smile.SmileParser;

public class EncodingHelper {
  private static final SmileFactory smileFactory = new SmileFactory();
  private static final ObjectMapper jsonMapper = new ObjectMapper();
  private static final ObjectMapper beanMapper = new ObjectMapper();

  static {
    beanMapper.registerModule(new UTCDateSecsTransform.DateTimeModule());
    jsonMapper.registerModule(new UTCDateSecsTransform.DateTimeModule());
  }

  @SuppressWarnings("unchecked")
  public static  Map asJsonMap(T value) throws Exception {
    return beanMapper.convertValue(value, LinkedHashMap.class);
  }

  public static  T asValue(Map objectMap, Class clazz) throws Exception {
    return beanMapper.convertValue(objectMap, clazz);
  }

  public static  String convertToJson(T value) throws Exception {
    return jsonMapper.writeValueAsString(value);
  }

  @SuppressWarnings("unchecked")
  public static Map parseJsonString(String value) throws Exception {
    if (value == null || value.length() == 0 || value.equals("null")) {
      throw new KazukiException("Invalid entity 'value'");
    }

    Object parsed = jsonMapper.readValue(value, LinkedHashMap.class);

    if (!(parsed instanceof Map)) {
      throw new KazukiException("Invalid entity 'value'");
    }

    return (Map) parsed;
  }

  public static byte[] convertToSmile(Object value) throws KazukiException {
    try {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      SmileGenerator smile = smileFactory.createGenerator(out);
      jsonMapper.writeValue(smile, value);

      byte[] smileBytes = out.toByteArray();

      return smileBytes;
    } catch (Exception e) {
      throw new KazukiException(e);
    }
  }

  public static  T parseSmile(byte[] valueBytes, Class clazz) throws KazukiException {
    try {
      ByteArrayInputStream in = new ByteArrayInputStream(valueBytes);
      SmileParser smile = smileFactory.createParser(in);

      return jsonMapper.readValue(smile, clazz);
    } catch (Exception e) {
      throw new KazukiException(e);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy