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

dev.soffa.foundation.commons.ObjectUtil Maven / Gradle / Ivy

There is a newer version: 0.17.31
Show newest version
package dev.soffa.foundation.commons;

import lombok.SneakyThrows;

import java.nio.charset.StandardCharsets;
import java.util.Optional;


public final class ObjectUtil {


    private ObjectUtil() {
    }

    @SneakyThrows
    private static byte[] serialize(Object input) {
        if (input == null) {
            return null;
        }
        if (input instanceof byte[]) {
            return (byte[]) input;
        }
        if (input instanceof Optional) {
            Optional opt = (Optional) input;
            return opt.map(ObjectUtil::serialize).orElse(null);
        }
        return Mappers.JSON.serialize(input).getBytes(StandardCharsets.UTF_8);
    }

    @SneakyThrows
    private static  T deserialize(byte[] input, Class type) {
        if (input == null || input.length == 0) {
            return null;
        }
        return Mappers.JSON.deserialize(input, type);
    }

    @SuppressWarnings("unchecked")
    public static  T clone(T input) {
        return (T) deserialize(serialize(input), input.getClass());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy