json.net

C# JSON deserialization: can I intercept the deserialization and optionally change the result?

ⅰ亾dé卋堺 提交于 2019-12-04 04:07:36
We've got some JSON that we are deserializing into a strongly-typed object graph in C#. However, we've got one issue: sometimes there is an "empty" value in the JSON (e.g., empty string) in a property that maps to a boolean value in our model. In our case, we know that 100% of the time, we can translate these "blank" values to Boolean false . However, the JSON deserializers I've tried don't know about this (understandably). I've been trying to find a way to intercept the deserialization of each property, and optionally override the output. I.e., if there was an "interceptor" method I could

Anyway to get JsonConvert.SerializeObject to ignore the JsonConverter attribute on a property?

依然范特西╮ 提交于 2019-12-04 04:07:35
问题 I have a class that I cannot change: public enum MyEnum { Item1 = 0, Item2 = 1 } public class foo { [JsonConverter(typeof(StringEnumConverter))] public MyEnum EnumTypes {get; set; } } Somewhere down the line JsonConvert.SerializeObject serializes the object and because of the JsonConverter attribute, it spits out name of the enum value for the foo.EnumTypes rather than the number. Is there anyway to get JsonConvert.SerializeObject to ignore the attribute on the EnumTypes property? 回答1: This

How do I use Json.NET to parse json in PowerShell?

核能气质少年 提交于 2019-12-04 04:03:55
I want to parse JSON in PowerShell but I can't use the new v3 functions that are available in PowerShell. My first thought was to load the JSON.Net assembly and use that to parse the JSON string but it doesn't work as I expect it to. I have this JSON: $json = "{""Name"": ""Apple"", ""Price"": 3.99, ""Sizes"": [ ""Small"", ""Medium"", ""Large""]}" I load the JSON.NET assembly with this code: [Reflection.Assembly]::LoadFile("$currentPath\Newtonsoft.Json.dll”) And tries to parse it with $result = [Newtonsoft.Json.JsonConvert]::DeserializeObject($json) Now I expect that $result["Name"] is Apple

ASP MVC 5 and Json.NET: action return type

心不动则不痛 提交于 2019-12-04 03:46:34
问题 i'm using ASP MVC 5. I have an action in a controller that return a json object: [HttpGet] public JsonResult GetUsers() { return Json(....., JsonRequestBehavior.AllowGet); } Now i want to use the JSON.Net library and i see that in ASP MVC 5 is yet present. In effect i can write using Newtonsoft.Json; without import the library from NuGet. Now i've tried to write: public JsonResult GetUsers() { return JsonConvert.SerializeObject(....); } But i have an error during compilation: I cann't convert

How to properly serialize tuple as key dictionary

别等时光非礼了梦想. 提交于 2019-12-04 03:43:33
问题 I have the following app that shows that the key part of a Dictionary is not sent to JsonConverter , but it is called ToString() on. This is an issue for me as I can't deserialize my Json string . Any ideas? class Program { static void Main(string[] args) { var coll = new Dictionary<Tuple<string,string>, string>(); coll.Add(Tuple.Create("key1", "KEY1"), "Value1"); coll.Add(Tuple.Create("key2", "KEY2"), "Value2"); string json = JsonConvert.SerializeObject(coll); Dictionary<Tuple<string, string

JObject nested property

二次信任 提交于 2019-12-04 03:16:38
I am trying to make a json object like this with JObject: { "input": { "webpage/url": "http://google.com/" } } I can add properties like: JObject job = new JObject( new JProperty("website/url", "http://www.google.com") ); But any time I try to nest an object inside another object so I can have the parent "input" it throws an exception. How do you make nested properties with JObject? Probably the most straightforward way would be: var input = new JObject(); input.Add("webpage/url", "http://google.com"); var obj = new JObject(); obj.Add("input", input); Which gives you: { "input": { "webpage/url

How to (de)serialize a XmlException with Newtonsoft JSON?

て烟熏妆下的殇ゞ 提交于 2019-12-04 03:15:45
问题 This sample code: var json = JsonConvert.SerializeObject(new XmlException("bla")); var exception = JsonConvert.DeserializeObject<XmlException>(json); throws an InvalidCastException in Newtonsoft.Json.dll: Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'System.String' with the following stack trace: at System.Xml.XmlException..ctor(SerializationInfo info, StreamingContext context) at Void .ctor(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization

Keep json:Array attribute when converting XML to JSON to XML

依然范特西╮ 提交于 2019-12-04 02:58:26
I have a piece of XML that looks like <person xmlns:json='http://james.newtonking.com/projects/json' id='1'> <name>Alan</name> <url>http://www.google.com</url> <role json:Array='true'>Admin</role> </person> When I try to serialize it to json string json = JsonConvert.SerializeXmlNode(xml); it ignores namespaces { "person": { "@id": "1", "name": "Alan", "url": "http://www.google.com", "role": [ "Admin" ] } } and when I deserialize it back to xml XmlDocument xml = JsonConvert.DeserializeXmlNode(json) , I get the following: <person id='1'> <name>Alan</name> <url>http://www.google.com</url> <role

JsonConverter how to deserialize to generic object

百般思念 提交于 2019-12-04 02:38:56
问题 I'm sending this structure through webapi: [DataContract] public class PacketData { public enum Opcodes { Hello = 0x00, Close = 0x01, Serial = 0x02, GPIO = 0x04 } [DataMember] public object Data { get; private set; } [DataMember] public Opcodes Opcode { get; private set; } public PacketData(Opcodes opcode, object data) { Data = data; Opcode = opcode; } } And my problem is that I set on server side when I sending it I assign to Data few class ex. CustomClass1, CustomClass2 Now on deserialize I

Json.NET - Default deserialization behavior for a single property in CustomCreationConverter

那年仲夏 提交于 2019-12-04 02:23:18
In the following scenario, how do I get CrazyItemConverter to carry on as usual when it encounters a JSON property that exists in the type I'm deserializing to? I have some JSON that looks like this: { "Item":{ "Name":"Apple", "Id":null, "Size":5, "Quality":2 } } The JSON gets deserialized into a class that looks a whole lot like this: [JsonConverter(typeof(CrazyItemConverter))] public class Item { [JsonConverter(typeof(CrazyStringConverter))] public string Name { get; set; } public Guid? Id { get; set; } [JsonIgnore] public Dictionary<string, object> CustomFields { get { if (_customFields ==