json.net

.Net Core 3.0 TimeSpan deserialization error

孤人 提交于 2020-01-13 19:43:33
问题 I am using .Net Core 3.0 and have the following string which I need to deserialize with Newtonsoft.Json: { "userId": null, "accessToken": null, "refreshToken": null, "sessionId": null, "cookieExpireTimeSpan": { "ticks": 0, "days": 0, "hours": 0, "milliseconds": 0, "minutes": 0, "seconds": 0, "totalDays": 0, "totalHours": 0, "totalMilliseconds": 0, "totalMinutes": 0, "totalSeconds": 0 }, "claims": null, "success": false, "errors": [ { "code": "Forbidden", "description": "Invalid username

Newtonsoft.json serializing and deserializing base/inheirited where classes are from shared projects

血红的双手。 提交于 2020-01-13 19:41:08
问题 So I have two classes like the ones below. They are both in the same namespace and in the same shared project. public class Person{ public string Name{get;set;} } public class EmployedPerson : Person{ public string JobTitle{get;set;} } When I serilize these items into rabbitmq I am serializing as the base class like so: JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple, TypeNameHandling = TypeNameHandling

How do I invoke WriteJson recursively?

二次信任 提交于 2020-01-13 19:10:42
问题 I use Json.Net. When I serialize a Department2 object and WriteJson() is invoked I want it to be recursively invoked with each of the Telephone2 objects like I do in ReadJson() . How do I do that? using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; public interface ISimpleDatabag { string Databag { get; set; } } [JsonConverter(typeof(JsonDataBagCreationConverter<Department2>))] public class Department2 { public Telephone2[] Phones { get; set; } } [JsonConverter(typeof

JSON.Net constructor parameter missing when deserializing

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-13 19:06:59
问题 I have a simple object that I'm roundtripping through JSON. It serializes just fine, but I deserialize it one of the values is set to the default value (0, in this particular case). Why is that? Here's my object: public class CurrencyExchangeRate { public string CurrencyCode { get; private set; } public decimal Rate { get; private set; } public CurrencyExchangeRate(string currencyCode, decimal exchangeRate) { this.CurrencyCode = currencyCode; this.Rate = exchangeRate; } } This serializes to

JsonSerializer behaves not as expected when the specifc class is casted to something else

a 夏天 提交于 2020-01-13 16:30:08
问题 Im trying to migrate from json.net to microsoft's json and found something that behaves very differently. Let's use this simplified example: public interface IName { string Name { get; set; } } public class Person : IName { public string Name { get; set; } public int Age { get; set; } } public void Foo() { IName p = new Person {Age = 4, Name = "Waldo"}; var s1 = System.Text.Json.JsonSerializer.Serialize(p); // --> {"Name":"Waldo"} var s2 = Newtonsoft.Json.JsonConvert.SerializeObject(p); // --

Deserialization in F# vs. C#

假如想象 提交于 2020-01-13 14:59:10
问题 I have the following json: { "data": [ { "timestamp": "2019-11-07T00:23:52.095Z", "symbol": "XBTUSD", "side": "Buy", "size": 1, "price": 9356.5, "tickDirection": "PlusTick", "trdMatchID": "01476235-ad89-1777-9067-8ce6d0e29992", "grossValue": 10688, "homeNotional": 0.00010688, "foreignNotional": 1 } ] } The last 3 fields are optional. When I deserialize it in C#, I do the following: public class Trade { public DateTime Timestamp; public string Symbol; public string Side; public long Size;

Deserialization in F# vs. C#

吃可爱长大的小学妹 提交于 2020-01-13 14:58:10
问题 I have the following json: { "data": [ { "timestamp": "2019-11-07T00:23:52.095Z", "symbol": "XBTUSD", "side": "Buy", "size": 1, "price": 9356.5, "tickDirection": "PlusTick", "trdMatchID": "01476235-ad89-1777-9067-8ce6d0e29992", "grossValue": 10688, "homeNotional": 0.00010688, "foreignNotional": 1 } ] } The last 3 fields are optional. When I deserialize it in C#, I do the following: public class Trade { public DateTime Timestamp; public string Symbol; public string Side; public long Size;

Custom $type value for serialized objects

╄→尐↘猪︶ㄣ 提交于 2020-01-13 13:50:14
问题 We're using Web API with Json.Net using TypeNameHandling = TypeNameHandling.Objects in our serializer settings. This works fine, but we use the type information only client-side, never for deserialization. Our serialized objects look like this: { "$type": "PROJECTNAME.Api.Models.Directory.DtoName, PROJECTNAME.Api", "id": 67, "offices": [{ "$type": "PROJECTNAME.Api.Models.Directory.AnotherDtoName, PROJECTNAME.Api", "officeName": "FOO" }] }, I would like to customize the value in the $type

Json.Net deserialize JSON objects with index as name [duplicate]

心不动则不痛 提交于 2020-01-13 13:12:08
问题 This question already has answers here : How can I parse a JSON string that would cause illegal C# identifiers? (2 answers) Closed 5 years ago . I am attempting to parse JSON from a web service using Json.NET, the web service returns data in the following format: { "0": { "ID": 193, "Title": "Title 193", "Description": "Description 193", "Order": 5, "Hyperlink": "http://someurl.com" }, "1": { "ID": 228, "Title": "Title 228", "Description": "Description 228", "Order": 4, "Hyperlink": "http:/

Deserializing a list of objects with different names in JSON.NET

ぃ、小莉子 提交于 2020-01-13 11:28:32
问题 I'm getting my data from a website which returns a .json format that is quite unfamiliar to me. I've been looking for the solution for a couple of hours, and I must be using the terminology. The json is formatted something like this: [ { "Foo": { "name": "Foo", "size": { "human": "832.73kB", "bytes": 852718 }, "date": { "human": "September 18, 2017", "epoch": 1505776741 }, } }, { "bar": { "name": "bar", "size": { "human": "4.02MB", "bytes": 4212456 }, "date": { "human": "September 18, 2017",