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

zipkin2.elasticsearch.internal.client.SearchResultConverter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright The OpenZipkin Authors
 * SPDX-License-Identifier: Apache-2.0
 */
package zipkin2.elasticsearch.internal.client;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import zipkin2.elasticsearch.internal.JsonSerializers.ObjectParser;

import static zipkin2.elasticsearch.internal.JsonReaders.enterPath;

public class SearchResultConverter implements HttpCall.BodyConverter> {
  final ObjectParser adapter;

  public static  SearchResultConverter create(ObjectParser adapter) {
    return new SearchResultConverter<>(adapter);
  }

  protected SearchResultConverter(ObjectParser adapter) {
    this.adapter = adapter;
  }

  @Override
  public List convert(JsonParser parser, Supplier contentString) throws IOException {
    JsonParser hits = enterPath(parser, "hits", "hits");
    if (hits == null || !hits.isExpectedStartArrayToken()) return List.of();

    List result = new ArrayList<>();
    while (hits.nextToken() != JsonToken.END_ARRAY) {
      JsonParser source = enterPath(hits, "_source");
      if (source != null) result.add(adapter.parse(source));
    }
    return result.isEmpty() ? List.of() : result;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy