json.net

Deserializing with Json.NET: Requiring a property/key to be present

对着背影说爱祢 提交于 2019-12-18 08:25:29
问题 When using Json.NET to deserialize a JSON string into an object, how do I require a key/property to be present in the JSON stirng, but allow for NULL values? For example: Lets say I have a class/object... [DataContract] public class Car { [DataMember(IsRequired = true)] public string Vin {get; set;} [DataMember(IsRequired = true)] public string Color {get; set;} public string Description {get; ;set} } In the above example, the VIN and Color are required, and an exception would be thrown if

Deserializing with Json.NET: Requiring a property/key to be present

点点圈 提交于 2019-12-18 08:23:46
问题 When using Json.NET to deserialize a JSON string into an object, how do I require a key/property to be present in the JSON stirng, but allow for NULL values? For example: Lets say I have a class/object... [DataContract] public class Car { [DataMember(IsRequired = true)] public string Vin {get; set;} [DataMember(IsRequired = true)] public string Color {get; set;} public string Description {get; ;set} } In the above example, the VIN and Color are required, and an exception would be thrown if

Merge two JTokens into one

烈酒焚心 提交于 2019-12-18 08:09:13
问题 How can I merge these two JTokens into one single JToken. This sounds like it should be simple, but can't get my way around it. { "data":[ { "ID":"53a1862000404a304942546b35519ba3", "name":"Private Approval Process: Draft Document CPL", "objCode":"ARVPTH" }] } { "data":[ { "ID":"53a1838200401324eb1ec66562e9d77d", "name":"Private Approval Process: Draft Document CPL", "objCode":"ARVPTH" }] } Thanks for the help! This is what I have tried so far: I started by assigning the first object to a

Why when I deserialize with JSON.NET ignores my default value?

坚强是说给别人听的谎言 提交于 2019-12-18 07:41:22
问题 I'm using JSON.NET as my main serializer. This is my model, look that I've setted some JSONProperties and a DefaultValue . public class AssignmentContentItem { [JsonProperty("Id")] public string Id { get; set; } [JsonProperty("Qty")] [DefaultValue(1)] public int Quantity { get; set; } } When I serialize a List<AssignmentContentItem> , it doing a good work: private static JsonSerializerSettings s = new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore,

Newtonsoft.Json JsonConvert To Datatable

只谈情不闲聊 提交于 2019-12-18 07:19:49
问题 I have a code like this, DataTable dt = new DataTable(); string data = "{\"ProductId\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77],\"ProductName\":[\"Chai\",\"Chang\",\"Aniseed Syrup\",\"Chef Anton's Cajun Seasoning\",\"Chef Anton's Gumbo Mix\",\"Grandma's Boysenberry Spread\",\"Uncle Bob's Organic Dried Pears\",\

Json serialization for Object Data Type

◇◆丶佛笑我妖孽 提交于 2019-12-18 07:17:14
问题 public class MyClass { public object BidAutoObject { get; set; } public bool IsApplied { get; set; } } I have a class like above and I am creating the Json string from the above Class object. Property "BidAutoObject " is of type "object". The object may be "CxDays" or "AMPM". It is setting dynamically. I am using newtonsoft.JsonConvert.SerializeObject to serialize the C# object to Json string. The outpout of Json serialization is like "BidAutoObject": { "IsSun": true, "IsMon": false, "IsTue":

Access custom attributes of .NET class inside custom json converter

断了今生、忘了曾经 提交于 2019-12-18 07:08:29
问题 In my project, I have written a custom json converter to trim the white-spaces present in the string property. Here is an example of the typical class we will use, public class Candidate { public string CandidateName { get; set; } } Here is my custom json converter public class StringSanitizingConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(string); } public override object ReadJson(JsonReader reader, Type objectType, object

Deserialize the JSON where the values are field names with JSON.NET

眉间皱痕 提交于 2019-12-18 06:57:07
问题 I have a very undesirable situation which requires me to deserialize the JSON where the values are field names with JSON.NET. Assuming that I have the following JSON which is very properly structured: { "name": "tugberk", "roles": [ { "id": "1", "name": "admin" }, { "id": "2", "name": "guest" } ] } It's very easy to deserialize this with JSON.NET to a CLR object: class Program { static void Main(string[] args) { var camelCaseSettings = new JsonSerializerSettings { ContractResolver = new

Serializing XNA Rectangle with Json.NET

允我心安 提交于 2019-12-18 06:26:10
问题 I'm using Json.NET First look at this: using System.Drawing; string json = JsonConvert.SerializeObject(new Rectangle(-3,6,32,32), Formatting.Indented); Console.WriteLine(json); Rectangle deserializedRectangle = JsonConvert.DeserializeObject<Rectangle>(json); Everything works as expected. The console output is: "3, 6, 32, 32" But when I want to do the same thing with the XNA Rectangle, I get an error. (just replaced the old using with this "using Microsoft.Xna.Framework;") The console output

How can I deserialize JSON containing delimited JSON?

点点圈 提交于 2019-12-18 05:57:18
问题 I have a problem with deserializing a Json-string to an object. This is a sample json i receive from a webservice: { "GetDataResult": "{ \"id\":1234, \"cityname\":\"New York\", \"temperature\":300, }" } And I have a class CityData that looks like this [JsonObject("GetDataResult")] public class CityData { [JsonProperty("id")] public int Id { get; set; } [JsonProperty("cityname")] public string CityName { get; set; } [JsonProperty("temperature")] public int Temperature { get; set; } } I try to