Skip to content

Commit

Permalink
Conserve escape sequences in json formatter & add json formatter to C…
Browse files Browse the repository at this point in the history
…Make lists
  • Loading branch information
Qrox committed Dec 29, 2019
1 parent 072ff2f commit 342170a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
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

0 comments on commit 342170a

Please sign in to comment.