|
1 | 1 | package com.back.global.converter; |
2 | 2 |
|
3 | | -import com.back.standard.util.Ut; |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 5 | import jakarta.persistence.AttributeConverter; |
5 | 6 | import jakarta.persistence.Converter; |
6 | 7 | import org.springframework.stereotype.Component; |
7 | 8 |
|
| 9 | +import java.util.ArrayList; |
8 | 10 | import java.util.List; |
9 | 11 |
|
10 | 12 | @Converter |
11 | 13 | @Component |
12 | 14 | public class StringListConverter implements AttributeConverter<List<String>, String> { |
13 | 15 |
|
| 16 | + private static final ObjectMapper objectMapper = new ObjectMapper(); |
| 17 | + |
14 | 18 | @Override |
15 | 19 | public String convertToDatabaseColumn(List<String> attribute) { |
16 | | - return attribute == null ? null : Ut.json.toString(attribute); |
| 20 | + if (attribute == null || attribute.isEmpty()) { |
| 21 | + return null; |
| 22 | + } |
| 23 | + try { |
| 24 | + return objectMapper.writeValueAsString(attribute); |
| 25 | + } catch (JsonProcessingException e) { |
| 26 | + throw new IllegalArgumentException("JSON 변환 실패", e); |
| 27 | + } |
17 | 28 | } |
18 | 29 |
|
19 | 30 | @Override |
20 | 31 | public List<String> convertToEntityAttribute(String dbData) { |
21 | | - return Ut.json.toList(dbData, String.class); |
| 32 | + if (dbData == null || dbData.trim().isEmpty()) { |
| 33 | + return new ArrayList<>(); |
| 34 | + } |
| 35 | + try { |
| 36 | + // 이중 인코딩된 경우 처리 |
| 37 | + String data = dbData; |
| 38 | + if (data.startsWith("\"") && data.endsWith("\"")) { |
| 39 | + data = objectMapper.readValue(data, String.class); |
| 40 | + } |
| 41 | + |
| 42 | + return objectMapper.readValue( |
| 43 | + data, |
| 44 | + objectMapper.getTypeFactory().constructCollectionType(List.class, String.class) |
| 45 | + ); |
| 46 | + } catch (JsonProcessingException e) { |
| 47 | + System.err.println("JSON 파싱 실패. 원본 데이터: " + dbData); |
| 48 | + e.printStackTrace(); |
| 49 | + return new ArrayList<>(); |
| 50 | + } |
22 | 51 | } |
23 | 52 | } |
0 commit comments