Exemplo n.º 1
0
        //public static object Deserialize(BsonDocument document)
        //{
        //    throw new NotImplementedException();
        //}

        public static T Deserialize <T>(BsonDocument document)
        {
            BsonDocumentReader reader = new BsonDocumentReader(document, new BsonDocumentReaderSettings()
            {
                GuidRepresentation = GuidRepresentation.Standard
            });
            ICommodityReader commodityReader = new BsonCommodityReader(reader);

            return(CommoditySerializer.Deserialize <T>(commodityReader));
        }
Exemplo n.º 2
0
        //public static BsonDocument Serialize(object instance)
        //{
        //    throw new NotImplementedException();
        //}

        public static BsonDocument Serialize <T>(T instance)
        {
            BsonDocument       doc    = new BsonDocument();
            BsonDocumentWriter writer = new BsonDocumentWriter(doc, new BsonDocumentWriterSettings()
            {
                GuidRepresentation = GuidRepresentation.Standard
            });
            ICommodityWriter commodityWriter = new BsonCommodityWriter(writer);

            CommoditySerializer.Serialize(commodityWriter, instance);
            return(doc);
        }
Exemplo n.º 3
0
        public void Start()
        {
            CommodityTypeResolver typeResolver = new CommodityTypeResolver();

            // Find all Types with attribute CommoditySerializable
            var allTypesWithSerializableAttribute = AppDomain.CurrentDomain.GetAssemblies().FindTypesWithAttribute <CommoditySerializableAttribute>();

            foreach (var type in allTypesWithSerializableAttribute)
            {
                CommoditySerializableAttribute attr = type.GetCustomAttribute <CommoditySerializableAttribute>();
                typeResolver.Register(type, attr.UniqueId);
            }
            typeResolver.Register(typeof(object), "object");

            // Commodity Serializers
            // 1) Type (priority 100)
            CommoditySerializer.RegisterSerializer((f) => typeof(Type).IsAssignableFrom(f), new TypeSerializer(typeResolver), 100);
            CommoditySerializer.RegisterSerializer((f) => f == typeof(String), new StringSerializer(), 100);
            CommoditySerializer.RegisterSerializer((f) => f.IsValueType, new ValueTypeSerializer(), 100);

            // 2) *
            CommoditySerializer.RegisterSerializer((f) => true, new DefaultSerializer());
        }
Exemplo n.º 4
0
 public ICommodityWriter WriteResolved(Type nominalType, object value)
 {
     CommoditySerializer.Serialize(this, nominalType, value);
     return(this);
 }