package me.chyxion.tigon.webmvc.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Oct 27, 2015 7:58:13 PM
 */
public class StringToJSONObjectTypeConverter 
    implements Converter<String, JSONObject> {

    /**
     * {@inheritDoc}
     */
    @Override
    public JSONObject convert(final String text) {
        try {
            return StringUtils.isNotBlank(text) ? 
                JSON.parseObject(text) : null;
        }
        catch (JSONException e) {
            throw new IllegalArgumentException(
                "Invalid JSONObject [" + text + "] param", e);
        }
    }
}
