@Override
 public void write(Json json, PolygonCentroid object, Class knownType) {
   json.writeObjectStart();
   json.writeValue("x", object.x);
   json.writeValue("y", object.y);
   json.writeObjectEnd();
 }
Exemple #2
0
 @Override
 public void write(Json json, TreeJson label, Class arg2) {
   json.writeObjectStart();
   json.writeValue("class", label.getClass().getName());
   json.writeValue("name", label.getName());
   json.writeValue("x", label.getX());
   json.writeValue("y", label.getY());
   json.writeValue("width", label.getWidth());
   json.writeValue("height", label.getHeight());
   json.writeValue("color", label.getColor().toString());
   json.writeObjectEnd();
 }
Exemple #3
0
  @Override
  public void write(Json json) {
    json.writeValue("name", name);

    json.writeObjectStart("dir");
    json.writeValue("x", dir.x);
    json.writeValue("y", dir.y);
    json.writeObjectEnd();

    json.writeValue("speed", speed);

    json.writeObjectStart("bounds");
    json.writeValue("x", bounds.x);
    json.writeValue("y", bounds.y);
    json.writeValue("width", bounds.width);
    json.writeValue("height", bounds.height);
    json.writeObjectEnd();

    json.writeObjectStart("score");
    score.write(json);
    json.writeObjectEnd();
  }
  @Override
  public void write(Json json, IntMap intMap, Class knownType) {
    json.writeObjectStart();
    json.writeValue(VALUE_SIZE, intMap.size);

    json.writeArrayStart(VALUE_ENTRIES);
    for (IntMap.Entry entry : (IntMap.Entries<?>) intMap.entries()) {
      json.writeValue(String.valueOf(entry.key), entry.value, null);
    }
    json.writeArrayEnd();

    json.writeObjectEnd();
  }
  public <E> void write(
      Json json, Iterable<E> entries, Function<E, ?> getKey, Function<E, ?> getValue) {

    json.writeArrayStart(name);

    entries.forEach(
        entry -> {
          json.writeObjectStart();

          json.writeValue("key", getKey.apply(entry), map.key());
          json.writeValue("value", getValue.apply(entry), map.value());

          json.writeObjectEnd();
        });

    json.writeArrayEnd();
  }
  /**
   * @param value May be null.
   * @param knownType May be null if the type is unknown.
   * @param elementType May be null if the type is unknown.
   */
  public void writeValue(Object value, Class knownType, Class elementType) {
    try {
      if (value == null) {
        writer.value(null);
        return;
      }

      Class actualType = value.getClass();

      if (actualType.isPrimitive()
          || actualType == String.class
          || actualType == Integer.class
          || actualType == Boolean.class
          || actualType == Float.class
          || actualType == Long.class
          || actualType == Double.class
          || actualType == Short.class
          || actualType == Byte.class
          || actualType == Character.class) {
        writer.value(value);
        return;
      }

      if (value instanceof Serializable) {
        writeObjectStart(actualType, knownType);
        ((Serializable) value).write(this);
        writeObjectEnd();
        return;
      }

      Serializer serializer = classToSerializer.get(actualType);
      if (serializer != null) {
        serializer.write(this, value, knownType);
        return;
      }

      if (value instanceof Array) {
        if (knownType != null && actualType != knownType)
          throw new SerializationException(
              "Serialization of an Array other than the known type is not supported.\n"
                  + "Known type: "
                  + knownType
                  + "\nActual type: "
                  + actualType);
        writeArrayStart();
        Array array = (Array) value;
        for (int i = 0, n = array.size; i < n; i++) writeValue(array.get(i), elementType, null);
        writeArrayEnd();
        return;
      }

      if (value instanceof Collection) {
        if (knownType != null && actualType != knownType && actualType != ArrayList.class)
          throw new SerializationException(
              "Serialization of a Collection other than the known type is not supported.\n"
                  + "Known type: "
                  + knownType
                  + "\nActual type: "
                  + actualType);
        writeArrayStart();
        for (Object item : (Collection) value) writeValue(item, elementType, null);
        writeArrayEnd();
        return;
      }

      if (actualType.isArray()) {
        if (elementType == null) elementType = actualType.getComponentType();
        int length = java.lang.reflect.Array.getLength(value);
        writeArrayStart();
        for (int i = 0; i < length; i++)
          writeValue(java.lang.reflect.Array.get(value, i), elementType, null);
        writeArrayEnd();
        return;
      }

      if (value instanceof OrderedMap) {
        if (knownType == null) knownType = OrderedMap.class;
        writeObjectStart(actualType, knownType);
        OrderedMap map = (OrderedMap) value;
        for (Object key : map.orderedKeys()) {
          writer.name(convertToString(key));
          writeValue(map.get(key), elementType, null);
        }
        writeObjectEnd();
        return;
      }

      if (value instanceof ArrayMap) {
        if (knownType == null) knownType = ArrayMap.class;
        writeObjectStart(actualType, knownType);
        ArrayMap map = (ArrayMap) value;
        for (int i = 0, n = map.size; i < n; i++) {
          writer.name(convertToString(map.keys[i]));
          writeValue(map.values[i], elementType, null);
        }
        writeObjectEnd();
        return;
      }

      if (value instanceof ObjectMap) {
        if (knownType == null) knownType = OrderedMap.class;
        writeObjectStart(actualType, knownType);
        for (Entry entry : ((ObjectMap<?, ?>) value).entries()) {
          writer.name(convertToString(entry.key));
          writeValue(entry.value, elementType, null);
        }
        writeObjectEnd();
        return;
      }

      if (value instanceof Map) {
        if (knownType == null) knownType = OrderedMap.class;
        writeObjectStart(actualType, knownType);
        for (Map.Entry entry : ((Map<?, ?>) value).entrySet()) {
          writer.name(convertToString(entry.getKey()));
          writeValue(entry.getValue(), elementType, null);
        }
        writeObjectEnd();
        return;
      }

      if (actualType.isEnum()) {
        writer.value(value);
        return;
      }

      writeObjectStart(actualType, knownType);
      writeFields(value);
      writeObjectEnd();
    } catch (IOException ex) {
      throw new SerializationException(ex);
    }
  }