{"record":{"id":"e60272006f7b7f04","repo":"memstechtips/Winhance","slug":"unexpected-token-type-reader-tokentype-when-read","errorCode":null,"errorMessage":"Unexpected token type {reader.TokenType} when reading string array.","messagePattern":"Unexpected token type (.+?) when reading string array\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/Winhance.Core/Features/Common/Converters/StringOrStringArrayConverter.cs","lineNumber":42,"sourceCode":"        if (reader.TokenType == JsonTokenType.StartArray)\r\n        {\r\n            var list = new List<string>();\r\n            while (reader.Read())\r\n            {\r\n                if (reader.TokenType == JsonTokenType.EndArray)\r\n                    break;\r\n\r\n                if (reader.TokenType == JsonTokenType.String)\r\n                {\r\n                    var item = reader.GetString();\r\n                    if (item != null)\r\n                        list.Add(item);\r\n                }\r\n            }\r\n            return list.ToArray();\r\n        }\r\n\r\n        throw new JsonException($\"Unexpected token type {reader.TokenType} when reading string array.\");\r\n    }\r\n\r\n    public override void Write(Utf8JsonWriter writer, string[]? value, JsonSerializerOptions options)\r\n    {\r\n        if (value == null)\r\n        {\r\n            writer.WriteNullValue();\r\n            return;\r\n        }\r\n\r\n        writer.WriteStartArray();\r\n        foreach (var item in value)\r\n        {\r\n            writer.WriteStringValue(item);\r\n        }\r\n        writer.WriteEndArray();\r\n    }\r\n}\r","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/memstechtips/Winhance/blob/f23d554eb2d6400b1827bcc46b91294ffa53fbc9/src/Winhance.Core/Features/Common/Converters/StringOrStringArrayConverter.cs#L24-L60","documentation":"JsonException from StringOrStringArrayConverter.Read when the JSON token at the property position is neither null, a string, nor the start of an array. The converter exists to accept either a single string or a string[] for AppxPackageName backward-compat; anything else (a number, boolean, object, or raw value) is rejected as malformed.","triggerScenarios":"Deserializing a config/settings JSON where a property typed as string[] (with the converter attached) holds a non-string scalar — e.g. `\"AppxPackageName\": 123`, `\"AppxPackageName\": true`, or `\"AppxPackageName\": {\"Name\":\"...\"}`. The reader is positioned at that token and none of the null/string/startarray branches match.","commonSituations":"A config file exported by a different/older Winhance version serialized the field as a number or object. A user hand-edited the JSON and put a non-string. A schema migration left a stale value type. A numeric-only package name was serialized without quotes by a broken exporter.","solutions":["Open the config JSON and find the property the converter is reading; ensure its value is a string, null, or array of strings.","Quote numeric package names: change `\"AppxPackageName\": 12345` to `\"AppxPackageName\": \"12345\"`.","Re-export the config from a known-good Winhance install to regenerate a schema-valid file.","Tolerate the malformed input by coercion in the converter (see exampleFix) only if backward-compat is required."],"exampleFix":"// before: only null/string/array accepted; numbers/bools/object throw\nthrow new JsonException($\"Unexpected token type {reader.TokenType} when reading string array.\");\n\n// after: coerce numbers/bools to their string form for resilience, still reject objects\nif (reader.TokenType == JsonTokenType.Number || reader.TokenType == JsonTokenType.True || reader.TokenType == JsonTokenType.False)\n    return new[] { reader.GetRawText() };\nthrow new JsonException($\"Unexpected token type {reader.TokenType} when reading string array.\");","handlingStrategy":"validation","validationCode":"// Before deserializing, schema-check that AppxPackageName is string/array/null.\nusing var doc = JsonDocument.Parse(jsonText);\nif (doc.RootElement.TryGetProperty(\"AppxPackageName\", out var el))\n    if (el.ValueKind is not (JsonValueKind.String or JsonValueKind.Array or JsonValueKind.Null))\n        return; // reject the file before deserialization","typeGuard":null,"tryCatchPattern":"catch (System.Text.Json.JsonException ex) when (ex.Message.Contains(\"Unexpected token type\"))\n{\n    // Report the path/line from ex; tell the user which property has the wrong JSON value kind.\n}","preventionTips":["Quote numeric values in config JSON (treat package names as strings).","Re-export configs from a known-good Winhance install.","Coerce non-string scalars in the converter only as an explicit backward-compat decision."],"tags":["json","serialization","config","data-integrity","converter"],"backgroundTag":null,"analyzedSha":"f23d554eb2d6400b1827bcc46b91294ffa53fbc9","analyzedAt":"2026-08-13T17:55:20.922Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}