package com.app.constant;

import com.app.service.guardian.dto.WxmpLoginDto;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.http.HttpStatus;

import java.io.IOException;

/**
 * 功能描述：重写JSON序列化
 *
 * @Auther: 梁展鹏
 * @Date: 2018/8/2 10:21
 */
public class EnumSerializer extends JsonSerializer {

    @Override
    public void serialize(Object object, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (object instanceof HttpStatus) {
            HttpStatus httpStatus = (HttpStatus) object;
            jsonGenerator.writeNumber(httpStatus.value());
        } else if (object instanceof WxmpLoginDto.StateEnum) {
            WxmpLoginDto.StateEnum stateEnum = (WxmpLoginDto.StateEnum) object;
            jsonGenerator.writeNumber(stateEnum.getValue());
        } else {
            jsonGenerator.writeString(object.toString());
        }
    }

}
