json.net

Supporting multiple custom DateTime formats when deserializing with Json.Net

一个人想着一个人 提交于 2020-07-15 07:27:00
问题 I want to support deserializing more than one custom DateTime format with the Newtonsoft Json deserializer, so I am using IsoDateTimeConverter : var serializeSettings = new JsonSerializerSettings(); serializeSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyyMMddTHHmmssZ" }); As the DateTimeFormat property does not accept an array of formats, I tried the following to support multiple custom date formats: var serializeSettings = new JsonSerializerSettings();

Supporting multiple custom DateTime formats when deserializing with Json.Net

佐手、 提交于 2020-07-15 07:26:51
问题 I want to support deserializing more than one custom DateTime format with the Newtonsoft Json deserializer, so I am using IsoDateTimeConverter : var serializeSettings = new JsonSerializerSettings(); serializeSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyyMMddTHHmmssZ" }); As the DateTimeFormat property does not accept an array of formats, I tried the following to support multiple custom date formats: var serializeSettings = new JsonSerializerSettings();

Cast an object to a class represented as Type object [duplicate]

廉价感情. 提交于 2020-07-10 11:33:06
问题 This question already has an answer here : Using System.Type as <T> at runtime when deserializing with Json.Net (1 answer) Closed 8 days ago . Ok i dont even know if the title makes any sense, but i am having difficulty describing what i need to do. So take a look at the example plz. I am doing this: (SportsParent)JsonConvert.DeserializeObject<SportsParent>(jsonObj); But what if i wanted to have the class name "SportsParent" stored in a string, and create a Type object from it. And then use

Deserialize only valid objects by ignoring errors

狂风中的少年 提交于 2020-07-09 04:41:08
问题 Is there a built-in way (or a trick) for parsing only valid objects and ignoring invalid ones? Not a duplicate The question Ignoring an invalid field when deserializing json in Json.Net does not answer my question because it's about a custom serializer for a very specific field of date-time type. I'm seeking a generic solution working for any property and any object. In other words if anything is invalid, just ignore it and contintue to the next entry. As far as json is concerned, the file is

Deserialize only valid objects by ignoring errors

佐手、 提交于 2020-07-09 04:39:13
问题 Is there a built-in way (or a trick) for parsing only valid objects and ignoring invalid ones? Not a duplicate The question Ignoring an invalid field when deserializing json in Json.Net does not answer my question because it's about a custom serializer for a very specific field of date-time type. I'm seeking a generic solution working for any property and any object. In other words if anything is invalid, just ignore it and contintue to the next entry. As far as json is concerned, the file is

How to deserialize string to JObject without scientific notation in C#

橙三吉。 提交于 2020-07-08 11:06:47
问题 I have a string like this: var str = "{'data': {'someProperty': 0.00001}}"; When I parse it to JObject like that var jObject = JObject.Parse(str); My jObject looks like this: {"data": {"someProperty": 1E-05}} I need to get rid of scientific notation so that resulting JObject would look like original json. I managed to do that using later version of Newtonsoft.Json like that: var serializer = new JsonSerializer { FloatParseHandling = FloatParseHandling.Decimal }; using (System.IO.TextReader tr

How to deserialize string to JObject without scientific notation in C#

空扰寡人 提交于 2020-07-08 11:06:25
问题 I have a string like this: var str = "{'data': {'someProperty': 0.00001}}"; When I parse it to JObject like that var jObject = JObject.Parse(str); My jObject looks like this: {"data": {"someProperty": 1E-05}} I need to get rid of scientific notation so that resulting JObject would look like original json. I managed to do that using later version of Newtonsoft.Json like that: var serializer = new JsonSerializer { FloatParseHandling = FloatParseHandling.Decimal }; using (System.IO.TextReader tr

IgnoreMissingMember setting doesn't seem to work with FSharpLu.Json deserializer

两盒软妹~` 提交于 2020-07-03 09:59:11
问题 This is a following to: deserialization issue, with json.net, in F#. I am deserializing some JSON that has an extra, unbound property using FSharpLu.Json. Here is the code: open System open Newtonsoft.Json open Microsoft.FSharpLu.Json type r = { a: int } let a = "{\"a\":3, \"b\":5}" Compact.TupleAsArraySettings.settings.MissingMemberHandling <- MissingMemberHandling.Ignore Compact.deserialize<r> a // doesn't work Despite setting MissingMemberHandling.Ignore it returns a json.net error: Could

C# Enum deserialization with Json.Net: Error converting value to type

老子叫甜甜 提交于 2020-07-03 01:54:09
问题 I'm using Json.NET to serialize/deserialize some JSON APIs. The API response have some integer values that map to an Enum defined in the application. The enum is like this: public enum MyEnum { Type1, Type2, Type3 } and the json API response has the following: { "Name": "abc", "MyEnumValue":"Type1" } sometimes the API returns a value for the MyEnumValue field that's not defined in my enum, like this: { "Name": "abc", "MyEnumValue":"Type4" } That throws an exception: Error converting value

Another failure at deserializing data with discriminated unions, in F#

我怕爱的太早我们不能终老 提交于 2020-06-29 03:47:31
问题 Following a question where the answer provided a working solution to serialize / deserialize discriminated unions (IgnoreMissingMember setting doesn't seem to work with FSharpLu.Json deserializer) I have now a practical case where this fails (although it works in simpler cases). here is the test code: open System.Collections.Generic open Microsoft.FSharpLu.Json open Newtonsoft.Json open Newtonsoft.Json.Serialization // set up the serialization / deserialization based on answer from: // https: