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

org.kurento.modulecreator.json.RemoteClassAdapter Maven / Gradle / Ivy

Go to download

Tool that generates code for RPC between the Kurento Media Server and remote libraries.

There is a newer version: 7.1.0
Show newest version
/*
 * (C) Copyright 2016 Kurento (http://kurento.org/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.kurento.modulecreator.json;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.kurento.modulecreator.definition.Method;
import org.kurento.modulecreator.definition.Property;
import org.kurento.modulecreator.definition.RemoteClass;
import org.kurento.modulecreator.definition.TypeRef;

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.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;

public class RemoteClassAdapter
    implements JsonSerializer, JsonDeserializer {

  @Override
  public JsonElement serialize(RemoteClass src, Type typeOfSrc, JsonSerializationContext context) {

    JsonObject object = new JsonObject();

    if (src.getName() != null) {
      object.addProperty("name", src.getName());
    }

    if (src.getDoc() != null) {
      object.addProperty("doc", src.getDoc());
    }

    if (src.isAbstract()) {
      object.add("abstract", new JsonPrimitive(true));
    }

    if (src.getExtends() != null) {
      object.add("extends", context.serialize(src.getExtends()));
    }

    if (src.getConstructor() != null) {
      object.add("constructor", context.serialize(src.getConstructor()));
    }

    if (!src.getProperties().isEmpty()) {
      object.add("properties", context.serialize(src.getProperties()));
    }

    if (!src.getMethods().isEmpty()) {
      object.add("methods", context.serialize(src.getMethods()));
    }

    if (!src.getEvents().isEmpty()) {
      object.add("events", context.serialize(src.getEvents()));
    }

    return object;
  }

  @Override
  public RemoteClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException {

    JsonObject object = (JsonObject) json;

    String name = null;
    String doc = null;
    boolean abstractValue = false;
    TypeRef extendsValue = null;
    Method constructor = null;
    List methods = new ArrayList();
    List properties = new ArrayList();
    List events = new ArrayList();

    if (object.get("name") != null) {
      name = object.get("name").getAsString();
    }

    if (object.get("doc") != null) {
      doc = object.get("doc").getAsString();
    }

    if (object.get("abstract") != null) {
      abstractValue = object.get("abstract").getAsBoolean();
    }

    if (object.get("extends") != null) {
      extendsValue = context.deserialize(object.get("extends"), TypeRef.class);
    }

    if (object.get("constructor") != null) {
      constructor = context.deserialize(object.get("constructor"), new TypeToken() {
      }.getType());
    }

    if (object.get("methods") != null) {
      methods = context.deserialize(object.get("methods"), new TypeToken>() {
      }.getType());
    }

    if (object.get("properties") != null) {
      properties = context.deserialize(object.get("properties"), new TypeToken>() {
      }.getType());
    }

    if (object.get("events") != null) {
      events = context.deserialize(object.get("events"), new TypeToken>() {
      }.getType());
    }

    RemoteClass remoteClass = new RemoteClass(name, doc, extendsValue, constructor, methods,
        properties, events);
    remoteClass.setAbstract(abstractValue);
    return remoteClass;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy