package com.eactive.eai.rms.common.converter;

import com.eactive.eai.rms.onl.common.exception.EMSRuntimeException;
import com.eactive.eai.rms.onl.manage.rule.layout.ui.LayoutItemUI;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StringToListLayoutItemUIGridDataConverter implements Converter<String, List<LayoutItemUI>> {
	@Autowired
	private ObjectMapper objectMapper;

	@Override
	public List<LayoutItemUI> convert(String source) {
		try {
			return objectMapper.readValue(source, new TypeReference<List<LayoutItemUI>>() {
			});
		} catch (JsonProcessingException e) {
			throw new EMSRuntimeException(e);
		}
	}

}
