6

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?

6
  • 1
    a property declaration usually has a getter and/or a setter. Can you try changing it to public string MyConstString { get; } = "your string"; Commented Mar 4, 2020 at 15:54
  • 4
    System.Text.Json won't serialise a const anyway, you don't need that attribute here. Commented Mar 4, 2020 at 15:56
  • 3
    In fact neither will Newtonsoft, it's not clear why you have this attribute on your const in the first place? Commented Mar 4, 2020 at 15:57
  • 1
    Perhaps a bigger problem you have is that the new serialiser doesn't serialise fields, meaning you need to change them to properties. Commented Mar 4, 2020 at 16:02
  • 1
    Newtonsoft will only serialise const values if you explicitly mark them with the [JsonProperty] attribute. Commented Mar 4, 2020 at 16:08

1 Answer 1

4

The JSON.Net [JsonIgnore] attribute has it's usage set to AttributeTargets.Property | AttributeTargets.Field meaning it can be used on a const. However the new .NET Core API version of [JsonIgnore] is set to AttributeTargets.Property only. That means you can only use it on a property.

Having said that, JSON.Net will not serialise const values unless you explicitly tell it to serialise by using a [JsonProperty] attribute, which then makes adding another attribute to ignore it a little peculiar anyway.

For example, JSON.Net will serialise the class you have in your question as:

{"data":"foo"}

Whereas the serialiser in System.Text.Json will give you this:

{}

So another problem is that the newer API doesn't serialise fields. The takeaway from this is that you should be using modern C# techniques, and that means using properties, for example:

public string data { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.