统一使用fastjson进行Json操作

This commit is contained in:
Zheng Jie 2023-07-06 20:41:50 +08:00
parent da342842d2
commit b5cdaee8f0
5 changed files with 21 additions and 88 deletions

View File

@ -15,9 +15,9 @@
*/ */
package me.zhengjie.utils; package me.zhengjie.utils;
import cn.hutool.json.JSONArray; import com.alibaba.fastjson.JSON;
import cn.hutool.json.JSONObject; import com.alibaba.fastjson.JSONArray;
import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.utils.enums.DataScopeEnum; import me.zhengjie.utils.enums.DataScopeEnum;
@ -68,7 +68,9 @@ public class SecurityUtils {
*/ */
public static Long getCurrentUserId() { public static Long getCurrentUserId() {
UserDetails userDetails = getCurrentUser(); UserDetails userDetails = getCurrentUser();
return new JSONObject(new JSONObject(userDetails).get("user")).get("id", Long.class); // Java 对象转换为 JSONObject 对象
JSONObject jsonObject = (JSONObject) JSON.toJSON(userDetails);
return jsonObject.getJSONObject("user").getLong("id");
} }
/** /**
@ -77,8 +79,10 @@ public class SecurityUtils {
*/ */
public static List<Long> getCurrentUserDataScope(){ public static List<Long> getCurrentUserDataScope(){
UserDetails userDetails = getCurrentUser(); UserDetails userDetails = getCurrentUser();
JSONArray array = JSONUtil.parseArray(new JSONObject(userDetails).get("dataScopes")); // Java 对象转换为 JSONObject 对象
return JSONUtil.toList(array,Long.class); JSONObject jsonObject = (JSONObject) JSON.toJSON(userDetails);
JSONArray jsonArray = jsonObject.getJSONArray("dataScopes");
return JSON.parseArray(jsonArray.toJSONString(), Long.class);
} }
/** /**

View File

@ -1,66 +0,0 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.utils;
import cn.hutool.json.JSONArray;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
/**
* @author Zheng Jie
* 翻译工具类
*/
public class TranslatorUtil {
public static String translate(String word){
try {
String url = "https://translate.googleapis.com/translate_a/single?" +
"client=gtx&" +
"sl=en" +
"&tl=zh-CN" +
"&dt=t&q=" + URLEncoder.encode(word, "UTF-8");
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return parseResult(response.toString());
}catch (Exception e){
return word;
}
}
private static String parseResult(String inputJson){
JSONArray jsonArray2 = (JSONArray) new JSONArray(inputJson).get(0);
StringBuilder result = new StringBuilder();
for (Object o : jsonArray2) {
result.append(((JSONArray) o).get(0).toString());
}
return result.toString();
}
}

View File

@ -16,8 +16,8 @@
package me.zhengjie.service.impl; package me.zhengjie.service.impl;
import cn.hutool.core.lang.Dict; import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONObject; import com.alibaba.fastjson.JSON;
import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -85,9 +85,9 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
sysLog.setParams(getParameter(method, joinPoint.getArgs())); sysLog.setParams(getParameter(method, joinPoint.getArgs()));
// 记录登录用户隐藏密码信息 // 记录登录用户隐藏密码信息
if(signature.getName().equals("login") && StringUtils.isNotEmpty(sysLog.getParams())){ if(signature.getName().equals("login") && StringUtils.isNotEmpty(sysLog.getParams())){
JSONObject obj = JSONUtil.parseObj(sysLog.getParams()); JSONObject obj = JSON.parseObject(sysLog.getParams());
sysLog.setUsername(obj.getStr("username", "")); sysLog.setUsername(obj.getString("username"));
sysLog.setParams(JSONUtil.toJsonStr(Dict.create().set("username", sysLog.getUsername()))); sysLog.setParams(JSON.toJSONString(Dict.create().set("username", sysLog.getUsername())));
} }
sysLog.setBrowser(browser); sysLog.setBrowser(browser);
// 保存 // 保存
@ -121,7 +121,7 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
if (argList.isEmpty()) { if (argList.isEmpty()) {
return ""; return "";
} }
return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList); return argList.size() == 1 ? JSON.toJSONString(argList.get(0)) : JSON.toJSONString(argList);
} }
@Override @Override

View File

@ -15,10 +15,9 @@
*/ */
package me.zhengjie.modules.mnt.websocket; package me.zhengjie.modules.mnt.websocket;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.websocket.*; import javax.websocket.*;
import javax.websocket.server.PathParam; import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint; import javax.websocket.server.ServerEndpoint;
@ -37,7 +36,7 @@ public class WebSocketServer {
/** /**
* concurrent包的线程安全Set用来存放每个客户端对应的MyWebSocket对象 * concurrent包的线程安全Set用来存放每个客户端对应的MyWebSocket对象
*/ */
private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>(); private static final CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
/** /**
* 与某个客户端的连接会话需要通过它来给客户端发送数据 * 与某个客户端的连接会话需要通过它来给客户端发送数据
@ -55,11 +54,7 @@ public class WebSocketServer {
public void onOpen(Session session,@PathParam("sid") String sid) { public void onOpen(Session session,@PathParam("sid") String sid) {
this.session = session; this.session = session;
//如果存在就先删除一个防止重复推送消息 //如果存在就先删除一个防止重复推送消息
for (WebSocketServer webSocket:webSocketSet) { webSocketSet.removeIf(webSocket -> webSocket.sid.equals(sid));
if (webSocket.sid.equals(sid)) {
webSocketSet.remove(webSocket);
}
}
webSocketSet.add(this); webSocketSet.add(this);
this.sid=sid; this.sid=sid;
} }
@ -105,7 +100,7 @@ public class WebSocketServer {
* 群发自定义消息 * 群发自定义消息
* */ * */
public static void sendInfo(SocketMsg socketMsg,@PathParam("sid") String sid) throws IOException { public static void sendInfo(SocketMsg socketMsg,@PathParam("sid") String sid) throws IOException {
String message = JSONObject.toJSONString(socketMsg); String message = JSON.toJSONString(socketMsg);
log.info("推送消息到"+sid+",推送内容:"+message); log.info("推送消息到"+sid+",推送内容:"+message);
for (WebSocketServer item : webSocketSet) { for (WebSocketServer item : webSocketSet) {
try { try {

View File

@ -98,7 +98,7 @@ public class QiniuContentServiceImpl extends ServiceImpl<QiniuContentMapper, Qin
qiniuContent.setBucket(qiniuConfig.getBucket()); qiniuContent.setBucket(qiniuConfig.getBucket());
qiniuContent.setType(qiniuConfig.getType()); qiniuContent.setType(qiniuConfig.getType());
qiniuContent.setKey(FileUtil.getFileNameNoEx(putRet.key)); qiniuContent.setKey(FileUtil.getFileNameNoEx(putRet.key));
qiniuContent.setUrl(qiniuConfig.getHost()+"/"+putRet.key); qiniuContent.setUrl(qiniuConfig.getHost() + "/" + putRet.key);
qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(String.valueOf(file.getSize())))); qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(String.valueOf(file.getSize()))));
save(qiniuContent); save(qiniuContent);
} }