package com.njzscloud.common.http.processor;

import cn.hutool.core.util.ReflectUtil;
import com.njzscloud.common.http.annotation.BodyParam;

import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;

public class DefaultBodyParamProcessor implements BodyParamProcessor {
    @Override
    public byte[] process(BodyParam bodyParam, String paramName, Class<?> paramClazz, Object paramValue) {
        Method toBytesMethod = ReflectUtil.getMethod(paramClazz, "toBytes");
        if (toBytesMethod == null) {
            return paramValue.toString().getBytes(StandardCharsets.UTF_8);
        } else {
            return ReflectUtil.invoke(paramValue, toBytesMethod);
        }
    }
}
