json.net

Ignore a property when deserializing using Json.Net with ItemRequired = Required.Always

人走茶凉 提交于 2019-12-17 19:12:07
问题 I'm using Json.Net to serialize and deserialize classes to json and back. I added to a class marked with [JsonObject(ItemRequired = Required.Always)] (or Required.Always ) a new get-only property. That results in the following JsonSerializationException : Newtonsoft.Json.JsonSerializationException: Required property '<PropertyName>' not found in JSON I thought marking that property with JsonIgnore would solve the issue, but that doesn't work. How can I tell Json.Net that this property should

How to use Newtonsoft.Json as default in Asp.net Core Web Api?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 18:48:00
问题 I am new to ASP.Net Web Api Core . I have been using ASP.Net MVC for past few years and I always have written an ActionFilter and used JSON.Net for Serializing data into JSON . So, in that way I replaced Microsoft's JavaScript Serializer (which is slower than JSON.Net ) with JSON.Net (it is claimed to be 400% faster). How to do all this in ASP.Net Web Api Core ? Where to change the default formattor ? Note: Please feel free to ask if you have any questions. Thanks 回答1: ASP.NET Core already

Serialize only interface properties to JSON with Json.net

ぃ、小莉子 提交于 2019-12-17 18:27:30
问题 With a simple class/interface like this public interface IThing { string Name { get; set; } } public class Thing : IThing { public int Id { get; set; } public string Name { get; set; } } How can I get the JSON string with only the "Name" property (only the properties of the underlying interface) ? Actually, when i make that : var serialized = JsonConvert.SerializeObject((IThing)theObjToSerialize, Formatting.Indented); Console.WriteLine(serialized); I get the full object as JSON (Id + Name);

Checking for empty/null JToken in a JObject

大憨熊 提交于 2019-12-17 17:42:09
问题 I have the following... JArray clients = (JArray)clientsParsed["objects"]; foreach (JObject item in clients.Children()) { // etc.. SQL params stuff... command.Parameters["@MyParameter"].Value = JTokenToSql(item["thisParameter"]); } JTokenToSql looks like this... public static object JTokenToSql(JToken obj) { if (obj.Any()) return (object)obj; else return (object)DBNull.Value; } I have tried ((JObject)obj).Count also.. But doesn't seem to be working. 回答1: To check whether a property exists on

Get the object is null using JSON in WCF Service

孤街浪徒 提交于 2019-12-17 17:15:51
问题 I have a WCF service and I created a website to test my service. However the object is always null in the method. I used the if statement to check the object and it return false for the null object. Would someone solve me how to fixed it. There is my service: [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "BookInfo/")] BookingResult Booking(BookInfo

JSON data returned from WCF service contains escape characters

六眼飞鱼酱① 提交于 2019-12-17 17:13:07
问题 I have a working WCF - WPF application working, however I'm looking for some optimization. Below is my code where a WCF restful service is exposing a JSON array, and a WPF UI is receiving it without any problem. WCF: public clsStatus[] GetAllStatus() { DataTable dt = new DataTable(); List<clsStatus> lstGetAllStatus = new List<clsStatus>(); try { dt = // My Data Table foreach (DataRow dr in dt.Rows) { dcStatus objGetAllStatus = new clsStatus(); objGetAllStatus.Id = Convert.ToInt32(dr["Id"]);

How to apply a general rule for remapping all property names when serializing with Json.NET?

故事扮演 提交于 2019-12-17 17:04:28
问题 When deserializing a Json object into a .Net type , if the field names don't match up I found you can decorate your type 's properties with [JsonProperty(PropertyName = "name")] This is fine and dandy for a couple of properties that don't match up, but is there a way to set a convention or rule? Json { "Job": [ { "Job #": "1", "Job Type": "A", } ] } C# [JsonProperty(PropertyName = "Job Type")] public string JobType { get; set; } [JsonProperty(PropertyName = "Job #")] public string JobNumber {

Deserializing JSON with numbers as keys

ε祈祈猫儿з 提交于 2019-12-17 17:03:03
问题 EDIT: I figured out how to get each key, now the problem is looping through each collection. Solution at bottom! I'm trying to parse a JSON payload that has the following format: { "version": "1.1", "0": { "artist": "Artist 1", "title": "Title 1" }, "1": { "artist": "Artist 2", "title": "Title 2" }, ... "29": { "artist": "Artist 30", "title": "Title 30" } } I don't need the version key, so I'm ignoring it while coding my classes. This is what I have so far: public class Song { public string

JSON.net (de)serialize untyped property

梦想与她 提交于 2019-12-17 16:59:39
问题 Suppose I have a class like this: public class Example { public int TypedProperty { get; set; } public object UntypedProperty { get; set; } } And suppose someone comes along and writes: var example = new Example { TypedProperty = 5, UntypedProperty = Guid.NewGuid() } If I serialize this with JsonConvert.SerializeObject(example) , I get { "TypedProperty": 5, "UntypedProperty": "24bd733f-2ade-4374-9db6-3c9f3d97b12c" } Ideally, I'd like to get something like this: { "TypedProperty": 5,

How to use class fields with System.Text.Json.JsonSerializer?

不想你离开。 提交于 2019-12-17 16:57:14
问题 I recently upgraded a solution to be all .NET Core 3 and I have a class that requires the class variables to be fields. This is a problem since the new System.Text.Json.JsonSerializer doesn't support serializing nor deserializing fields but only handles properties instead. Is there any way to ensure that the two final classes in the example below have the same exact values? using System.Text.Json; public class Car { public int Year { get; set; } // does serialize correctly public string Model