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

com.vmware.pscoe.iac.artifact.rest.helpers.JsonHelper Maven / Gradle / Ivy

There is a newer version: 3.1.1
Show newest version
/*
 * #%L
 * artifact-manager
 * %%
 * Copyright (C) 2023 VMware
 * %%
 * Build Tools for VMware Aria
 * Copyright 2023 VMware, Inc.
 * 
 * This product is licensed to you under the BSD-2 license (the "License"). You may not use this product except in compliance with the BSD-2 License.  
 * 
 * This product may include a number of subcomponents with separate copyright notices and license terms. Your use of these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file.
 * #L%
 */
package com.vmware.pscoe.iac.artifact.rest.helpers;

import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.function.Function;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonHelper {

	private static final Logger logger = LoggerFactory.getLogger(JsonHelper.class);

	public static String getPrettyJson(String json) {
		try{
			Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
			return gson.toJson(JsonParser.parseString(json));
		}catch(JsonSyntaxException e) {
			logger.error("Unable to parse Json[" + json + "]", e);
			return json;
		}
	}

	public static String toJson(Map map) {
		Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
		return gson.toJson(map);
	}

	public static String toSortedJson(Map map) {
		Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
		return gson.toJson(toTreeMap(map));
	}

	@SuppressWarnings("unchecked")
	private static TreeMap toTreeMap(Map obj) {
		TreeMap result = new TreeMap();
		obj.forEach((key, value) -> {
			result.put(key, value instanceof Map ? JsonHelper.toTreeMap((Map) value) : value);
		});
		return result;
	}

	public static  U getAs(JsonObject el, String property, Function mapper) {
		return Optional.ofNullable(el.get(property)).map(mapper).orElse(null);
	}

	public static  U getDefAs(JsonObject el, String property, Function mapper, U def) {
		return Optional.ofNullable(el.get(property)).map(mapper).orElse(def);
	}


}