Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make JSON formatter conserve escape sequences #36538

Merged
merged 1 commit into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ option(USE_HOME_DIR "Use user's home directory for save files." "ON")
option(LOCALIZE "Support for language localizations. Also enable UTF support." "ON")
option(LANGUAGES "Compile localization files for specified languages." "")
option(DYNAMIC_LINKING "Use dynamic linking. Or use static to remove MinGW dependency instead." "ON")
option(JSON_FORMAT "Build JSON formatter" "OFF")
option(CATA_CLANG_TIDY_PLUGIN "Build Cata's custom clang-tidy plugin" "OFF")
set(CATA_CLANG_TIDY_INCLUDE_DIR "" CACHE STRING "Path to internal clang-tidy headers required for plugin (e.g. ClangTidy.h)")
set(CATA_CHECK_CLANG_TIDY "" CACHE STRING "Path to check_clang_tidy.py for plugin tests")
Expand Down Expand Up @@ -354,6 +355,9 @@ if (NOT MSVC)
add_subdirectory(src/chkjson)
endif()
add_subdirectory(tests)
if (JSON_FORMAT)
add_subdirectory(tools/format)
endif()
if (CATA_CLANG_TIDY_PLUGIN)
add_subdirectory(tools/clang-tidy-plugin)
endif()
Expand Down
11 changes: 11 additions & 0 deletions tools/format/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include(ExternalProject)

add_executable(
json_formatter
format.cpp
${CMAKE_SOURCE_DIR}/src/json.cpp
)

ADD_DEFINITIONS(-DCATA_IN_TOOL)

INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/src )
12 changes: 10 additions & 2 deletions tools/format/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,16 @@ static void format( JsonIn &jsin, JsonOut &jsout, int depth, bool force_wrap )
} else if( jsin.test_object() ) {
format_collection( jsin, jsout, depth, write_object, force_wrap );
} else if( jsin.test_string() ) {
std::string str = jsin.get_string();
jsout.write( str );
// The string may contain escape sequences which we want to keep in the output.
const int start_pos = jsin.tell();
jsin.get_string();
const int end_pos = jsin.tell();
std::string str = jsin.substr( start_pos, end_pos - start_pos );
str = str.substr( str.find( '"' ) );
str = str.substr( 0, str.rfind( '"' ) + 1 );
jsout.write_separator();
*jsout.get_stream() << str;
jsout.set_need_separator();
} else if( jsin.test_number() ) {
// Have to introspect into the string to distinguish integers from floats.
// Otherwise they won't serialize correctly.
Expand Down