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

com.graphql_java_generator.server.util.JsonKit Maven / Gradle / Ivy

There is a newer version: 1.18
Show newest version
package com.graphql_java_generator.server.util;

import java.util.Collections;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

/**
 * This example code chose to use GSON as its JSON parser. Any JSON parser should be fine
 */
public class JsonKit {
	private static final Gson GSON = new GsonBuilder()
			//
			// This is important because the graphql spec says that null values should be present
			//
			.serializeNulls().create();

	public static Map toMap(String jsonStr) {
		if (jsonStr == null || jsonStr.trim().length() == 0) {
			return Collections.emptyMap();
		}
		// gson uses type tokens for generic input like Map
		TypeToken> typeToken = new TypeToken>() {
		};
		Map map = GSON.fromJson(jsonStr, typeToken.getType());
		return map == null ? Collections.emptyMap() : map;
	}

	public static String toJsonString(Object obj) {
		return GSON.toJson(obj);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy