/
githubmirror
/
Files
Обзор
Документация
Войти
/
githubmirror
/
Files
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Files.Core.SourceGenerator/Parser/JsonParser.cs
117 строк
3 KB
Kris Vandermotten
Code Quality: Fix bugs in StringPropertyGenerator and StringsPropertyAnalyzer (#18161)
16 фев 2026, 20:58
Не верифицирован
16 фев 2026, 20:58
94e6559
Код
Авторство
О чём код?
// Copyright (c) Files Community // Licensed under the MIT License. using System.Text.Json; using static Files.Core.SourceGenerator.Constants.StringsPropertyGenerator; namespace Files.Core.SourceGenerator.Parser { /// <summary> /// Provides methods to parse JSON files and extract keys with optional context information. /// </summary> internal static class JsonParser { /// <summary> /// Parses a JSON file and extracts keys with optional context information. /// </summary> /// <param name="text">The text in the JSON file to parse.</param> /// <returns>An <see cref="IEnumerable{ParserItem}"/> containing the extracted keys and their associated values.</returns> internal static IEnumerable<ParserItem> GetKeys(string text) { var jsonDocument = JsonDocument.Parse(text); var result = new List<ParserItem>(); ProcessJsonObject(jsonDocument.RootElement, string.Empty, result); return result.OrderBy(item => item.Key); } private static void ProcessJsonObject(JsonElement jsonElement, string prefix, List<ParserItem> result) { if (jsonElement.ValueKind == JsonValueKind.Object) { foreach (var property in jsonElement.EnumerateObject()) { var key = string.IsNullOrEmpty(prefix) ? property.Name : $"{prefix}{ConstantSeparator}{property.Name}"; if (property.Value.ValueKind == JsonValueKind.Object) { var obj = property.Value; if (obj.TryGetProperty("text", out var textElement) && obj.TryGetProperty("crowdinContext", out var crowdinContextElement)) { // Add an entry for the "text" field and its corresponding context result.Add(new ParserItem { Key = key, Value = textElement.GetString() ?? string.Empty, Comment = crowdinContextElement.GetString() }); } else { // Recursively process the object ProcessJsonObject(obj, key, result); } } else { // Handle other value kinds directly ProcessJsonElement(property.Value, key, result); } } } else { // If the root element is not an object, process it directly ProcessJsonElement(jsonElement, prefix, result); } } private static void ProcessJsonElement(JsonElement element, string key, List<ParserItem> result) { switch (element.ValueKind) { case JsonValueKind.String: result.Add(new ParserItem { Key = key, Value = element.GetString() ?? string.Empty }); break; case JsonValueKind.Number: result.Add(new ParserItem { Key = key, Value = element.GetRawText() }); break; case JsonValueKind.True: case JsonValueKind.False: result.Add(new ParserItem { Key = key, Value = element.GetBoolean().ToString() }); break; case JsonValueKind.Array: var index = 0; foreach (var item in element.EnumerateArray()) { ProcessJsonElement(item, $"{key}[{index}]", result); index++; } break; default: result.Add(new ParserItem { Key = key, Value = element.GetRawText() }); break; } } } }