json.net

Json.NET custom serialization/deserialization of a third party type

爷,独闯天下 提交于 2019-12-21 17:28:13
问题 I want to converts Vectors of the OpenTK library to and from JSON. The way I thought it worked is just making a custom JsonConverter, so I did this: class VectorConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(Vector4); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var obj = JToken.Load(reader); if (obj.Type == JTokenType.Array) { var arr = (JArray)obj; if (arr

Json.NET says “operation may destabilize the runtime” under .NET 4, but not under .NET 3.5

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 16:47:14
问题 This code: namespace ConsoleApplication3 { class Program { static void Main(string[] args) { var client = new WebClient(); client.Headers.Add("User-Agent", "Nobody"); var response = client.DownloadString(new Uri("http://www.hanselman.com/smallestdotnet/json.ashx")); var j = JsonConvert.DeserializeObject<SmallestDotNetThing>(response); } public class SmallestDotNetThing { public DotNetVersion latestVersion { get; set; } public List<DotNetVersion> allVersions { get; set; } public List

How to serialize static properties in JSON.NET without adding [JsonProperty] attribute

雨燕双飞 提交于 2019-12-21 15:22:27
问题 Is it possible to serialize static properties with JSON.NET without adding [JsonProperty] attribute to each property. Example class: public class Settings { public static int IntSetting { get; set; } public static string StrSetting { get; set; } static Settings() { IntSetting = 5; StrSetting = "Test str"; } } Expected result: { "IntSetting": 5, "StrSetting": "Test str" } Default behavior skips static properties: var x = JsonConvert.SerializeObject(new Settings(), Formatting.Indented); 回答1:

How to serialize static properties in JSON.NET without adding [JsonProperty] attribute

不想你离开。 提交于 2019-12-21 15:20:55
问题 Is it possible to serialize static properties with JSON.NET without adding [JsonProperty] attribute to each property. Example class: public class Settings { public static int IntSetting { get; set; } public static string StrSetting { get; set; } static Settings() { IntSetting = 5; StrSetting = "Test str"; } } Expected result: { "IntSetting": 5, "StrSetting": "Test str" } Default behavior skips static properties: var x = JsonConvert.SerializeObject(new Settings(), Formatting.Indented); 回答1:

JSON.Net - cannot deserialize the current json object (e.g. {“name”:“value”}) into type 'system.collections.generic.list`1

泄露秘密 提交于 2019-12-21 12:42:05
问题 I have a JSON like { "40": { "name": "Team A vs Team B", "value": { "home": 1, "away": 0 } }, "45": { "name": "Team A vs Team C", "value": { "home": 2, "away": 0 } }, "50": { "name": "Team A vs Team D", "value": { "home": 0, "away": 2 } } } So it's kind of list of matches. And I have the class to deserialize it into: public class Match { [JsonProperty(PropertyName = "name")] public string Name { get; set; } [JsonProperty(PropertyName = "value")] public Value Values { get; set; } } public

Deserialize JSON with json.NET into C# dynamic

陌路散爱 提交于 2019-12-21 11:02:23
问题 I have following problem: I have a json file that looks like this { "Path": { "FirstPath": "/1/2/text()" } } If I parse this JSON-File with Newtonsoft like this dynamic dyn = JObject.Parse(json); or this dynamic dyn = JsonConvert.DeserializeObject(json); I get a dynamic object that needs to be used like this dyn.Path.FirstPath.Value How can I get rid of the Value stuff? All of my objects in the JSON end up being a string. I don't want to always write ".Value" at the end if it is not necessary

Deserialize JSON with json.NET into C# dynamic

橙三吉。 提交于 2019-12-21 11:02:19
问题 I have following problem: I have a json file that looks like this { "Path": { "FirstPath": "/1/2/text()" } } If I parse this JSON-File with Newtonsoft like this dynamic dyn = JObject.Parse(json); or this dynamic dyn = JsonConvert.DeserializeObject(json); I get a dynamic object that needs to be used like this dyn.Path.FirstPath.Value How can I get rid of the Value stuff? All of my objects in the JSON end up being a string. I don't want to always write ".Value" at the end if it is not necessary

Change the JSON serialization settings of a single ASP.NET Core controller

强颜欢笑 提交于 2019-12-21 07:29:46
问题 I'm having two controller controllers: ControllerA and ControllerB . The base class of each controller is Controller . The ControllerA needs to return JSON in the default format (camelCase). The ControllerB needs to return data in a different JSON format: snake_case. How can I implement this in ASP.NET Core 2.1? I've tried the startup with: services .AddMvc() .AddJsonOptions(options => { options.SerializerSettings.Converters.Add(new StringEnumConverter()); options.SerializerSettings

How to use a JsonConverter with JToken.ToObject<>() method?

半城伤御伤魂 提交于 2019-12-21 07:27:13
问题 I'm reading a large JSON file successfully into JObjects. One of the types I'm deserializing into has a property of type System.Drawing.Color. The JSON for this property has an integer value representing the color. When I try to do a ToObject() I get Error converting value 16711680 to type 'System.Drawing.Color'. The solution seems to be a simple JsonConverter that can convert from an integer to a Color but I can't find out how to use the converter with an existing JObject. Am I missing

JSON Date and DateTime serialisation in c# & newtonsoft

[亡魂溺海] 提交于 2019-12-21 07:24:08
问题 We are sending JSON to an API defined by swagger that some properties are DateTime in the format yyyy-MM-ddThh:mm:ss.000Z (the milliseconds must be 3 digits or it fails validation at the endpoint) and some are Date (no time) properties. I have seen many messages saying use the formatters like this: var jsonSettings = new JsonSerializerSettings(); jsonSettings.DateFormatString = "yyyy-MM-ddThh:mm:ss.000Z"; //try .fffZ too var jsonObject= Newtonsoft.Json.JsonConvert.DeserializeObject