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

io.vlingo.common.serialization.JsonSerialization Maven / Gradle / Ivy

There is a newer version: 1.7.5
Show newest version
// Copyright © 2012-2020 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.

package io.vlingo.common.serialization;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;

public class JsonSerialization {
  private final static Gson gson;

  static {
    gson = new GsonBuilder()
        .registerTypeAdapter(Class.class, new ClassSerializer())
        .registerTypeAdapter(Class.class, new ClassDeserializer())
        .registerTypeAdapter(Date.class, new DateSerializer())
        .registerTypeAdapter(Date.class, new DateDeserializer())
        .create();
  }

  public static  T deserialized(String serialization, final Class type) {
    T instance = gson.fromJson(serialization, type);
    return instance;
  }

  public static  T deserialized(String serialization, final Type type) {
    T instance = gson.fromJson(serialization, type);
    return instance;
  }

  public static  List deserializedList(String serialization, final Type listOfType) {
    final List list = gson.fromJson(serialization, listOfType);
    return list;
  }

  public static String serialized(final Object instance) {
    final String serialization = gson.toJson(instance);
    return serialization;
  }

  public static  String serialized(final Collection instance) {
    final Type collectionOfT = new TypeToken>(){}.getType();
    final String serialization = gson.toJson(instance, collectionOfT);
    return serialization;
  }

  public static  String serialized(final List instance) {
    final Type listOfT = new TypeToken>(){}.getType();
    final String serialization = gson.toJson(instance, listOfT);
    return serialization;
  }

  @SuppressWarnings("rawtypes")
  private static class ClassSerializer implements JsonSerializer {
    @Override
    public JsonElement serialize(Class source, Type typeOfSrc, JsonSerializationContext context) {
      return new JsonPrimitive(source.getName());
    }
  }

  @SuppressWarnings("rawtypes")
  private static class ClassDeserializer implements JsonDeserializer {
    @Override
    public Class deserialize(JsonElement json, Type typeOfTarget, JsonDeserializationContext context) throws JsonParseException {
        final String classname = json.getAsJsonPrimitive().getAsString();
        try {
          return Class.forName(classname);
        } catch (ClassNotFoundException e) {
          throw new JsonParseException(e);
        }
    }
  }

  private static class DateSerializer implements JsonSerializer {
    @Override
    public JsonElement serialize(Date source, Type typeOfSource, JsonSerializationContext context) {
        return new JsonPrimitive(Long.toString(source.getTime()));
    }
  }

  private static class DateDeserializer implements JsonDeserializer {
    @Override
    public Date deserialize(JsonElement json, Type typeOfTarget, JsonDeserializationContext context) throws JsonParseException {
        long time = Long.parseLong(json.getAsJsonPrimitive().getAsString());
        return new Date(time);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy