Reading and Writing Configuration Files with JSON-P

JSON-P ships with Java EE 8 and is already included in the API:

In standalone applications, like e.g. CLI, you will have to add the SPI (Service Provider Implementation) dependency:

JsonReader reads the configuration from InputStream:

    public final static String CONFIGURATION_FILE = "duke-config.json";

    public static JsonObjectBuilder load() throws FileNotFoundException {
        try (JsonReader reader = Json.createReader(new FileInputStream(CONFIGURATION_FILE))) {
            return Json.createObjectBuilder(reader.readObject());
and the JsonWrite serializes an in-memory instance into a stream:

    public static void write(JsonObject configuration) {
        try (JsonWriter writer = Json.createWriter(new FileOutputStream(CONFIGURATION_FILE))) {
        } catch (FileNotFoundException ex){}
An JsonObject is a typed Map<String, JsonValue>, the values can be directly access with a key:

public static String getValue(String key) {
    JsonObject configuration = null;
    try {
        configuration = load().build();
    } catch (FileNotFoundException ex) {    }
    return configuration.getString(key);
} uses the above approach (see to store the private and public keys, issuer, as well as the JWT location.

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home:

NEW workshops: MicroProfile with Quarkus and Micro Frontends with Web Components at MUC airport the podcast:

Stay in touch:


Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments