Skip to content

Commit

Permalink
refactor : JsonNode로 파싱하는 메서드 BlockJsonProcessor로 이동 (Fastcampus-Fina…
Browse files Browse the repository at this point in the history
  • Loading branch information
miyounlee committed Oct 22, 2023
1 parent fae2eb2 commit a53903a
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.javajober.spaceWall.strategy;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.javajober.core.exception.ApiStatus;
import com.javajober.core.exception.ApplicationException;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.javajober.spaceWall.dto.request.BlockSaveRequest;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@Component
public class BlockJsonProcessor {
private final ObjectMapper jsonMapper;
Expand All @@ -33,7 +43,7 @@ public void addBlockInfoToArray(final ArrayNode blockInfoArray, final Long posit
blockInfoObject.put("position", position);
blockInfoObject.put("block_type", currentBlockTypeTitle);
blockInfoObject.put("block_id", blockId);
blockInfoObject.put("block_uuid", blockUUID);
blockInfoObject.put("block_UUID", blockUUID);

blockInfoArray.add(blockInfoObject);
}
Expand All @@ -46,8 +56,19 @@ public void addBlockToJsonArray(final ArrayNode blockInfoArray, final Long posit
blockInfoObject.put("position", position);
blockInfoObject.put("block_type", blockType);
blockInfoObject.put("block_id", blockId);
blockInfoObject.put("block_uuid", "");
blockInfoObject.put("block_UUID", "");

blockInfoArray.add(blockInfoObject);
}

public Map<Long, List<JsonNode>> toJsonNode(String blocks) {
try {
JsonNode jsonNode = jsonMapper.readTree(blocks);
return StreamSupport.stream(jsonNode.spliterator(), false)
.sorted(Comparator.comparingInt(a -> a.get("position").asInt()))
.collect(Collectors.groupingBy(node -> (long) node.get("position").asInt()));
} catch (JsonProcessingException e) {
throw new ApplicationException(ApiStatus.FAIL, "제이슨 변환 실패");
}
}
}

0 comments on commit a53903a

Please sign in to comment.