using System; namespace ln.bson.mapper { public abstract class BsonMapping { public Type SourceType { get; } public BsonMapping(Type sourceType) { SourceType = sourceType; } public abstract bool TryMapValue(BsonMapper mapper, object o, out BsonValue bsonValue); public abstract bool TryMapValue(BsonMapper mapper, BsonValue bsonValue, out object o); public class Int32 : BsonMapping { public Int32() : base(typeof(int)) { } public override bool TryMapValue(BsonMapper mapper, object o, out BsonValue bsonValue) { bsonValue = new BsonInt32((int)o); return true; } public override bool TryMapValue(BsonMapper mapper, BsonValue bsonValue, out object o) { if (bsonValue is BsonInt32 bsonInt32) { o = bsonInt32.Value; return true; } o = null; return false; } } public class Int64 : BsonMapping { public Int64() : base(typeof(long)) { } public override bool TryMapValue(BsonMapper mapper, object o, out BsonValue bsonValue) { bsonValue = new BsonInt64((long)o); return true; } public override bool TryMapValue(BsonMapper mapper, BsonValue bsonValue, out object o) { if (bsonValue is BsonInt64 bsonInt64) { o = bsonInt64.Value; return true; } o = null; return false; } } public class String : BsonMapping { public String() : base(typeof(string)) { } public override bool TryMapValue(BsonMapper mapper, object o, out BsonValue bsonValue) { bsonValue = new BsonString((string)o); return true; } public override bool TryMapValue(BsonMapper mapper, BsonValue bsonValue, out object o) { if (bsonValue is BsonString bson) { o = bson.Value; return true; } o = null; return false; } } public class Double : BsonMapping { public Double() : base(typeof(double)) { } public override bool TryMapValue(BsonMapper mapper, object o, out BsonValue bsonValue) { bsonValue = new BsonDouble((double)o); return true; } public override bool TryMapValue(BsonMapper mapper, BsonValue bsonValue, out object o) { if (bsonValue is BsonDouble bsonDouble) { o = bsonDouble.Value; return true; } o = null; return false; } } public class DateTime : BsonMapping { public DateTime() : base(typeof(System.DateTime)) { } public override bool TryMapValue(BsonMapper mapper, object o, out BsonValue bsonValue) { bsonValue = new BsonDateTime((System.DateTime)o); return true; } public override bool TryMapValue(BsonMapper mapper, BsonValue bsonValue, out object o) { if (bsonValue is BsonDateTime bsonDateTime) { o = bsonDateTime.Value; return true; } o = null; return false; } } public class Boolean : BsonMapping { public Boolean() : base(typeof(bool)) { } public override bool TryMapValue(BsonMapper mapper, object o, out BsonValue bsonValue) { bsonValue = (bool)o ? BsonBoolean.True : BsonBoolean.False; return true; } public override bool TryMapValue(BsonMapper mapper, BsonValue bsonValue, out object o) { if (bsonValue is BsonBoolean bson) { o = bson.Value; return true; } o = null; return false; } } public class Guid : BsonMapping { public Guid() : base(typeof(System.Guid)) { } public override bool TryMapValue(BsonMapper mapper, object o, out BsonValue bsonValue) { bsonValue = new BsonGuid((System.Guid)o); return true; } public override bool TryMapValue(BsonMapper mapper, BsonValue bsonValue, out object o) { if (bsonValue is BsonGuid bson) { o = bson.GuidValue; return true; } o = null; return false; } } public class Binary : BsonMapping { public Binary() : base(typeof(byte[])) { } public override bool TryMapValue(BsonMapper mapper, object o, out BsonValue bsonValue) { bsonValue = new BsonBinary(BsonBinarySubType.Generic, (byte[])o); return true; } public override bool TryMapValue(BsonMapper mapper, BsonValue bsonValue, out object o) { if (bsonValue is BsonBinary bson) { o = bson.Value; return true; } o = null; return false; } } } }