0

In my Startup.cs i have set up my default serializer with options like this

.AddNewtonsoftJson(options =>
{
    options.UseMemberCasing();
    options.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
    options.SerializerSettings.FloatParseHandling = Newtonsoft.Json.FloatParseHandling.Double;
    options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
    options.SerializerSettings.TraceWriter = new NLogTraceWriter();                    
    options.SerializerSettings.Culture = CultureInfo.InvariantCulture;                    

    JsonConvert.DefaultSettings = () => options.SerializerSettings;
});

and expected anything returned from Action or coming to Action as a parameter to be serialized (or deserialized) using this setting, which is not the case. This setting only gets used when i explicitly use JsonConvert.SerializeObject() or JsonConvert.DeserializeObject() method. Is there anything else I need to set up so I don't have to use these functions explicitly in every action?

Can anyone please enlighten me on what am I doing wrong?

dbc
  • 104,963
  • 20
  • 228
  • 340
Martin
  • 404
  • 1
  • 5
  • 15
  • Also, please share a [mcve] showing the JSON you need to parse that uses commas for a decimal separator. [How to localize when JSON-serializing?](https://stackoverflow.com/q/4721143/3744182) and [deserialize number with comma in json to decimal](https://stackoverflow.com/q/42401151/3744182) look most closely related. – dbc Oct 07 '21 at 18:27
  • From the [docs](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonConvert_DefaultSettings.htm), `JsonConvert.DefaultSettings` are used by [`JsonConvert`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConvert.htm),[`JToken.FromObject`](https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JToken_FromObject.htm), [`ToObject`](https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JToken_ToObject__1.htm), and [`JsonSerializer.CreateDefault()`](https://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_JsonSerializer_CreateDefault.htm). – dbc Oct 08 '21 at 15:10
  • `JsonConvert.DefaultSettings` are not used by [`JsonSerializer.Create()`](https://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_JsonSerializer_Create.htm) or `new JsonSerializer()`. So if some 3rd party app just does `JsonSerializer.Create(myOwnDefaultSettings)` then serializes from and to a stream they won't be taken into account -- which is I believe what asp.net does, and is why it has its own default settings `options.SerializerSettings`. – dbc Oct 08 '21 at 15:11
  • If `options.SerializerSettings` are being ignored by asp.net itself when serializing or deserializing *anything returned from Action or coming to Action as a parameter* then I think we need to see a [mcve] for that. – dbc Oct 08 '21 at 15:13

0 Answers0