package com.persagy.adm.diagram.core.util;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.persagy.adm.diagram.core.model.template.DiagramTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 自定义ObjectMapper，为jackson定义不同场景下的处理逻辑
 * @author zhaoyk
 */
@Component
public class MyObjectMapper {

	private final ObjectMapper objectMapper;

	private ObjectMapper mapper4Save;

	@Autowired
	public MyObjectMapper(ObjectMapper objectMapper) {
		this.objectMapper = objectMapper;
		configCommonMapper();

		this.mapper4Save = buildMapper4Save();
	}

	private void configCommonMapper(){
		//objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		objectMapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
		objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
		objectMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
		objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
	}

	private ObjectMapper buildMapper4Save(){
		ObjectMapper mapper = new ObjectMapper();
		mapper.addMixIn(DiagramTemplate.class, Template4Save.class);
		//TODO
		return mapper;
	}


	@JsonIgnoreProperties({})
	public interface Template4Save {

	}

}
