I am trying to migrate from Newtonsoft.Json to System.Text.Json. "[Newtonsoft.Json.JsonIgnore]" works for ignoring the constant property during serialization, but "[System.Text.Json.Serialization.JsonIgnore]" does not. I wonder if there is a work around.
So I am trying to migrate from;
using Newtonsoft.Json;
public class MyClass: MyBaseClass
{
[JsonIgnore]
public const string MyConstString = "lets get rid of netwonsoft dependency";
public string data;
public String(string data)
{
this.data = data;
}
}
to;
using System.Text.Json.Serialization;
public class MyClass: MyBaseClass
{
[JsonIgnore] // Error
public const string MyConstString = "lets get rid of netwonsoft dependency";
public string data;
public String(string data)
{
this.data = data;
}
}
The error description is; "Attribute 'JsonIgnore' is not valid on this declaration type. It is only valid on 'property, indexer' declarations."
Is it because System.Text.Json does not support such use of JsonIgnore, or am I missing something? I could not find anything useful on this link regarding this issue. Would you have any ideas?
public string MyConstString { get; } = "your string";System.Text.Jsonwon't serialise aconstanyway, you don't need that attribute here.constin the first place?constvalues if you explicitly mark them with the[JsonProperty]attribute.