json.net

Change default null value in JSON.NET

不羁的心 提交于 2019-12-13 12:45:59
问题 Is there some way to set what the default representation for null values should be in Json.NET? More specifically null values inside an array. Given the class public class Test { public object[] data = new object[3] { 1, null, "a" }; } Then doing this Test t = new Test(); string json = JsonConvert.SerializeObject(t); Gives {"data":[1,null,"a"]} Is it possible to make it look like this? {"data":[1,,"a"]} Without using string.Replace. 回答1: Figured it out. I had to implement a custom

C# Json.NET Render Flags Enum as String Array

二次信任 提交于 2019-12-13 12:25:06
问题 In a .NET Application, I have a set of values that are stored as a [Flags] enum . I want to serialize these to json, but instead of having the result be an integer, I'd like to get an array of string for the flags that are active. So if I have the following code [Flags] public enum F { Val1 = 1, Val2 = 2, Val4 = 4, Val8 = 8 } public class C { public F Flags { get; set; } } string Serialize() { return JsonConvert.SerializeObject(new C { Flags = F.Val1 | F.Val4 }); } I want the Serialize()

Is the Json.NET JsonSerializer threadsafe?

寵の児 提交于 2019-12-13 12:23:34
问题 I'm trying to reduce the amount of garbage my web service generates, and I noticed that we're creating a new Json.NET JsonSerializer instance for each request. It is not the most lightweight object ever, so I'm wondering if I can just create a single instance and reuse it for all requests. Primarily this requires that it be threadsafe during serialization and deserialization. The documentation doesn't say whether it's threadsafe or not. Inspecting the code it appears that the serialization

Json.net: Serialisation/Deserialisation not working for ISerializable object that has circular reference

旧街凉风 提交于 2019-12-13 12:09:46
问题 Some time back i have reported an issue for which i have got a fix in Json.net 4.5 R11. If my circular referenced property Manager is NULL then serialisation and deserialisation works fine. But when circular reference property Manager is set to NON NULL value then it gets ignored in the serialised string and hence it throws an exception in deserialisation. Json.net issue base says the problem is in your code but i could not figure out. Can somebody help me here? Questions: Is there any

Json.Net IsoDateTimeConverter is not working

无人久伴 提交于 2019-12-13 12:09:29
问题 I have a web application made in asp.net mvc 4 in Global.asax, i added added IsoDateTimeConverter protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add( new IsoDateTimeConverter { DateTimeFormat = "dd-MM-yyyy hh:mm" }); } I have an ActionResult public ActionResult GetDate() { DateTime dateTime = DateTime.Now; return Json(dateTime, JsonRequestBehavior.AllowGet); } However, this

Handling null objects in custom JsonConverter's ReadJson method

我的梦境 提交于 2019-12-13 11:52:20
问题 I've got a Newtonsoft JSON.NET JsonConverter to help deserialize a property whose type is an abstract class. The gist of it looks like this: public class PetConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(Animal); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JObject jsonObject = JObject.Load(reader); if (jsonObject["Lives"] != null) return jsonObject.ToObject

C# JSON.NET remove strings with “,”

可紊 提交于 2019-12-13 09:48:23
问题 My last question to json.net: My file contains some of those guys here: { "type": "06A, 06B, 06C, 06D, 06E", "qt": "6-8-", "id": "06A, 06B, 06C, 06D, 06E6-8-" } Now I want to clean my file and remove all objects where the type contains a comma or ",". I already read this: C# remove json child node using newtonsoft but there is no possibility to remove the object if it contains a special char... I would really appreciate any help! At the moment I have: public void filter() { string sourcePath

Retrieving json data which in C#

佐手、 提交于 2019-12-13 09:47:54
问题 I want to deserialise the following Json. I have tried the following: JSON Data: { "data":[ { "employee":[ { "empdetails":[ { "empid":"40", "empname":"Amit", "empdept":"Director", "empphone":[ { "home":"23432235", "office":"2352353", }] }, { "empid":"54", "empname":"Abhishek", "empdept":"HR", "empphone":[ { "home":"5445457", "office":"34634634", }] }, { "empid":"80", "empname":"Rahul", "empdept":"Finance" }, { "empid":"71", "empname":"Anand", "empdept":"Marketing", "empphone":[ { "home":

Exception occurs when trying to deserialize using Json.Net [duplicate]

拟墨画扇 提交于 2019-12-13 09:38:06
问题 This question already has answers here : Deserializing JSON into one of several C# subclasses (1 answer) Deserializing polymorphic json classes without type information using json.net (5 answers) Closed 5 months ago . I am trying to deserialize the following json using Json.Net into C# classes as defined below. I am having this exception: Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type Viewer.ShapeDto. Type is an interface or abstract class and cannot be

NewtonSoft Json Invalid Cast Exception

二次信任 提交于 2019-12-13 09:28:19
问题 This is related to my question HTTPClient Buffer Exceeded 2G; Cannot write more bytes to the buffer but is different enough that IMO it warrants a separate question. In the other question, I'm trying to figure out how to deal with breaking the 2G request buffer. The idea was to use streaming, but I need to deserialize. In talking to Professor Google, I found that I have to use TextReader to stream/deserialize. so my code for that is: public async Task<API_Json_Special_Feeds.RootObject>