001/*
002 *    Copyright 2024-2025, Warm-Flow (290631660@qq.com).
003 *
004 *    Licensed under the Apache License, Version 2.0 (the "License");
005 *    you may not use this file except in compliance with the License.
006 *    You may obtain a copy of the License at
007 *
008 *       https://www.apache.org/licenses/LICENSE-2.0
009 *
010 *    Unless required by applicable law or agreed to in writing, software
011 *    distributed under the License is distributed on an "AS IS" BASIS,
012 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *    See the License for the specific language governing permissions and
014 *    limitations under the License.
015 */
016package org.dromara.warm.plugin.json;
017
018import com.alibaba.fastjson2.JSON;
019import com.alibaba.fastjson2.TypeReference;
020import org.dromara.warm.flow.core.json.JsonConvert;
021import org.dromara.warm.flow.core.utils.MapUtil;
022import org.dromara.warm.flow.core.utils.StringUtils;
023
024import java.util.HashMap;
025import java.util.Map;
026
027/**
028 * fastjson:map和json字符串转换工具类
029 *
030 * @author warm
031 */
032public class JsonConvertFastJson implements JsonConvert {
033
034    /**
035     * 将字符串转为map
036     * @param jsonStr json字符串
037     * @return map
038     */
039    @Override
040    public Map<String, Object> strToMap(String jsonStr) {
041        if (StringUtils.isNotEmpty(jsonStr)) {
042            return JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>(){});
043        }
044        return new HashMap<>();
045    }
046
047    /**
048     * 将map转为字符串
049     * @param variable map
050     * @return json字符串
051     */
052    @Override
053    public String mapToStr(Map<String, Object> variable) {
054        if (MapUtil.isNotEmpty(variable)) {
055            return JSON.toJSONString(variable);
056        }
057        return null;
058    }
059
060}