spring-projects / spring-boot

Spring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.
https://spring.io/projects/spring-boot
Apache License 2.0
75.31k stars 40.71k forks source link

Refactor writeString to reduce duplication and improve readability #43205

Closed minwoo1999 closed 4 days ago

minwoo1999 commented 4 days ago

Description: I refactored the writeString method in the JsonValueWriter class to make it more readable and easier to maintain. Previously, the method used a long switch statement to handle special characters and ISO control characters, resulting in some redundancy and making it harder to follow.

  1. Extracted Special Character Handling

Introduced two new helper methods: isSpecialCharacter(char ch) to check if a character is one of the predefined special JSON characters. getEscapedCharacter(char ch) to return the escaped representation of these characters. This centralizes the logic for special character handling, making it reusable and reducing code duplication.

  1. Simplified ISO Control Character Handling

Moved the logic for ISO control characters to a separate condition outside the switch statement. This avoids redundancy and clearly separates concerns.

3.Improved Method Structure

The writeString method now follows a cleaner, linear structure, making it easier to understand and modify in the future.

philwebb commented 4 days ago

Thanks for the suggestions, but I find the existing code easier to read than the refactors that you're suggesting. I also prefer the switch because we don't need to duplicate the same chars as you've had to do in isSpecialCharacter and getEscapedCharacter.