json.net

How to throw an exception when JsonConstructor parameter name doesn't match JSON?

落爺英雄遲暮 提交于 2021-02-10 07:09:09
问题 I'm deserializing a bunch of C# readonly structures (which have their constructors marked by [JsonConstructor] ), and I'm trying to fail early if any JSON that I receive is malformed. Unfortunately, if there is a naming discrepancy between the constructor parameter and the input JSON, the parameter just gets assigned a default value. Is there a way that I could get an exception instead, so these defaults don't accidentally "pollute" the rest of my business logic? I have tried playing with

How to throw an exception when JsonConstructor parameter name doesn't match JSON?

ε祈祈猫儿з 提交于 2021-02-10 07:08:33
问题 I'm deserializing a bunch of C# readonly structures (which have their constructors marked by [JsonConstructor] ), and I'm trying to fail early if any JSON that I receive is malformed. Unfortunately, if there is a naming discrepancy between the constructor parameter and the input JSON, the parameter just gets assigned a default value. Is there a way that I could get an exception instead, so these defaults don't accidentally "pollute" the rest of my business logic? I have tried playing with

How to deserialize JSON with Json.Net and Xamarin with SubTypes?

一个人想着一个人 提交于 2021-02-10 06:57:21
问题 I'm trying to deserialize this json: { "teaser": [{ "id": "...", "type": "category", "url": "https:...", },{ "id": "...", "type": "brand", "url": "https:...", "videoCount": 1, },{ "id": "...", "type": "video", "url": "https:...", "headline": "...", }] } It has a list of teasers whereby each teaser is different depending on its type. These would be my objects: public class StartPage { public IList<Teaser> Teaser { get; set; } } public abstract class Teaser { public string Id { get; set; }

Newtonsoft.Json - DeserializeObject throws when deserializing custom type: Error converting value “somestring” to type CustomType

和自甴很熟 提交于 2021-02-10 05:14:41
问题 I have a custom type: [TypeConverter(typeof(FriendlyUrlTypeConverter))] public class FriendlyUrl : IEquatable<FriendlyUrl>, IConvertible { public FriendlyUrl() { _friendlyUrl = string.Empty; } public FriendlyUrl(string value) { value = value.Trim(); if (!FriednlyUrlValidator.Validate(value)) throw new FriendlyUrlValidationException("Invalid value for FrienlyUrl"); _friendlyUrl = value; } public static implicit operator FriendlyUrl(string friendlyUrlValue) { if (friendlyUrlValue != "" &&

Newtonsoft.Json - DeserializeObject throws when deserializing custom type: Error converting value “somestring” to type CustomType

 ̄綄美尐妖づ 提交于 2021-02-10 05:09:04
问题 I have a custom type: [TypeConverter(typeof(FriendlyUrlTypeConverter))] public class FriendlyUrl : IEquatable<FriendlyUrl>, IConvertible { public FriendlyUrl() { _friendlyUrl = string.Empty; } public FriendlyUrl(string value) { value = value.Trim(); if (!FriednlyUrlValidator.Validate(value)) throw new FriendlyUrlValidationException("Invalid value for FrienlyUrl"); _friendlyUrl = value; } public static implicit operator FriendlyUrl(string friendlyUrlValue) { if (friendlyUrlValue != "" &&

Newtonsoft.Json - DeserializeObject throws when deserializing custom type: Error converting value “somestring” to type CustomType

懵懂的女人 提交于 2021-02-10 05:04:56
问题 I have a custom type: [TypeConverter(typeof(FriendlyUrlTypeConverter))] public class FriendlyUrl : IEquatable<FriendlyUrl>, IConvertible { public FriendlyUrl() { _friendlyUrl = string.Empty; } public FriendlyUrl(string value) { value = value.Trim(); if (!FriednlyUrlValidator.Validate(value)) throw new FriendlyUrlValidationException("Invalid value for FrienlyUrl"); _friendlyUrl = value; } public static implicit operator FriendlyUrl(string friendlyUrlValue) { if (friendlyUrlValue != "" &&

Deserialize JSON which can have different objects under same property name

最后都变了- 提交于 2021-02-09 11:12:12
问题 I'm using JSON.NET to deserialize JSON responses from HTTP queries, but I'm stuck with an issue. That's because the response can send two types of object under same property, as shown below: 1st case sample (most common): { "type": "myType", "tid": 4, "action": "myAction", "method": "myMethod", "result": { "success": true, "total": 4, "records": [ { "id": 4, "nome": "PRIMEIRO NOME", "sigla": "PN" }, { "id": 1974, "nome": "SEGUNDO NOME", "sigla": "SN" }, { "id": 2584, "nome": "TERCEIRO NOME",

Deserialize JSON which can have different objects under same property name

偶尔善良 提交于 2021-02-09 11:05:56
问题 I'm using JSON.NET to deserialize JSON responses from HTTP queries, but I'm stuck with an issue. That's because the response can send two types of object under same property, as shown below: 1st case sample (most common): { "type": "myType", "tid": 4, "action": "myAction", "method": "myMethod", "result": { "success": true, "total": 4, "records": [ { "id": 4, "nome": "PRIMEIRO NOME", "sigla": "PN" }, { "id": 1974, "nome": "SEGUNDO NOME", "sigla": "SN" }, { "id": 2584, "nome": "TERCEIRO NOME",

Change in JSON generated for a System.Data.DataTable in a .NET Core project vs .NET Framework

余生长醉 提交于 2021-02-08 15:44:14
问题 The program below generates different JSON when run in a .NET Core project vs a .NET Framework app. Code class Program { internal static readonly MediaTypeFormatter DefaultFormatter = new JsonMediaTypeFormatter { UseDataContractJsonSerializer = false, SerializerSettings = { NullValueHandling = NullValueHandling.Ignore, DateTimeZoneHandling = DateTimeZoneHandling.Utc, DateFormatHandling = DateFormatHandling.IsoDateFormat } }; private static DataTable BuildTestDataTable() { var testDataTable =

Json.Net serialization of IEnumerable with TypeNameHandling=auto

核能气质少年 提交于 2021-02-08 13:11:19
问题 According to Json.Net documentation all IEnumerable types should be serialized as json array. So I expect the following class: public class MyClass { public IEnumerable<string> Values { get; set; } } to be serialized as: { "Values": [] } The problem is that when I use TypeNameHandling=Auto I get: { "Values": { "$type": "System.String[], mscorlib", "$values": [] } } I need TypeNameHandling=Auto for other properties but I expect IEnumerable to use the default serialization. Other types ( IList