package eu.dnetlib.springutils.propertyeditors;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.propertyeditors.CustomMapEditor;

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

/**
 * Allow to put map definition inside a property file, by encoding it as plain string  (json format).
 * 
 * @author marko
 *
 */
public class JsonMapEditor extends CustomMapEditor {

	public JsonMapEditor() {
		super(HashMap.class);
	}
	
	public JsonMapEditor(Class<?> mapType) {
		super(mapType);
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		Gson son = new Gson();
		Type type = new TypeToken<Map<Long, String>>() {
		}.getType();
		super.setValue(son.fromJson(text, type));
	}

}
