json.net

JSON.net, C#, unable to set default values in data model

雨燕双飞 提交于 2019-12-05 22:46:09
I am using JsonConvert.DeserializeObject(json.ToString()); to deserialise a JSON and populate my fields in a related model. This works well until there is data missing in the JSON. Basically instead of having "key : value" I will just have "key : null". My understanding about the JsonConverter was that it would create an object, in which I should be able to populate fields with a default value either through the constructor (not being called in my case for unknown reasons), or through the tags as mentioned here: Default value for missing properties with JSON.net or here: Why when I deserialize

Parse Json Array objects using Newtonsoft.Json

喜夏-厌秋 提交于 2019-12-05 21:40:57
I have an array of objects like this in json as per below format { Table: [ { userstatus: [ { STATUS: "TRUE", PACK: "UM6MONTHPACK", EXPIRY: "8/15/2014 1:00:03 PM", } ] }, { activeauctions: [ { ISBILLED: "0", AUCTION_ID: "24", AUCTION_NAME: "Swimsuit", } ] }, { upcomingauctions: [ { AUCTION_ID: "4", AUCTION_NAME: "Jacqueline Fernandezs Handbag", SKU: "4_20131120" }, { AUCTION_ID: "4", AUCTION_NAME: "Jacqueline Fernandezs Handbag", SKU: "4_20131120" } ] } ] } I am deserializing like this: var outObject = JsonConvert.DeserializeObject<Table>(response); Here are the classes I am deserializing into

Can I parse json either into a string or another concrete type as object?

心已入冬 提交于 2019-12-05 21:38:57
I'd like to have property of type object that can be either a string or Template type. Is it possible to tell Json.NET to parse something into one of several specified types? class MyClass { public object Template { get; set; } } where Template = "TemplateName" { "Template": "TemplateName" } or Template = new Template() { "Template": { "Name": "OtherTamplate", ... } } UPDATE: I tried to follow @krillgar' advice and create a custom JsonConverter but unfortunatelly the CanConvert method receives only the target type, in this case object . This information is not enough to tell wheter it can be

JObject nested property

烈酒焚心 提交于 2019-12-05 21:04:00
问题 I am trying to make a json object like this with JObject: { "input": { "webpage/url": "http://google.com/" } } I can add properties like: JObject job = new JObject( new JProperty("website/url", "http://www.google.com") ); But any time I try to nest an object inside another object so I can have the parent "input" it throws an exception. How do you make nested properties with JObject? 回答1: Probably the most straightforward way would be: var input = new JObject(); input.Add("webpage/url", "http:

JSON.Net serializing Enums to strings in dictionaries by default - how to make it serialize to int?

烈酒焚心 提交于 2019-12-05 20:42:13
问题 Why does my serialized JSON end up as {"Gender":1,"Dictionary":{"Male":100,"Female":200}} i.e. why do the enums serialize to their value, but when they form they key to the dictionary they are converted to their key? How do I make them be ints in the dictionary, and why isnt this the default behaviour? Id expect the following output {"Gender":1,"Dictionary":{"0":100,"1":200}} thanks public void foo() { var testClass = new TestClass(); testClass.Gender = Gender.Female; testClass.Dictionary.Add

How to create a json string where a property contains a dot (period)?

China☆狼群 提交于 2019-12-05 20:15:49
I'm trying to send an HttpRequest that takes a JSON object like this: { "some.setting.withperiods":"myvalue" } I've been creating anonymous objects for my other requests, but I can't do that with this one since the name contains a dot. I know I can create a class and specify the [DataMember(Name="some.setting.withperiods")] attribute, but there must be a more lightweight solution. There is no "easy" way to achieve this because the . in C# is reserved. However, you could achieve something pretty close by using a dictionary and collection initializer. It's still somewhat isolated, and doesn't

Adding Iso8601TimeSpanConverter to JsonConverter list throws ArrayTypeMismatch exception

给你一囗甜甜゛ 提交于 2019-12-05 19:48:55
I'm using AutoRest to auto generate a c# class for a REST API from a Swagger definition file. The problem is that when the client class initialize methos is executed, it throws an ArrayTypeMismatch exception in the following code: SerializationSettings = new JsonSerializerSettings { Formatting = Formatting.Indented, DateFormatHandling = DateFormatHandling.IsoDateFormat, DateTimeZoneHandling = DateTimeZoneHandling.Utc, NullValueHandling = NullValueHandling.Ignore, ReferenceLoopHandling = ReferenceLoopHandling.Serialize, ContractResolver = new ReadOnlyJsonContractResolver(), Converters = new

Why have warning Newtonsoft.Json.JsonConvert.DeserializeObjectAsync

只谈情不闲聊 提交于 2019-12-05 18:47:07
I'm using JSON.NET version 6.0.1 and here my code below var text = await FileHelper.ReadFileAsync(folderSetting, fileName); var items = await JsonConvert.DeserializeObjectAsync<ObservableCollection<ItemModel>>(text); But my Visual Studio Warning Warning 7 'Newtonsoft.Json.JsonConvert.DeserializeObjectAsync(string)' is obsolete: 'DeserializeObjectAsync is obsolete. Use the Task.Factory.StartNew method to deserialize JSON asynchronously: Task.Factory.StartNew(() => DeserializeObject(value))' The library authors decided that it was not the responsibility of the library to provide asynchronous

NewtonSoft.Json custom JsonConverter deserialize to DateTime not working

心已入冬 提交于 2019-12-05 18:42:48
I am trying to deserialize a Unix timestamp to a DateTime . In my case, I need to do much more checks before I can set a property to DateTime from a timestamp. If I use DateTime from Newtonsoft.Json it deserializes it to UTC time and I need to deserialize it to a specific timezone The problem is that I am not able to get the correct time. It seems like my string to long parsing is failing. If I can get the long unix timestamp, I can get the rest of the logic working I have a class named Alert class Alert { // Some properties [JsonConverter(typeof(UnixTimestampJsonConverter))] public DateTime

Serializing in Azure Function

主宰稳场 提交于 2019-12-05 18:22:51
I'm running into a strange issue with Azure Function Apps. Newtonsoft Json.NET deserialization is not liking the $type annotations. My deserialization code looks like: return JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); The json looks like: { "$type": "Trading.Control.Json.TradingConfig, Trading", "Config": { "$type": "Trading.Control.Json.Config, Trading", "Optimize": false }, "Trading": { "$type": "System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[Trading.Platforms.Credentials, Trading]], mscorlib", ... And