Add helper function to combine JSON strings as larger JSON file

This commit is contained in:
Veronica Berglyd Olsen
2025-10-05 17:13:40 +02:00
parent 04198f3ede
commit 0344701f1b
3 changed files with 23 additions and 6 deletions
+6
View File
@@ -560,6 +560,12 @@ def jsonEncode(data: dict | list | tuple, n: int = 0, nmax: int = 0) -> str:
return "".join(buffer)
def jsonCombine(data: dict[str, str]) -> str:
"""Combine multiple already packed JSON strings."""
payload = ",\n".join(f' "{k}": {v}' for k, v in data.items())
return f"{{\n{payload}\n}}\n"
##
# XML Helpers
##
+15 -4
View File
@@ -36,10 +36,10 @@ from novelwriter.common import (
describeFont, elide, encodeMimeHandles, firstFloat, fontMatcher,
formatFileFilter, formatInt, formatTime, formatTimeStamp, formatVersion,
fuzzyTime, getFileSize, hexToInt, isHandle, isItemClass, isItemLayout,
isItemType, isListInstance, isTitleTag, jsonEncode, makeFileNameSafe,
minmax, numberToRoman, openExternalPath, processDialogSymbols,
readTextFile, simplified, transferCase, uniqueCompact, utf16CharMap,
xmlElement, xmlIndent, xmlSubElem, yesNo
isItemType, isListInstance, isTitleTag, jsonCombine, jsonEncode,
makeFileNameSafe, minmax, numberToRoman, openExternalPath,
processDialogSymbols, readTextFile, simplified, transferCase,
uniqueCompact, utf16CharMap, xmlElement, xmlIndent, xmlSubElem, yesNo
)
from novelwriter.enum import nwItemClass
@@ -651,6 +651,17 @@ def testBaseCommon_jsonEncode():
)
@pytest.mark.base
def testBaseCommon_jsonCombine():
"""Test the jsonCombine function."""
assert jsonCombine({"a": "[1, 2]", "b": "[3, 4]"}) == (
'{\n'
' "a": [1, 2],\n'
' "b": [3, 4]\n'
'}\n'
)
@pytest.mark.base
def testBaseCommon_xmlIndent():
"""Test the xmlIndent function."""
+2 -2
View File
@@ -66,8 +66,8 @@ class C:
def cmpFiles(
fileOne: str | Path,
fileTwo: str | Path,
ignoreLines: list | None = None,
ignoreStart: tuple | None = None
ignoreLines: list[int] | None = None,
ignoreStart: tuple[str] | None = None
) -> bool:
"""Compare two files, with optional line ignore."""
if ignoreLines is None: