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

com.goodow.realtime.model.util.JsonSerializer Maven / Gradle / Ivy

/*
 * Copyright 2012 Goodow.com
 * 
 * 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 com.goodow.realtime.model.util;

import com.goodow.realtime.CollaborativeObject;
import com.goodow.realtime.operation.util.JsonUtility;

import java.util.Map;

import elemental.json.Json;
import elemental.json.JsonArray;
import elemental.json.JsonType;
import elemental.json.JsonValue;

public class JsonSerializer {
  public static final int REFERENCE_TYPE = 2;
  private static final int VALUE_TYPE = 21;

  public static Object deserializeObject(JsonValue arrayOrNull,
      Map objects) {
    if (JsonUtility.isNull(arrayOrNull)) {
      return null;
    }
    JsonArray array = (JsonArray) arrayOrNull;
    switch ((int) array.getNumber(0)) {
      case VALUE_TYPE:
        switch (array.get(1).getType()) {
          case BOOLEAN:
            return array.getBoolean(1);
          case NUMBER:
            return array.getNumber(1);
          case STRING:
            return array.getString(1);
          case OBJECT:
          case ARRAY:
            JsonValue val = array.get(1);
            return Json.instance().parse(val.toJson());
          case NULL:
          default:
            throw new RuntimeException("Should not reach here!");
        }
      case REFERENCE_TYPE:
        return objects.get(array.getString(1));
      default:
        throw new UnsupportedOperationException();
    }
  }

  public static JsonArray serializeObject(Object obj) {
    if (obj == null) {
      return null;
    }
    JsonArray array = Json.createArray();
    int type = VALUE_TYPE;
    JsonValue val;
    if (obj instanceof String) {
      val = Json.create((String) obj);
    } else if (obj instanceof Number) {
      val = Json.create(((Number) obj).doubleValue());
    } else if (obj instanceof Boolean) {
      val = Json.create((Boolean) obj);
    } else if (obj instanceof JsonValue) {
      val = (JsonValue) obj;
      if (JsonType.NULL == val.getType()) {
        return null;
      }
      val = Json.instance().parse(val.toJson());
    } else if (obj instanceof CollaborativeObject) {
      type = REFERENCE_TYPE;
      val = Json.create(((CollaborativeObject) obj).getId());
    } else {
      throw new ClassCastException("Unsupported class type: " + obj.getClass().getName());
    }
    array.set(0, type);
    array.set(1, val);
    return array;
  }

  public static JsonValue[] serializeObjects(Object... values) {
    JsonValue[] array = new JsonValue[values.length];
    int i = 0;
    for (Object obj : values) {
      array[i++] = serializeObject(obj);
    }
    return array;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy