json.net

How to serialize a large JSON object directly to HttpResponseMessage stream?

青春壹個敷衍的年華 提交于 2019-12-05 13:10:37
Is there any way to stream a large JSON object directly to the HttpResponseMessage stream? Here is my existing code: Dictionary<string,string> hugeObject = new Dictionary<string,string>(); // fill with 100,000 key/values. Each string is 32 chars. HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StringContent( content: JsonConvert.SerializeObject(hugeObject), encoding: Encoding.UTF8, mediaType: "application/json"); Which works fine for smaller objects. However, the process of calling JsonConvert.SerializeObject() to convert the object into a

How to handle error in Json.Net parsing

被刻印的时光 ゝ 提交于 2019-12-05 12:44:14
I'm using Json.Net for Json deserialization. On occasion the Json string I read is not correct (which I can't fix since I don't produce it). In particular, at one specific place, where there should be a string, sometimes there is a serialized object instead. Json.Net then complains, not surprisingly, about finding an object where it expected a string. I found I can intercept this by using Error in JsonSerializerSettings and also make Json.Net ignore the problem by setting ErrorContext.Handled . But I want to do even more. If I could look at the serialised object I could figure out what the

Deserialize from json where can be single T object or array of T into List<T> [duplicate]

有些话、适合烂在心里 提交于 2019-12-05 12:24:24
This question already has answers here : How to handle both a single item and an array for the same property using JSON.net (7 answers) Closed 5 years ago . I have the code like this: var json = GetJsonData(path); JObject event_dates_data = JObject.Parse(json); var event_dates_list = JObject.Parse(event_dates_data["document"]["date"].ToString()); var event_dates = JsonConvert.DeserializeObject<List<EventDate>>(event_dates_list.ToString()); Json may contains an array objects (for example "date:[{},{},{}]") or only one (for example "date:{}") Json looks like that: { "document": { "result":

Deserialize json objects and transform inner objects to a string value?

雨燕双飞 提交于 2019-12-05 11:53:53
I have a webservice returning json data. I have no control over the server side generated json. I'm deserializing json like this: JsonConvert.DeserializeObject<OuterObject>(jsonString); The problem is there are inner objects embedded (with a whole lot of more nested inner objects). I have no interest in modeling them in my application. The json-data goes like this: { id : "xyz", name : "Some Object", properties : { prop_1 : "foo", prop_2 : "bar" }, inner_object : { id : "abc$1", name : "Inner Object Name", .... // a whole lot of stuff here // with more embedded objects .... } } I would like to

JSON.NET Visual Studio 2008 and .NET 3.5 Compact Framework

妖精的绣舞 提交于 2019-12-05 11:51:18
Can I use JSON.NET in Visual Studio 2008 with .NET 3.5 Compact Framework? And how can I install/Configure it in the IDE? I've searched the internet but could not find it. I found this NuGet Support for Visual Studio 2008 to try install JSON.NET through NuGet but could not get it working. The result of this tutorial was an error Unable to find package 'your.package.name' : I don't think NuGet will help you here. Json.NET removed support for the .NET 3.5 Compact Framework in release 4.0.1 , as is stated in the 4.0.1 release notes : Other major changes in this release are two new builds and the

How do I deserialize a JSON array that contains only values?

走远了吗. 提交于 2019-12-05 11:41:11
I'm getting this result from a web function. ["767,20150221122715,121053103,14573465,1,7,302", "767,20150221122756,121053165,14573375,1,0,302", "767,20150221122840,121053498,14572841,1,12,124"] Usually Json have PropertyName: Value But this have an array of strings, and each string have the values separated by comma. I know what each value position mean. I try using JsonConvert.DeserializeObject but couldn't make it work. string deserializedProduct = JsonConvert.DeserializeObject<string>(json); //and List<string> deserializedProduct = JsonConvert.DeserializeObject<string>(json); I can parse

JSON.Net not calling CanConvert for collection item?

醉酒当歌 提交于 2019-12-05 11:13:01
I have a converter that I only want used when deserializing. So I set CanWrite to false, which works fine and everything Serializes fine. The Json string then contains an object graph within which there is a SantaClauseCollection with an array of SantaClause items and a $type indicating they are concrete type SantaClause. However, when it encounters a collection of SantaClaus while deserializing, it never calls CanConvert(I have a break point and see the SantaClausCollection, hit F5 to continue, which should then hit the break point again when encountering an item in the collection of

How do I add comments to Json.NET output?

删除回忆录丶 提交于 2019-12-05 10:07:22
问题 Is there a way I can automatically add comments to the serialised output from JSON.Net? Ideally I'd imagine it's something similar to the following: public class MyClass { [JsonComment("My documentation string")] public string MyString { get; set; } } or (even better if annotations can be avoided): public class MyClass { /// <summary> /// My documentation string /// </summary> public string MyString { get; set; } } that would produce: { //My documentation string "MyString": "Test" } The

Why can't use LINQ methods on JObject?

允我心安 提交于 2019-12-05 09:56:56
Newtonsoft.Json.Linq.JObject implemented IEnumerable<T> , and not explicit implementation, but why can't do this: using System.Linq; ... var jobj = new JObject(); var xxx = jobj.Select(x => x); //error foreach(var x in jobj) { } //no error WHY? Thanks. JObject implements both IEnumerable<KeyValuePair<string, JToken>> and IEnumerable<JToken> (by inheriting from JContainer ). Thus you cannot use LINQ (e.g. Select ) directly since it doesn't know which of the enumerables to 'extend'. Thus you need to cast first: ((IEnumerable<KeyValuePair<string, JToken>>) jobj).Select(x => x) or: jobj.Cast

Order of fields when serializing the derived class in JSON.NET

亡梦爱人 提交于 2019-12-05 09:52:49
问题 Consider these two classes: public Class Base { public string Id {get; set;} public string Name {get; set;} public string LastName {get; set;} } And the derived class: public Class Derived : Base { public string Address {get; set;} public DateTime DateOfBirth {get; set;} } When serializing the Derived class using Json.Net : Derived record = new Derived record(); {// Initialize here...} JsonConvert.SerializeObject(record); By default, the properties of the Derived class appear first: {