0

Newtonsoft.Json ignore JsonProperty attributes while serializing object to json in .NET Framework 4.5 legacy webapi project.

Properties in my class looks as simple as this:

using Newtonsoft.Json;

public class A
{

    [JsonProperty(PropertyName = "workflow")]
    public string Workflow { get; private set; }

}

Then i am executing serializing:

using Newtonsoft.Json;

var asJson = JsonConvert.SerializeObject(
    a,
    Formatting.None,
    new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Ignore
    });

And output looks like this:

{
    "Workflow": "18",
}

instead of:

{
    "workflow": "18"
},

Based on JsonProperty. My question is, why newtonsoft serializer ignore Jsonproperty attributes in .NET Framework 4.5 web api project?

michasaucer
  • 4,562
  • 9
  • 40
  • 91
  • Please add all the detail required to reproduce the problem – stuartd Jul 04 '22 at 16:57
  • @stuartd What do you need can you tell me please? – michasaucer Jul 04 '22 at 17:00
  • You need to put the full json and full class, not just one property – Serge Jul 04 '22 at 17:03
  • OK got it, check this now – michasaucer Jul 04 '22 at 17:04
  • Thanks, but it is not valid, can you post a valid json pls, and you can not add property attribute to a class – Serge Jul 04 '22 at 17:05
  • Edited, now you can get the clue better – michasaucer Jul 04 '22 at 17:08
  • 1
    I can't reproduce the problem. Here's a complete piece of code that compiles (you have provided incomplete code...) https://gist.github.com/jskeet/b2a3738b426f6deaa0d073126c0dac8f The output is `{"workflow":"test"}`. – Jon Skeet Jul 04 '22 at 17:13
  • 1
    I can't reproduce either, see https://dotnetfiddle.net/UYwntN. Two possibilities: 1) You are mixing version of Json.NET and the `[JsonProperty]` applied to your model is from a different DLL version that the one being used by the call to `JsonConvert.SerializeObject()`. 2) You are actually using System.Text.Json to serialize without realizing it. – dbc Jul 04 '22 at 18:00
  • 1
    A third possibility to add to @dbc is that you are actually using `JsonPropertyName` which is for `System.Text.Json` but you are deserializing using JSON.Net – Charlieface Jul 04 '22 at 19:47
  • Or 4) You have some [global default value for `JsonSerializerSettings`](https://www.newtonsoft.com/json/help/html/DefaultSettings.htm) that is interfering with `JsonPropertyAttribute.PropertyName` somehow. What is the value of `JsonConvert.DefaultSettings`? See: [Json.net global settings](https://stackoverflow.com/q/15066904/3744182). – dbc Jul 04 '22 at 23:40

1 Answers1

2

you examples are not valid, this is working for me

    var a = new A {Workflow="18"};

    var asJson = JsonConvert.SerializeObject(
        a,
        Newtonsoft.Json.Formatting.None,
        new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore
        });
}


public class A
{
    [JsonProperty(PropertyName = "workflow")]
    public string Workflow { get;  set; }
}

result

{"workflow":"18"}

the same is with this class

public class A
{
    [JsonProperty(PropertyName = "workflow")]
    public string Workflow { get; private set; }
    
    public A(string workflow)
    {
        Workflow=workflow;
    }
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Yea but believe me, in my case it is not working. Do you have an idea what to check wehn these properties not working? – michasaucer Jul 04 '22 at 17:32
  • @michasaucer - *Yea but believe me, in my case it is not working.* -- then please [edit](https://stackoverflow.com/posts/72859929/edit) your question to share a [mcve]. That can't be your real code because your class `A` has no public setter and thus `Workflow` could never be set to `"18"`. See [ask]. Serge made a reasonable guess as to how to fix your example to make it work but it could be your real code is what is causing the problem. – dbc Jul 04 '22 at 18:02
  • 1
    @michasaucer The best thing is to post your original. You don't need to post all properties, just a couple would be enough. – Serge Jul 04 '22 at 18:42