package cn.axzo.msg.center.service.util;

import cn.axzo.basics.common.exception.ServiceException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @author yanglin
 */
@Slf4j
public class JSONUtils {

    public static JSONObject parseObjectOrThrow(String fieldName, String jsonStr) {
        if (StringUtils.isBlank(jsonStr))
            return new JSONObject();
        try {
            return JSON.parseObject(jsonStr);
        } catch (Exception e) {
            log.warn("无效的json格式, fieldName={}, jsonStr={}", fieldName, jsonStr, e);
            throw new ServiceException(String.format("无效的参数格式, 要求是JSON对象. %s=%s", fieldName, jsonStr));
        }
    }

}
