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

org.nuiton.topia.persistence.gson.TopiaSqlScriptAdapter Maven / Gradle / Ivy

The newest version!
package org.nuiton.topia.persistence.gson;

/*-
 * #%L
 * ObServe Toolkit :: ToPIA Extension
 * %%
 * Copyright (C) 2017 - 2018 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.reflect.TypeToken;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.nuiton.topia.persistence.TopiaException;
import org.nuiton.topia.persistence.script.TopiaBlobsContainer;
import org.nuiton.topia.persistence.script.TopiaSqlScript;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Set;

/**
 * Created by tchemit on 17/05/2018.
 *
 * @author Tony Chemit - [email protected]
 */
public class TopiaSqlScriptAdapter implements JsonDeserializer, JsonSerializer {

    public static final Type TYPE = new TypeToken>() {
    }.getType();

    @Override
    public TopiaSqlScript deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
        JsonObject jsonObject = json.getAsJsonObject();
        byte[] location = context.deserialize(jsonObject.get("location"), byte[].class);
        Set blobs = context.deserialize(jsonObject.get("blobs"), TYPE);
        TopiaSqlScript result = TopiaSqlScript.of(location);
        if (blobs != null) {
            result.addBlobsContainers(blobs);
        }
        return result;
    }

    @Override
    public JsonElement serialize(TopiaSqlScript src, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject element = new JsonObject();
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            src.copy(outputStream);
            element.add("location", context.serialize(outputStream.toByteArray()));
        } catch (IOException e) {
            throw new TopiaException(e);
        }
        if (src.withBlobs()) {
            element.add("blobs", context.serialize(src.getBlobsContainers(), TYPE));
        }
        return element;
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy