public static AggregationExpression Translate(TranslationContext context, MethodCallExpression expression) { var method = expression.Method; var arguments = expression.Arguments; if (method.IsOneOf(__dateTimeSubtractWithDateTimeMethods)) { Expression thisExpression, valueExpression; if (method.IsStatic) { thisExpression = arguments[0]; valueExpression = arguments[1]; } else { thisExpression = expression.Object; valueExpression = arguments[0]; } var thisTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, thisExpression); var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression); AstExpression unit, startOfWeek; IBsonSerializer serializer; if (method.IsOneOf(__dateTimeSubtractWithUnitMethods)) { var unitExpression = arguments[2]; var unitConstant = unitExpression.GetConstantValue <DateTimeUnit>(containingExpression: expression); unit = unitConstant.Unit; if (unitConstant is WeekWithStartOfWeekDayTimeUnit unitConstantWithStartOfWeek) { startOfWeek = unitConstantWithStartOfWeek.StartOfWeek; } else { startOfWeek = null; } serializer = BsonInt64Serializer.Instance; } else { unit = "millisecond"; startOfWeek = null; serializer = new TimeSpanSerializer(representation: BsonType.Int64, units: TimeSpanUnits.Milliseconds); } AstExpression timezone = null; if (method.IsOneOf(__dateTimeSubtractWithTimezoneMethods)) { var timezoneExpression = arguments.Last(); var timezoneTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, timezoneExpression); timezone = timezoneTranslation.Ast; } var ast = AstExpression.DateDiff(valueTranslation.Ast, thisTranslation.Ast, unit, timezone, startOfWeek); return(new AggregationExpression(expression, ast, serializer)); } throw new ExpressionNotSupportedException(expression); }
/// <summary> /// Returns a serializer that can be used to serialize and object /// of type <paramref name="objectType"/>. /// <note> /// TODO: Add support for caching. /// </note> /// </summary> /// <param name="objectType">The type of object to be serialized.</param> /// <param name="ctx">The serialization context.</param> public virtual ISerializer Build(Type objectType, SerializationContext ctx) { if (objectType == null) { return(null); } ISerializer s; if (typeof(Calendar).IsAssignableFrom(objectType)) { s = new CalendarSerializer(ctx); } else if (typeof(ICalendarComponent).IsAssignableFrom(objectType)) { s = typeof(CalendarEvent).IsAssignableFrom(objectType) ? new EventSerializer(ctx) : new ComponentSerializer(ctx); } else if (typeof(ICalendarProperty).IsAssignableFrom(objectType)) { s = new PropertySerializer(ctx); } else if (typeof(CalendarParameter).IsAssignableFrom(objectType)) { s = new ParameterSerializer(ctx); } else if (typeof(string).IsAssignableFrom(objectType)) { s = new StringSerializer(ctx); } else if (objectType.GetTypeInfo().IsEnum) { s = new EnumSerializer(objectType, ctx); } else if (typeof(TimeSpan).IsAssignableFrom(objectType)) { s = new TimeSpanSerializer(ctx); } else if (typeof(int).IsAssignableFrom(objectType)) { s = new IntegerSerializer(ctx); } else if (typeof(Uri).IsAssignableFrom(objectType)) { s = new UriSerializer(ctx); } else if (typeof(ICalendarDataType).IsAssignableFrom(objectType)) { s = _mDataTypeSerializerFactory.Build(objectType, ctx); } // Default to a string serializer, which simply calls // ToString() on the value to serialize it. else { s = new StringSerializer(ctx); } return(s); }
public void SerializeAndDeserializeToTheSameObject(TimeSpan originalValue) { Serializer <TimeSpan> serializer = new TimeSpanSerializer(); byte[] serializedValue = serializer.Serialize(originalValue); TimeSpan deserializedValue = serializer.Deserialize(serializedValue); Assert.Equal(originalValue, deserializedValue); }
// constructors /// <summary> /// Initializes a new instance of the <see cref="SystemProfileReadWriteLockStatisticsSerializer"/> class. /// </summary> public SystemProfileReadWriteLockStatisticsSerializer() { var timeSpanSerializer = new TimeSpanSerializer(BsonType.Double, TimeSpanUnits.Microseconds); RegisterMember("DatabaseReadLock", "r", timeSpanSerializer); RegisterMember("GlobalReadLock", "R", timeSpanSerializer); RegisterMember("DatabaseWriteLock", "w", timeSpanSerializer); RegisterMember("GlobalWriteLock", "W", timeSpanSerializer); }
public WKTSerializers(ISerializerStorage serializerStorage, IValueTypeObjectsDictionary objectCache) { Boolean = new BooleanSerializer(serializerStorage); Byte = new ByteSerializer(serializerStorage); Char = new CharSerializer(serializerStorage); DateTime = new DateTimeSerializer(serializerStorage); DateTimeOffset = new DateTimeOffsetSerializer(serializerStorage); Decimal = new DecimalSerializer(serializerStorage); Double = new DoubleSerializer(serializerStorage); Guid = new GuidSerializer(serializerStorage); Int16 = new Int16Serializer(serializerStorage); Int32 = new Int32Serializer(serializerStorage); Int64 = new Int64Serializer(serializerStorage); Object = new ObjectSerializer(serializerStorage); SByte = new SByteSerializer(serializerStorage); Single = new SingleSerializer(serializerStorage); String = new StringSerializer(serializerStorage, objectCache); TimeSpan = new TimeSpanSerializer(serializerStorage); UInt16 = new UInt16Serializer(serializerStorage); UInt32 = new UInt32Serializer(serializerStorage); }
public void ZeroTimeSpan_Test() { var result = new TimeSpanSerializer().SerializeToString(TimeSpan.Zero); Assert.IsTrue("P0D".Equals(result, StringComparison.Ordinal)); }
public void ShouldThrowIfDeserializingLessThanEightBytes(byte[] data) { Serializer <TimeSpan> serializer = new TimeSpanSerializer(); Assert.Throws <ArgumentOutOfRangeException>(() => serializer.Deserialize(data)); }
public void ShouldThrowIfDeserializingNull() { Serializer <TimeSpan> serializer = new TimeSpanSerializer(); Assert.Throws <ArgumentNullException>(() => serializer.Deserialize(null)); }