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

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

How to get DateTime from the internet?

Here is code sample that you can use to retrieve time from NIST Internet Time Service var client = new TcpClient(“time.nist.gov”, 13); using (var streamReader = new StreamReader(client.GetStream())) { var response = streamReader.ReadToEnd(); var utcDateTimeString = response.Substring(7, 17); var localDateTime = DateTime.ParseExact(utcDateTimeString, “yy-MM-dd HH:mm:ss”, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); }

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)