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

de.gesellix.docker.response.JsonContentHandler Maven / Gradle / Ivy

There is a newer version: 2024-09-15T20-35-00
Show newest version
package de.gesellix.docker.response;

import okio.Okio;
import okio.Source;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class JsonContentHandler {

  public Object getContent(InputStream stream) throws IOException {
    return readJsonObject(stream);
  }

  public Object getContent(Source source) throws IOException {
    return readJsonObject(source);
  }

  private Object readJsonObject(InputStream stream) throws IOException {
    Source source = Okio.source(stream);
    return readJsonObject(source);
  }

  private Object readJsonObject(Source source) throws IOException {
    List parsed = new ArrayList<>();
    JsonChunksReader reader = new JsonChunksReader<>(source);
    while (reader.hasNext()) {
      parsed.add(reader.readNext(Object.class));
    }

    if (parsed.size() == 1) {
      return parsed.get(0);
    }
    return parsed;
  }
}