Last active
August 29, 2015 14:17
-
-
Save aj-r/39b79672a9c5a5b55037 to your computer and use it in GitHub Desktop.
Newtonsoft.Json IConvertible excepetion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Serialization; | |
namespace Sandbox | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
var settings = new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Populate }; | |
try | |
{ | |
var obj = JsonConvert.DeserializeObject<MyClass>("{}", settings); | |
} | |
catch (Exception ex) | |
{ | |
var s = ex.ToString(); | |
} | |
} | |
public struct MyStruct : IConvertible | |
{ | |
public MyStruct(int value) | |
{ | |
this.value = value; | |
} | |
private readonly int value; | |
public int Value | |
{ | |
get { return value; } | |
} | |
public TypeCode GetTypeCode() | |
{ | |
return TypeCode.Object; | |
} | |
public bool ToBoolean(IFormatProvider provider) | |
{ | |
throw new InvalidCastException(); | |
} | |
public byte ToByte(IFormatProvider provider) | |
{ | |
throw new InvalidCastException(); | |
} | |
public char ToChar(IFormatProvider provider) | |
{ | |
throw new InvalidCastException(); | |
} | |
public DateTime ToDateTime(IFormatProvider provider) | |
{ | |
throw new InvalidCastException(); | |
} | |
public decimal ToDecimal(IFormatProvider provider) | |
{ | |
return value; | |
} | |
public double ToDouble(IFormatProvider provider) | |
{ | |
return value; | |
} | |
public short ToInt16(IFormatProvider provider) | |
{ | |
return (short)value; | |
} | |
public int ToInt32(IFormatProvider provider) | |
{ | |
return value; | |
} | |
public long ToInt64(IFormatProvider provider) | |
{ | |
return value; | |
} | |
public sbyte ToSByte(IFormatProvider provider) | |
{ | |
return (sbyte)value; | |
} | |
public float ToSingle(IFormatProvider provider) | |
{ | |
return value; | |
} | |
public string ToString(IFormatProvider provider) | |
{ | |
return value.ToString(); | |
} | |
public object ToType(Type conversionType, IFormatProvider provider) | |
{ | |
throw new InvalidCastException(); | |
} | |
public ushort ToUInt16(IFormatProvider provider) | |
{ | |
return (ushort)value; | |
} | |
public uint ToUInt32(IFormatProvider provider) | |
{ | |
return (uint)value; | |
} | |
public ulong ToUInt64(IFormatProvider provider) | |
{ | |
return (ulong)value; | |
} | |
} | |
public class MyClass | |
{ | |
public MyStruct? NullableProperty { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment