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

fr.ird.observe.gson.DtoPropertyModificationAdapter Maven / Gradle / Ivy

package fr.ird.observe.gson;

/*-
 * #%L
 * ObServe Toolkit :: Dto
 * %%
 * Copyright (C) 2017 - 2021 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.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 fr.ird.observe.dto.DtoPropertyModification;

import java.io.Serializable;
import java.lang.reflect.Type;

/**
 * Created on 30/08/2020.
 *
 * @author Tony Chemit - [email protected]
 * @since 8.1.0
 */
public class DtoPropertyModificationAdapter implements JsonDeserializer, JsonSerializer {

    @Override
    public DtoPropertyModification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        JsonObject reportVariableJson = json.getAsJsonObject();

        String dcpId = context.deserialize(reportVariableJson.get("dcpId"), String.class);
        String propertyName = context.deserialize(reportVariableJson.get("propertyName"), String.class);
        Class type = context.deserialize(reportVariableJson.get("type"), Class.class);
        Serializable newValue1 = context.deserialize(reportVariableJson.get("newValue"), type);
        return new DtoPropertyModification(dcpId, propertyName, newValue1);
    }

    @Override
    public JsonElement serialize(DtoPropertyModification src, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject result = new JsonObject();
        result.add("dcpId", context.serialize(src.getId()));
        result.add("propertyName", context.serialize(src.getPropertyName()));
        Serializable newValue = src.getNewValue();
        result.add("type", context.serialize(newValue == null ? String.class : newValue.getClass()));
        result.add("newValue", context.serialize(newValue));
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy