{"record":{"id":"73da3533274fa86e","repo":"tui-cs/Terminal.Gui","slug":"propertytype-name-error-writing-property-with-c","errorCode":null,"errorMessage":"{propertyType.Name}: Error writing property with converter \"{converterType.FullName}\".","messagePattern":"(.+?): Error writing property with converter \"(.+?)\"\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Configuration/ScopeJsonConverter.cs","lineNumber":396,"sourceCode":"            converter = factory.CreateConverter (propertyType, options)!;\n        }\n\n        MethodInfo? writeMethod = converter.GetType ().GetMethod (nameof (Write), [typeof (Utf8JsonWriter), propertyType, typeof (JsonSerializerOptions)]);\n\n        if (writeMethod is null)\n        {\n            throw new JsonException ($\"{propertyType.Name}: Converter \\\"{converterType.FullName}\\\" does not expose a compatible Write method.\");\n        }\n\n        try\n        {\n            writeMethod.Invoke (converter, [writer, value, options]);\n\n            return true;\n        }\n        catch (TargetInvocationException e) when (e.InnerException is { })\n        {\n            throw new JsonException ($\"{propertyType.Name}: Error writing property with converter \\\"{converterType.FullName}\\\".\", e.InnerException);\n        }\n    }\n\n    private static bool TryWriteWithKnownConverter (Utf8JsonWriter writer, Type propertyType, Type converterType, object? value, JsonSerializerOptions options)\n    {\n        if (converterType == typeof (ConcurrentDictionaryJsonConverter<ThemeScope>))\n        {\n            new ConcurrentDictionaryJsonConverter<ThemeScope> ().Write (writer, (ConcurrentDictionary<string, ThemeScope>)value!, options);\n\n            return true;\n        }\n\n        if (converterType == typeof (DictionaryJsonConverter<Scheme?>))\n        {\n            new DictionaryJsonConverter<Scheme?> ().Write (writer, (Dictionary<string, Scheme?>)value!, options);\n\n            return true;\n        }","sourceCodeStart":378,"sourceCodeEnd":414,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Configuration/ScopeJsonConverter.cs#L378-L414","documentation":"Thrown by TryWriteWithDynamicConverter when the converter's Write method was found and invoked but threw an exception (surfaced as TargetInvocationException with a non-null InnerException). The InnerException is rewrapped as a JsonException so callers see the property type, the converter type, and the real underlying error. This is the write-side analog of error 84.","triggerScenarios":"Serializing a configuration property whose custom converter's Write throws — e.g. the converter cannot handle null, throws on an out-of-range enum, or fails on a value it wasn't designed for. Reached from any Write path (ConfigurationManager.ToJson, SourcesManager.ToStream, etc.).","commonSituations":"A converter assumes non-null but the property value is null; a converter written for an older schema encounters a new value; an enum converter hits an undefined member.","solutions":["Read the InnerException message printed alongside this JsonException to find the real failure in the converter, then fix the converter to handle that case.","Ensure the property value is in a state the converter expects before serializing (initialize defaults, validate enums).","If the converter is third-party, avoid assigning values it cannot serialize, or replace it with a source-generated type."],"exampleFix":"// before - converter throws on null\npublic override void Write (Utf8JsonWriter w, Foo v, JsonSerializerOptions o)\n    => w.WriteStringValue (v.Name); // NullReferenceException if v is null\n\n// after\npublic override void Write (Utf8JsonWriter w, Foo? v, JsonSerializerOptions o)\n{\n    if (v is null) { w.WriteNullValue (); return; }\n    w.WriteStringValue (v.Name);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { sourcesManager.ToJson (scope); }\ncatch (JsonException ex) when (ex.Message.Contains (\"Error writing property with converter\"))\n{\n    // ex.InnerException carries the converter's real exception\n    Logging.Error ($\"Write failed: {ex.InnerException?.Message}\");\n}","preventionTips":["Make converter Write methods null-safe and defensive against unexpected values.","Validate enum/value ranges before serializing config properties.","Unit-test converters against null and boundary values."],"tags":["config","json","serialization","converter","reflection"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}