package com.uaihebert.uaimockserver.util;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

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

public class GsonCollectionAdapter implements JsonSerializer<Collection<?>> {
    @Override
    public JsonElement serialize(final Collection<?> source, final Type typeOfSrc,
                                 final JsonSerializationContext context) {
        if (source == null || source.isEmpty()) {
            return null;
        }

        final JsonArray array = new JsonArray();

        for (Object child : source) {
            final JsonElement element = context.serialize(child);
            array.add(element);
        }

        return array;
    }
}
