package me.chyxion.tigon.webmvc;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.PostConstruct;
import me.chyxion.tigon.model.ViewModel;
import me.chyxion.tigon.model.ListResult;
import me.chyxion.tigon.json.ViewModelableSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Mar 19, 2017 11:31:04
 */
@Slf4j
@Getter
public class JSONViewConfig {
    @Value("${tigon.webmvc.jsonview.success-key:success}")
    private String successKey;
    @Value("${tigon.webmvc.jsonview.data-key:data}")
    private String dataKey;
    @Value("${tigon.webmvc.jsonview.code-key:errcode}")
    private String codeKey;
    @Value("${tigon.webmvc.jsonview.message-key:errmsg}")
    private String messageKey;
    @Autowired(required = false)
    private JSONViewDataModelAssembler dataModelAssembler;

    // init json serialize config
    @PostConstruct
    void init() {
        log.info("Init FastJSON global serialize config to serialize view modelable.");
        final SerializeConfig serializeConfig = SerializeConfig.getGlobalInstance();
        final ViewModelableSerializer viewModelableSerializer = new ViewModelableSerializer();
        serializeConfig.put(ViewModel.class, viewModelableSerializer);
        serializeConfig.put(ListResult.class, viewModelableSerializer);
    }
}
