Exemplo n.º 1
0
  @SuppressWarnings("unchecked")
  public Map<?, ?> read(Json json, JsonValue jsonData) {

    Map<K, V> values;

    try {
      values = (Map<K, V>) map.map().newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
      throw new GdxRuntimeException(e);
    }

    JsonValue entry = jsonData.getChild(name);

    while (entry != null) {

      JsonValue keyValue = entry.get("key");
      K key = json.readValue((Class<K>) map.key(), keyValue);

      JsonValue valueValue = entry.get("value");
      V value = json.readValue((Class<V>) map.value(), valueValue);

      values.put(key, value);

      entry = entry.next;
    }

    return values;
  }
Exemplo n.º 2
0
  @SuppressWarnings("unchecked")
  public <M> M read(
      Json json, JsonValue jsonData, Supplier<M> newInstance, KeyValueConsumer<M, K, V> put) {

    M values = newInstance.get();

    JsonValue entry = jsonData.getChild(name);

    while (entry != null) {

      JsonValue keyValue = entry.get("key");
      K key = json.readValue((Class<K>) map.key(), keyValue);

      JsonValue valueValue = entry.get("value");
      V value = json.readValue((Class<V>) map.value(), valueValue);

      put.accept(values, key, value);

      entry = entry.next;
    }

    return values;
  }
Exemplo n.º 3
0
  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();
  }