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

org.codegas.commons.ende.json.JsonValueDecoder Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.codegas.commons.ende.json;

import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.stream.Stream;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonValue;

import org.codegas.commons.ende.api.Decoder;

@FunctionalInterface
public interface JsonValueDecoder extends Decoder {

    static JsonValueDecoder extractValue(String key) {
        return jsonValue -> asObject().andThen(jsonObject -> jsonObject.get(key)).apply(jsonValue);
    }

    static JsonValueDecoder> toValueStream() {
        return jsonValue -> asArray().apply(jsonValue).stream();
    }

    static JsonValueDecoder asObject() {
        return JsonObject.class::cast;
    }

    static JsonValueDecoder asArray() {
        return JsonArray.class::cast;
    }

    default T decode(String json) {
        return json == null ? null : decode(new StringReader(json));
    }

    default T decode(Reader reader) {
        return decode(Json.createReader(reader).read());
    }

    default T decode(InputStream inputStream) {
        return decode(Json.createReader(inputStream).read());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy