using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public class BigIntegerConverter : JsonConverter
public override bool CanConvert(Type objectType) { return objectType == typeof(BigInteger ); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
var token = JToken.Load(reader);
if (token.Type == JTokenType.String && ((string)(JValue)token).StartsWith("0x"))
var s = "0" + ((string)(JValue)token).Substring(2);
return BigInteger.Parse(s, NumberStyles.HexNumber);
return token.ToObject<BigInteger>();
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
[JsonConverter(typeof(BigIntegerConverter))]
public BigInteger result { get; set; }
public static void Test()
foreach (var json in GetJson())
public static void Test(string json)
Console.WriteLine("Deserializing directly: ");
var result1 = JsonConvert.DeserializeObject<XResult>(json);
Console.WriteLine(JsonConvert.SerializeObject(result1));
Console.WriteLine("\nDeserializing indirectly via JToken: ");
var result2 = JToken.Parse(json).ToObject<XResult>();
Console.WriteLine(JsonConvert.SerializeObject(result2));
static IEnumerable<string> GetJson()
@"{""result"":""0x00FFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000FFFFFFFFFFFFFFF""}",
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
public static void IsTrue(bool value, string message)
throw new AssertionFailedException(message ?? "failed");