///<summary> /// Reads the JSON representation of the object. ///</summary> ///<param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param> ///<param name="objectType">Type of the object.</param> ///<param name="existingValue">The existing value of object being read.</param> ///<param name="serializer">The calling serializer.</param> ///<returns>The object value.</returns> publicoverrideobjectReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { returnnull; }
///<summary> /// 判斷此物件是否符合自訂規則的設定型別,符合即可進行轉換 ///</summary> ///<param name="objectType">Type of the object.</param> ///<returns><c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.</returns> publicoverrideboolCanConvert(Type objectType) { var result = _types.Any(t => t == objectType); return result; } }
///<summary> /// Reads the JSON representation of the object. ///</summary> ///<param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param> ///<param name="objectType">Type of the object.</param> ///<param name="existingValue">The existing value of object being read.</param> ///<param name="serializer">The calling serializer.</param> ///<returns>The object value.</returns> publicoverrideobjectReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { // reader Json 讀取出來的類型為 Date 格式就進行轉換 // 當序列化使用此規則時,此處可以省略,因為會沒有 Date 格式 if (reader.TokenType == JsonToken.Date) { return ((DateTime)existingValue).ToLocalTime(); }
if (DateTime.TryParse(existingValue.ToString(), out DateTime dateValue)) { return dateValue.ToLocalTime(); }
///<summary> /// Initializes a new instance of the <see cref="SampleModelConverter"/> class. ///</summary> ///<param name="types">The types.</param> publicSampleModelConverter(params Type[] types) { _types = types; }
///<summary> /// Writes the JSON representation of the object. ///</summary> ///<param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter" /> to write to.</param> ///<param name="value">The value.</param> ///<param name="serializer">The calling serializer.</param> publicoverridevoidWriteJson(JsonWriter writer, objectvalue, JsonSerializer serializer) { Type type = value.GetType(); if (type == typeof(DateTime)) { var date = (DateTime)value; serializer.Serialize(writer, date.ToLocalTime().ToString("yyyy-MM-dd hh:mm:ss")); } }
///<summary> /// Reads the JSON representation of the object. ///</summary> ///<param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param> ///<param name="objectType">Type of the object.</param> ///<param name="existingValue">The existing value of object being read.</param> ///<param name="serializer">The calling serializer.</param> ///<returns>The object value.</returns> publicoverrideobjectReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Date) { return ((DateTime)existingValue).ToLocalTime(); } if (DateTime.TryParse(existingValue.ToString(), out DateTime dateValue)) { return dateValue.ToLocalTime(); } returnnull; }
///<summary> /// 判斷此物件是否符合自訂規則的設定型別,符合即可進行轉換 ///</summary> ///<param name="objectType">Type of the object.</param> ///<returns><c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.</returns> publicoverrideboolCanConvert(Type objectType) { var result = _types.Any(t => t == objectType); return result; } }
timeZoneIdName.Dump(); ReadOnlyCollection<TimeZoneInfo> list = TimeZoneInfo.GetSystemTimeZones(); foreach (var zone in list) { if (!timeZoneIdName.ContainsKey(zone.Id)) timeZoneIdName.Add(zone.Id, zone.DisplayName); }