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

org.eclipse.jkube.kit.common.JsonFactory Maven / Gradle / Ivy

/**
 * Copyright (c) 2019 Red Hat, Inc.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at:
 *
 *     https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.jkube.kit.common;

import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class JsonFactory {
    private static final Gson GSON = new Gson();

    private JsonFactory() {
        // Empty Constructor
    }

    public static JsonObject newJsonObject(String json) {
        return GSON.fromJson(json, JsonObject.class);
    }

    public static JsonArray newJsonArray(String json) {
        return GSON.fromJson(json, JsonArray.class);
    }

    public static JsonArray newJsonArray(List list) {
        final JsonArray jsonArray = new JsonArray();

        for(String element : list)
        {
            jsonArray.add(element);
        }

        return jsonArray;
    }

    public static JsonObject newJsonObject(Map map) {
        final JsonObject jsonObject = new JsonObject();

        for (Map.Entry entry : map.entrySet()) {
            jsonObject.addProperty(entry.getKey(), entry.getValue());
        }

        return jsonObject;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy