示例#1
0
 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 {
     if (value is DateTime)
     {
         double unixTimestamp = UnixDateTimeJsonConverter.DateTimeToUnixSeconds((DateTime)value);
         if (this.convertFromMillis)
         {
             unixTimestamp *= 1000d;
         }
         writer.WriteRawValue(unixTimestamp.ToString());
     }
 }
示例#2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            object value = reader.Value;

            if (reader.Value is long)
            {
                value = (double)(long)value;
            }
            else if (reader.Value is string)
            {
                double doubleValue;
                if (double.TryParse((string)value, out doubleValue))
                {
                    value = doubleValue;
                }
            }
            if (value is double)
            {
                return(this.convertFromMillis
                                        ? UnixDateTimeJsonConverter.UnixMillisToDateTime((double)value)
                                        : UnixDateTimeJsonConverter.UnixSecondsToDateTime((double)value));
            }
            return(null);
        }
示例#3
0
 private static DateTime UnixSecondsToDateTime(double unixSeconds)
 {
     return(UnixDateTimeJsonConverter.UnixMillisToDateTime(unixSeconds * 1000d));
 }