im getting StackOverflowException in method JsonTypeConverter.ConvertTo when calling Project.Settings.Default.Save()
i think this it is because method JsonConvert.SerializeObject detect typeconverter on model type and calls it internally ...
whats the correct way to write this? (only way i can think of is storing string in settings and do serialization/deserialization manualy ...)
public class JsonTypeConverter<TModel> : TypeConverter { #region Overrides of TypeConverter public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return destinationType == typeof(string); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string strValue) { return JsonConvert.DeserializeObject<TModel>(strValue); } return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value is TModel model) { return JsonConvert.SerializeObject(model, Formatting.None); } return base.ConvertTo(context, culture, value, destinationType); } #endregion } namespace Model { [TypeConverter(typeof(JsonTypeConverter<DataModel>))] [SettingsSerializeAs(SettingsSerializeAs.String)] public class DataModel { public string Value { get; set; } } } content of 'Properties/Settings.settings':
<?xml version='1.0' encoding='utf-8'?> <SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="Builder.Properties" GeneratedClassName="Settings"> <Profiles /> <Settings> <Setting Name="DataSource" Type="Model.DataModel" Scope="User"> <Value Profile="(Default)" /> </Setting> </Settings> </SettingsFile> PS: Any suggestions for this question title?