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

io.vertigo.orchestra.plugins.services.MapCodec Maven / Gradle / Ivy

The newest version!
/*
 * vertigo - application development platform
 *
 * Copyright (C) 2013-2024, Vertigo.io, [email protected]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.vertigo.orchestra.plugins.services;

import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;

import com.google.gson.Gson;
import com.google.gson.JsonParser;

import io.vertigo.commons.codec.Codec;
import io.vertigo.core.lang.json.CoreJsonAdapters;

/**
 * Encodes a map to a simple json (as a string).
 * Decodes a json (as a string) into a simple map.
 * @author pchretien
 */
public final class MapCodec implements Codec, String> {
	private static final Gson GSON = CoreJsonAdapters.V_CORE_GSON;

	@Override
	public Map decode(final String toEncode) {
		if (toEncode == null) {
			return Collections.emptyMap();
		}
		return new JsonParser()
				.parse(toEncode)
				.getAsJsonObject()
				.entrySet()
				.stream()
				.collect(Collectors.toMap(
						Map.Entry::getKey,
						entry -> entry.getValue().getAsString()));
	}

	@Override
	public String encode(final Map encoded) {
		return GSON.toJson(encoded);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy