JSON.NET serializer incorrectly serializes string as logical

I have a class:

[JsonObject(MemberSerialization.OptIn)] public class UserPreferenceDTO { [JsonProperty] public string Name { get; set; } [JsonProperty] public string Value { get; set; } public static explicit operator UserPreferenceDTO(UserPreference pref) { if (pref == null) return null; return new UserPreferenceDTO { Name = pref.Name, Value = pref.Value }; } public static explicit operator UserPreference(UserPreferenceDTO pref) { if (pref == null) return null; return new UserPreference { Name = pref.Name, Value = pref.Value }; } } 

and controller, for example:

 public HttpResponseMessage Post(int caseid, Guid id, UserPreferenceDTO prefs) { ... } 

NOTE : the controller class is decorated with the [CamelCaseControllerConfig] attribute that does this:

 public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) { var formatter = controllerSettings.Formatters.OfType<JsonMediaTypeFormatter>().Single(); controllerSettings.Formatters.Remove(formatter); formatter = new JsonMediaTypeFormatter { SerializerSettings = { ContractResolver = new CamelCasePropertyNamesContractResolver() } }; controllerSettings.Formatters.Add(formatter); } 

On the client, I send the object as follows:

 { name: "name", value: "Some value" } 

Often value is JS boolean. The problem is that when it reaches the controller, the boolean value is converted to C # boolean ( True / False ) and gated.

For example, sending

 '{ "name": "wantColFit", "value": "false" }' 

becomes:

Boolean value in <code> Value </code> is C # string string boolean

in the .NET controller.

If you look at the definition of the model ( UserPreferenceDTO ) value , take string . So why does a serializer convert a value to a boolean?

I would prefer to keep this value as "true" / "false" when it is saved (which will simplify the analysis of the logical value on the client, since JSON.parse("true") === true , but JSON.parse("True") !== true )

0
c # asp.net-web-api
source share
2 answers

Json Boolean will display in C # bool. Since the name of your field is the same, and on the C # side it is a string that has been converted to a string. According to the intermediate language .NEt, the boolean value "true" will become "True" in .Net.

To solve this problem, you need to pass the boolean value in quotation marks or change the C # property.

 string valueField = ""; [JsonProperty] public string Value { get{ return valueField.ToLower(); } set{ value = valueField; } } 
0
source share

It was not possible to figure out how (and why) Json.Net to deserialize a string such as "true" in C # boolean True , so I just borrowed this meaning: BooleanConverter.js , where ReadJson does:

  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var val = reader.Value.ToString().ToLower().Trim(); if (val == "true" || val == "false") return val; // If we reach here, we're pretty much going to throw an error so let let Json.NET throw it pretty-fied error message. return new JsonSerializer().Deserialize(reader, objectType); } 

and I managed to serialize my model as the correct value of "true" or "false" (rather than "true" or "false" )

If someone can respond to what Json.Net is doing internally to discard the logical lines "true" / "false" , I will accept their answer.

0
source share

All Articles