JsonConfig: support JSON5

This commit is contained in:
李通洲
2025-08-19 10:25:52 +08:00
parent ea86273a0e
commit fa3b634c07
+20 -8
View File
@@ -376,13 +376,12 @@ static void listModules(bool pretty)
}
}
static bool parseJsoncFile(const char* path, bool strictJson)
static bool parseJsoncFile(const char* path, yyjson_read_flag flg)
{
assert(!instance.state.configDoc);
{
yyjson_read_err error;
yyjson_read_flag flg = strictJson ? 0 : YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS;
instance.state.configDoc = path
? yyjson_read_file(path, flg, NULL, &error)
: yyjson_read_fp(stdin, flg, NULL, &error);
@@ -480,11 +479,19 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val
FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateS(value);
bool strictJson = ffStrbufEndsWithIgnCaseS(&absolutePath, ".json");
bool needExtension = !strictJson && !ffStrbufEndsWithIgnCaseS(&absolutePath, ".jsonc");
bool jsonc = !strictJson && ffStrbufEndsWithIgnCaseS(&absolutePath, ".jsonc");
bool json5 = !strictJson && !jsonc && ffStrbufEndsWithIgnCaseS(&absolutePath, ".json5");
bool needExtension = !strictJson && !jsonc && !json5;
if (needExtension)
ffStrbufAppendS(&absolutePath, ".jsonc");
if (parseJsoncFile(absolutePath.chars, strictJson)) return;
yyjson_read_flag flag = strictJson
? 0
: jsonc
? YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS
: YYJSON_READ_JSON5;
if (parseJsoncFile(absolutePath.chars, flag)) return;
//Try to load as a relative path
@@ -496,7 +503,7 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val
if (needExtension)
ffStrbufAppendS(&absolutePath, ".jsonc");
if (parseJsoncFile(absolutePath.chars, strictJson)) return;
if (parseJsoncFile(absolutePath.chars, flag)) return;
}
//Try to load as a relative path with the directory of fastfetch binary
@@ -511,7 +518,7 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val
ffStrbufAppendS(&absolutePath, value);
if (needExtension)
ffStrbufAppendS(&absolutePath, ".jsonc");
if (parseJsoncFile(absolutePath.chars, strictJson)) return;
if (parseJsoncFile(absolutePath.chars, flag)) return;
// Try {exePath}/presets/
ffStrbufSubstrBefore(&absolutePath, lastSlash);
@@ -519,7 +526,7 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val
ffStrbufAppendS(&absolutePath, value);
if (needExtension)
ffStrbufAppendS(&absolutePath, ".jsonc");
if (parseJsoncFile(absolutePath.chars, strictJson)) return;
if (parseJsoncFile(absolutePath.chars, flag)) return;
}
//File not found
@@ -685,7 +692,12 @@ static void parseConfigFiles(void)
uint32_t dirLength = dir->length;
ffStrbufAppendS(dir, "fastfetch/config.jsonc");
bool success = parseJsoncFile(dir->chars, false);
bool success = parseJsoncFile(dir->chars, YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS);
ffStrbufSubstrBefore(dir, dirLength);
if (success) return;
ffStrbufAppendS(dir, "fastfetch/config.json5");
success = parseJsoncFile(dir->chars, YYJSON_READ_JSON5);
ffStrbufSubstrBefore(dir, dirLength);
if (success) return;
}