Skip to content

Commit

Permalink
refactor: json parsing 관련 메소드 수정 (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
yongbin97 committed Oct 3, 2024
1 parent 27fb0e7 commit 34f0518
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package project.backend.business.post.implement;

import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatModel;
Expand All @@ -12,6 +15,8 @@
import project.backend.business.post.request.summary.SummaryOption;
import project.backend.business.post.response.dto.SummaryResultDto;
import project.backend.business.post.util.JsonParser;
import project.backend.common.error.CustomException;
import project.backend.common.error.ErrorCode;

@Slf4j
@Component
Expand All @@ -27,11 +32,12 @@ public SummaryResultDto getSummary(CreatePostServiceRequest createPostServiceReq
.getOutput()
.getContent();

Map<String, String> result = JsonParser.convertString2Json(responseContent);
JSONObject jsonObject = JsonParser.parseJsonFromText(responseContent);
Map<String, String> summaryResult = extractSummaryDataFromJson(jsonObject);

return SummaryResultDto.builder()
.title(result.get("title"))
.content(result.get("content"))
.title(summaryResult.get("title"))
.content(summaryResult.get("content"))
.build();
}

Expand All @@ -54,4 +60,21 @@ private Prompt getPrompt(CreatePostServiceRequest createPostServiceRequest) {
.withModel(VertexAiGeminiChatModel.ChatModel.GEMINI_1_5_FLASH)
.build());
}

private Map<String, String> extractSummaryDataFromJson(JSONObject jsonObject) {
try {
String title = jsonObject.getString("title");
String content = jsonObject.getString("content");

Map<String, String> summaryDataMap = new HashMap<>();

summaryDataMap.put("title", title);
summaryDataMap.put("content", content);

return summaryDataMap;

} catch (JSONException e) {
throw new CustomException(ErrorCode.INVALID_SUMMARY);
}
}
}
22 changes: 6 additions & 16 deletions src/main/java/project/backend/business/post/util/JsonParser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package project.backend.business.post.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONException;
Expand All @@ -11,29 +9,21 @@

public class JsonParser {

public static Map<String, String> convertString2Json(String text) {
public static JSONObject parseJsonFromText(String text) {
try {
text = stripJsonMarkdown(text);
JSONObject jsonObject = new JSONObject(text);

String title = jsonObject.getString("title");
String content = jsonObject.getString("content");

Map<String, String> result = new HashMap<>();
result.put("title", title);
result.put("content", content);
return result;
text = extractJsonContent(text);
return new JSONObject(text);
} catch (JSONException e) {
throw new CustomException(ErrorCode.BAD_REQUEST);
throw new CustomException(ErrorCode.INVALID_SUMMARY);
}
}

private static String stripJsonMarkdown(String text) {
private static String extractJsonContent(String text) {
String jsonPattern = "(?s)```json\\s*(.*?)\\s*```";
Pattern pattern = Pattern.compile(jsonPattern);
Matcher matcher = pattern.matcher(text);
if (!matcher.find()) {
throw new CustomException(ErrorCode.BAD_REQUEST);
throw new CustomException(ErrorCode.INVALID_SUMMARY);
}
return matcher.group(1);
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/project/backend/common/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@

@Getter
public enum ErrorCode {
// auth
NONE_AUTHENTICATED("인증 정보가 없습니다.", HttpStatus.UNAUTHORIZED),
NOT_AUTHENTICATED("유효하지 않는 인증 정보입니다.", HttpStatus.UNAUTHORIZED),
USER_NOT_FOUND("존재하지 않는 유저입니다.", HttpStatus.UNAUTHORIZED),
BAD_REQUEST("잘못된 요청입니다.", HttpStatus.BAD_REQUEST),
INVALID_REFRESH_TOKEN("유효하지 않은 리프레시 토큰입니다.", HttpStatus.UNAUTHORIZED),
INVALID_ACCESS_TOKEN("유효하지 않은 엑세스 토큰입니다.", HttpStatus.UNAUTHORIZED),
TOKEN_INVALID("유효하지 않은 토큰입니다.", HttpStatus.UNAUTHORIZED),
ACCESS_DENIED("접근 권한이 없습니다.", HttpStatus.FORBIDDEN),
NOT_EXIST_REFRESH_TOKEN("존재하지 않는 리프레시 토큰입니다.", HttpStatus.BAD_REQUEST);
NOT_EXIST_REFRESH_TOKEN("존재하지 않는 리프레시 토큰입니다.", HttpStatus.BAD_REQUEST),

// 400
BAD_REQUEST("잘못된 요청입니다.", HttpStatus.BAD_REQUEST),

// 500
INVALID_SUMMARY("웹 사이트 요약 중 문제가 발생하였습니다.", HttpStatus.INTERNAL_SERVER_ERROR);


private final String message;
private final HttpStatus httpStatus;
Expand Down

0 comments on commit 34f0518

Please sign in to comment.