json.net

Avoiding CamelCasePropertyNamesContractResolver for a specific method

青春壹個敷衍的年華 提交于 2019-12-05 08:58:40
I have web api controllers and I am using this method of configuration in WebApiConfig file for camel casing my results for all controllers. var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); Now I have a controller's method which is giving data to Angularjs translation provider and all translation strings are not in camelcase in my html, thats why I need results of that method to be not in CamelCase. How to avoid camel casing serialization behavior for this specific controllers' method

How to exclude specific type from json serialization

对着背影说爱祢 提交于 2019-12-05 08:53:09
I am logging all requests to my WCF web services, including the arguments, to the database. This is the way I do it: create a class WcfMethodEntry which derives from PostSharp's aspect OnMethodBoundaryAspect, annotate all WCF methods with WcfMethodEntry attribute, in the WcfMethodEntry I serialize the method arguments to json with the JsonConvert.SerializeObject method and save it to the database. This works ok, but sometimes the arguments are quite large, for example a custom class with a couple of byte arrays with photo, fingerprint etc. I would like to exclude all those byte array data

How do I serialize IHtmlString to JSON with Json.NET?

你离开我真会死。 提交于 2019-12-05 08:46:41
I have a field containing raw HTML published via JSON that was recently converted from a string to an IHtmlString. When that change happened, the field went from being a JSON string to being an empty object and a bunch of things consuming that JSON started blowing up. // When it was a string... { someField: "some <span>html</span> string" } // When it became an IHtmlString... { someField: { } } Ignoring any arguments against raw HTML in JSON since it is moot for this project, how do I get the expected string in my JSON serialization? patridge Background Both Json.NET and the default .NET

Odata Controller: How to convert Odata response to C# object at client

风格不统一 提交于 2019-12-05 08:41:39
We have got a Odata response as below: "{\r\n \"@odata.context\":\"http://localhost/ApplicationService/model/$metadata#Edm.String\",\"value\":\"{\\\"Messages\\\":[\\\"message 1\\\",\\\"message 2\\\",\\\"message 3\\\",\\\"message 4\\\"],\\\"IsValidEntity\\\":false}\"\r\n}" Now say we have a class: public class myValidationResult { public myValidationResult() { Messages = new List<string>(); } public List<string> Messages { get; set; } public bool IsValidEntity { get; set; } } This class used in MyOdataController class as below: public class MyODataController : ODataController { [Authorize(Roles

specify a value can be a string or null with json schema

允我心安 提交于 2019-12-05 08:33:30
问题 Hopefully this isn't obvious to others because I find the docs at http://json-schema.org/ to be lacking in finer details. I'm getting a block of json with some properties that can be null or a string. How do you specify, in a json schema (to be parsed by json.NET's JsonSchema.Parse method), that a value can be of type null or type string? Is there something simple I'm missing like supplying an array for the type? For example; "member_region": { "type": [ "string", null ] } // this throws an

Why is Newtonsoft.Json so prone to assembly version conflicts?

痴心易碎 提交于 2019-12-05 08:27:16
I have noticed that we often get assembly version conflicts in our project, and 90% of the time it's Newtonsoft.Json at the bottom. There are many questions on SO specifically for Newtonsoft.Json conflicts - the infamous "Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0" for example. Searching for "assembly 'Newtonsoft.Json, Version=6.0.0.0" gives 37 questions - a lot of them highly upvoted. Or this one about 4.5.0.0 . Is there any explanation why this happen so often with that library specifically instead of others, and why is it such a consistent source of assembly version

JSON.NET JToken Keys Are Case Sensitive?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 08:20:51
I'm having to perform some custom deserialization with JSON.NET and I just found that it's treating the key values in a JToken as case sensitive. Here's some code: public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); JToken version = token["version"]; string ver = version.ToObject<string>(); return new MyVersion(ver); } The version variable is null even though the json contains a version element at the top level, it's just in upper case: { "VERSION" : "1.0", "NAME" : "john smith" } Is there

Setting a custom json converter for DocumentDb

假如想象 提交于 2019-12-05 08:07:01
I am using a typed DocumentQuery to read documents from a collection of an Azure DocumentDb. from f in client.CreateDocumentQuery<MyModel>(Collection.SelfLink) select f Because I do not find a way how I can set the neccesarry custom json converter, it throws this exeption: Could not create an instance of type AbstractObject. Type is an interface or abstract class and cannot be instantiated. Usually you do something like this to make it work: var settings = new JsonSerializerSettings(); settings.Converters.Add(new MyAbstractConverter()); client.SerializerSettings = settings; DocumentClient

Web API project won't run when its deployed - Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0

拟墨画扇 提交于 2019-12-05 08:05:31
I keep getting this error when I deploy my MVC 5 WEB API project: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. I have followed this and re-install the NuGet package "Update-Package Newtonsoft.Json -Reinstall" but it didn't work. Does anyone have any idea here as to what could be going wrong? Check the reference to the Newtonsoft.json DLL and make sure it has not automatically chosen a version outside of the packages folder in your solution. My VS 2013

How to deserialize JSON array with “root” element for each object in array using Json.NET?

笑着哭i 提交于 2019-12-05 07:57:36
I have following JSON string: [ { "Person" : { "Name" : "John", "Gender" : "male" } }, { "Person" : { "Name" : "John", "Gender" : "male" } } ] (As you may notice unfortunately I have a sort of "root" element for each object in the array. Without this "root" element the task becomes quite trivial.) I have to deserialize it into a list of Person class: class Person { public string Name { get; set; } public string Gender { get; set; } } ... List<Person> ListPersons() { return JsonConvert.DeserializeObject<List<Person>>(jsonString); } Is it possible to do with Json.NET without creating wrapper