Closed TruePluto closed 1 year ago
when using [property: DefaultValue(0.0)]
[property: DefaultValue(0.0)]
[ObservableProperty] [NotifyPropertyChangedFor(nameof(Summary))] [property: JsonProperty] [property: DefaultValue(0.0)] private double _x2 = 0.0;
the sourcegenerator build code to [DefaultValue(0)] from field to property
[DefaultValue(0)]
/// <inheritdoc cref="_x2"/> [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] [global::Newtonsoft.Json.JsonPropertyAttribute()] [global::System.ComponentModel.DefaultValueAttribute(0)] public double X2 { get => _x2; set { if (!global::System.Collections.Generic.EqualityComparer<double>.Default.Equals(_x2, value)) { OnX2Changing(value); OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.X2); _x2 = value; OnX2Changed(value); OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.X2); OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Summary); } } }
and that cause newtonsoft.json fail with DefaultValueHandling = DefaultValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
No response
I have a viewmodel such as
public partial class BaseVM : ObservableRecipient { [DefaultValue(7)] [JsonProperty()] public double PropDoubleZeroO { get; set; } = 7; [DefaultValue(7.0)] [JsonProperty()] public double PropDoubleZeroA { get; set; } = 7.0; [DefaultValue(7)] [JsonProperty()] public double PropDoubleZeroB { get; set; } = 7.0; [DefaultValue(7.0)] [JsonProperty()] public double PropDoubleZeroC { get; set; } = 7; //[property: JsonProperty] //[property: DefaultValue((double)15)] //private double _z2 = 165; }
using the code following
BaseVM vm0= new BaseVM(); JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings() { Formatting = Formatting.Indented, DefaultValueHandling = DefaultValueHandling.Ignore, TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple, MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead, PreserveReferencesHandling = PreserveReferencesHandling.Objects, }; var TempJson = JsonConvert.SerializeObject(vm0,jsonSerializerSettings); Console.WriteLine("--------BEGIN----------"); Console.WriteLine(TempJson); Console.WriteLine("---------END-----------");
the result is
--------BEGIN---------- { "$id": "1", "$type": "DvjsonTest.BaseVM, JsonDefaultValueConsole", "PropDoubleZeroO": 7.0, "PropDoubleZeroB": 7.0 } ---------END-----------
Meaning [DefaultValue(7)] is not equal to [DefaultValue(7.0)]
[DefaultValue(7)]
[DefaultValue(7.0)]
but when I suing ObservableObject with property such as
[ObservableProperty] [property: JsonProperty] [property: DefaultValue(0.0)] private double _x2 = 0.0;
the sourcegenerator generated the code :
/// <inheritdoc cref="_x2"/> ... [global::Newtonsoft.Json.JsonPropertyAttribute()] [global::System.ComponentModel.DefaultValueAttribute(0)] public double X2 { get => _x2; set { ... } } }
[global::System.ComponentModel.DefaultValueAttribute(0.0)] is expected
[global::System.ComponentModel.DefaultValueAttribute(0.0)]
VS 2022
8.1.0
Yes, I'd like to be assigned to work on this item
Good find, thank you! 🙂
Describe the bug
when using
[property: DefaultValue(0.0)]
the sourcegenerator build code to
[DefaultValue(0)]
from field to propertyand that cause newtonsoft.json fail with
DefaultValueHandling = DefaultValueHandling.Ignore,
Regression
No response
Steps to reproduce
I have a viewmodel such as
using the code following
the result is
Meaning
[DefaultValue(7)]
is not equal to[DefaultValue(7.0)]
but when I suing ObservableObject with property such as
the sourcegenerator generated the code :
Expected behavior
[global::System.ComponentModel.DefaultValueAttribute(0.0)]
is expectedScreenshots
No response
IDE and version
VS 2022
IDE version
No response
Nuget packages
Nuget package version(s)
8.1.0
Additional context
No response
Help us help you
Yes, I'd like to be assigned to work on this item