json.net

JSON.NET - How do I include Type Name Handling for primitive C# types that are stored in an array/list as Object type

ε祈祈猫儿з 提交于 2019-12-23 09:48:39
问题 I am using Newtonsoft JSON.NET to serialize/deserialize stuff for me. But I have this list wherein they are of Object type: var list = new List<Object>() { "hi there", 1, 2.33 }; When I serialize that with TypeNameHandling set to TypeNameHandling.All , I was expecting that it will also give a $type for each instance on the list but doesn't seem to be the case. Here is the actual output: { "$type": "System.Collections.Generic.List`1[[System.Object, mscorlib]], mscorlib", "$values": [ "hi there

Json.Net Deserialization Constructor vs. Property Rules

孤人 提交于 2019-12-23 09:34:20
问题 I was troubleshooting a (de)serialization issue with the following class using Json.Net: public class CoinsWithdrawn { public DateTimeOffset WithdrawlDate { get; private set; } public Dictionary<CoinType, int> NumberOfCoinsByType { get; private set; } public CoinsWithdrawn(DateTimeOffset withdrawDate, Dictionary<CoinType, int> numberOfCoinsByType) { WithdrawlDate = withdrawDate; NumberOfCoinsByType = numberOfCoinsByType; } } The problem is that the constructor argument "withdrawDate" is named

Unable to deserialize the JSON result in C#. Input string is not in a correct format error

北城以北 提交于 2019-12-23 09:19:40
问题 I am trying to deserialize a json output to a C# object. JSON result: {"order":{"commission":3.490000,"cost":4.490000,"duration":"day","extended_hours ":false,"fees":0.000000,"class":"equity","price":1.000000,"quantity":1.000000,"r equest_date":"2013-11-26T09:43:17.118Z","result":true,"side":"buy","status":"ok" ,"symbol":"DIS","type":"limit"}} My derived class from JSON: public class Rootobject { public Order Order { get; set; } } public class Order { public float commission { get; set; }

Check JSON and XML is valid? c#

不羁的心 提交于 2019-12-23 09:13:08
问题 Im using newtonsoft json.net http://json.codeplex.com/ and I would like to know ... how to validate json and xml are valid json/xml. how do i verify this? 回答1: Where you want to validate json, on server side or on client side. Assuming you want to do it on server side, try deserializing the json string. if it breaks, then its not a valid json. Use JavaScriptSerializer for deserializing purpose var serializer = new JavaScriptSerializer(); var result = serializer.Deserialize<Dictionary<string,

TypeNameHandling in Newtonsoft requires $type to be the first property? [duplicate]

社会主义新天地 提交于 2019-12-23 09:11:20
问题 This question already has an answer here : Newtonsoft JSON.net deserialization error where fields in JSON change order (1 answer) Closed 3 years ago . I have the following method in my web api public void Put(string id, [FromBody]IContent value) { //Do stuff } I'm using backbone js to send the following JSON to the server using fiddler the value is null: { "id": "articles/1", "heading": "Bar", "$type": "BrickPile.Samples.Models.Article, BrickPile.Samples" } but if I add the $type property

JSON.Net incorrectly serializes a two dimensional array to a one dimension

北慕城南 提交于 2019-12-23 08:33:23
问题 Trying to convert a two dimensional array to a two dimensional JSON.Net array. Is there something wrong with the code below? Or just isn't this supported by JSON.Net? var A = new int[2, 4] { { 1, 1, 1, 1 }, { 2, 2, 2, 2 } }; Console.WriteLine(JsonConvert.SerializeObject(A)); // CONSOLE: [1,1,1,1,2,2,2,2] // // NB. displays a one dimensional array // instead of two e.g. [[1,1,1,1],[2,2,2,2]] 回答1: Javascript doesn't have the notion of a 2D array in the same sense that C# does. In order to get

How do I get the name of a JsonProperty in JSON.Net?

自闭症网瘾萝莉.ら 提交于 2019-12-23 07:59:18
问题 I have a class like so: [JsonObject(MemberSerialization.OptIn)] public class foo { [JsonProperty("name_in_json")] public string Bar { get; set; } // etc. public Dictionary<string, bool> ImageFlags { get; set; } } The JSON is generated from a CSV file originally, each line representing a foo object - it's basically flat, so I need to map certain keys to imageflags. I tried to write a CustomCreationConverter based on the example here. This seems to map the flags fine, but it fails setting

How can I deserialize integer number to int, not to long?

谁说我不能喝 提交于 2019-12-23 07:49:47
问题 I'm using Json.NET to deserialize requests on the server-side. There is something like public object[] Values I need to put in values like 30.0 , 27 , 54.002 , and they need to be double 's and int 's. Json.NET has a deserialization property called FloatParseHandling , but there is no option like IntParseHandling . So the question is how can I deserialize integers to int ? 回答1: Your best bet is to deserialize into a typed model where the model expresses that Values is an int / int[] / etc. In

OutOfMemoryException when reading a string

假如想象 提交于 2019-12-23 07:47:25
问题 I have a C# .NET system that takes a JSON data feed and converts it to an object using the Newtonsoft.Json.JsonConvert.DeserializeObject converter. This process works perfect as long as the JSON string is below a certain size (a few Mb) but as soon as the returned data is large (almost 100Mb) I get the error OutOfMemoryException This code works great for small data: // WebClient ------------------------------------------------------------------ var _client = new System.Net.WebClient(); var

Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Runtime.Serialization.ISafeSerializationData'

こ雲淡風輕ζ 提交于 2019-12-23 07:30:07
问题 I am getting the following exception trying to deserialize a web api call - the message is obscure so I can't understand what is going on - this cast works in other cases not sure what's wrong here: Exception: InnerException: System.Reflection.TargetInvocationException _HResult=-2146232828 _message=Exception has been thrown by the target of an invocation. HResult=-2146232828 IsTransient=false Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: at