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

io.nosqlbench.nb.api.config.params.JsonConfigSource Maven / Gradle / Ivy

Go to download

The top level API module for NoSQLBench. This module should have no internal module dependencies other than the mvn-default module. All modules within NoSQLBench can safely depend on this module with circular dependencies. This module provides cross-cutting code infrastracture, such as path utilities and ways of describing services used between modules. It is also the transitive aggregation point for system-wide library dependencies for logging and testing or similar needs.

There is a newer version: 5.17.0
Show newest version
package io.nosqlbench.nb.api.config.params;

import com.google.gson.*;

import java.util.ArrayList;
import java.util.List;

public class JsonConfigSource implements ConfigSource {
    private final static Gson gson = new GsonBuilder().setPrettyPrinting().create();
    private String name;

    @Override
    public boolean canRead(Object data) {
        if (data instanceof JsonElement) {
            return true;
        }
        if (data instanceof CharSequence) {
            return (data.toString().startsWith("[") || data.toString().startsWith("{"));
        }
        return false;
    }

    @Override
    public List getAll(String name, Object data) {
        this.name = name;

        JsonElement element = null;

        // Pull JSON element from data
        if (data instanceof CharSequence) {
            JsonParser p = new JsonParser();
            element = p.parse(data.toString());
        } else if (data instanceof JsonElement) {
            element = (JsonElement) data;
        }

        // Handle element modally by type
        List elements = new ArrayList<>();


        if (element.isJsonArray()) {
            JsonArray ary = element.getAsJsonArray();
            for (JsonElement jsonElem : ary) {
                if (jsonElem.isJsonObject()) {
                    elements.add(new JsonBackedConfigElement(null, jsonElem.getAsJsonObject()));
                } else {
                    throw new RuntimeException("invalid object type for element in sequence: "
                        + jsonElem.getClass().getSimpleName());
                }
            }
        } else if (element.isJsonObject()) {
            elements.add(new JsonBackedConfigElement(null,element.getAsJsonObject()));
        } else if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()) {
            String asString = element.getAsJsonPrimitive().getAsString();
            ElementData e = DataSources.element(name,asString);
            elements.add(e);
        } else {
            throw new RuntimeException("Invalid object type for element:" +
                element.getClass().getSimpleName());
        }

        return elements;
    }

    @Override
    public String getName() {
        return this.name;
    }
//
//    @Override
//    public ElementData getOneElementData(Object src) {
//        JsonElement element = (JsonElement) src;
//
//
//    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy