Convert an int to bool with Json.Net [duplicate]

Ended up creating a converter: public class BoolConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteValue(((bool)value) ? 1 : 0); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return reader.Value.ToString() == “1”; } public override bool CanConvert(Type objectType) { return objectType == typeof(bool); } … Read more

Using a custom type discriminator to tell JSON.net which type of a class hierarchy to deserialize

In case you are still looking, here is an example: http://james.newtonking.com/archive/2011/11/19/json-net-4-0-release-4-bug-fixes.aspx This will allow you to create a table based mapping: public class TypeNameSerializationBinder : SerializationBinder { public TypeNameSerializationBinder(Dictionary<Type, string> typeNames = null) { if (typeNames != null) { foreach (var typeName in typeNames) { Map(typeName.Key, typeName.Value); } } } readonly Dictionary<Type, string> typeToName = … Read more

Random number in long range, is this the way?

Why don’t you just generate two random Int32 values and make one Int64 out of them? long LongRandom(long min, long max, Random rand) { long result = rand.Next((Int32)(min >> 32), (Int32)(max >> 32)); result = (result << 32); result = result | (long)rand.Next((Int32)min, (Int32)max); return result; } Sorry, I forgot to add boundaries the first … Read more

How to get DateTime from the internet?

For environments where port 13 is blocked, time from NIST can be web scraped as below, public static DateTime GetNistTime() { DateTime dateTime = DateTime.MinValue; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“http://nist.time.gov/actualtime.cgi?lzbc=siqm9b”); request.Method = “GET”; request.Accept = “text/html, application/xhtml+xml, */*”; request.UserAgent = “Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)”; request.ContentType = “application/x-www-form-urlencoded”; request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); //No … Read more