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

io.getstream.core.models.serialization.DataDeserializer Maven / Gradle / Ivy

There is a newer version: 3.15.0
Show newest version
package io.getstream.core.models.serialization;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import io.getstream.core.models.Data;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

public final class DataDeserializer extends StdDeserializer {
  public DataDeserializer() {
    super(Data.class);
  }

  @Override
  public Data deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
    JsonNode node = parser.readValueAsTree();
    if (node.getNodeType() != JsonNodeType.OBJECT) {
      return new Data(node.asText());
    }
    Data data = new Data(node.get("id").asText());
    for (Map.Entry field : iterate(node.fields())) {
      if (field.getKey().equals("id")) continue;
      if (field.getKey().equals("data")) {
        data.from(field.getValue());
      } else {
        data.set(field.getKey(), parser.getCodec().treeToValue(field.getValue(), Object.class));
      }
    }
    return data;
  }

  private static  Iterable iterate(Iterator iterator) {
    return () -> iterator;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy