Documentation Coverage Report
Current view: top level - Cqrs.MongoDB/Serialisers - TypeSerialiser.cs Hit Total Coverage
Version: 2.2 Artefacts: 4 4 100.0 %
Date: 2018-08-07 15:04:50

          Line data    Source code
       1             : #region Copyright
       2             : // // -----------------------------------------------------------------------
       3             : // // <copyright company="Chinchilla Software Limited">
       4             : // //   Copyright Chinchilla Software Limited. All rights reserved.
       5             : // // </copyright>
       6             : // // -----------------------------------------------------------------------
       7             : #endregion
       8             : 
       9             : using System;
      10             : using MongoDB.Bson;
      11             : using MongoDB.Bson.Serialization;
      12             : 
      13             : namespace Cqrs.MongoDB.Serialisers
      14             : {
      15             :         /// <summary>
      16             :         /// A <see cref="IBsonSerializer"/> that stores <see cref="Type"/> information as well.
      17             :         /// </summary>
      18             :         public class TypeSerialiser : IBsonSerializer<Type>
      19           1 :         {
      20             :                 #region Implementation of IBsonSerializer
      21             : 
      22             :                 /// <summary>
      23             :                 /// Deserialises a value, first reading the <see cref="Type"/> information from the provide <paramref name="context"/>.
      24             :                 /// </summary>
      25             :                 /// <param name="context">The deserialisation context.</param>
      26             :                 /// <param name="args">The deserialisation arguments.</param>
      27             :                 /// <returns>
      28             :                 /// A deserialised value.
      29             :                 /// </returns>
      30           1 :                 public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
      31             :                 {
      32             :                         return ((IBsonSerializer<Type>) this).Deserialize(context, args);
      33             :                 }
      34             : 
      35             :                 /// <summary>
      36             :                 /// Serializes a value.
      37             :                 /// </summary>
      38             :                 /// <param name="context">The serialisation context.</param>
      39             :                 /// <param name="args">The serialisation arguments.</param>
      40             :                 /// <param name="value">The value.</param>
      41           1 :                 public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Type value)
      42             :                 {
      43             :                         if (value == null)
      44             :                         {
      45             :                                 context.Writer.WriteNull();
      46             :                         }
      47             :                         else
      48             :                         {
      49             :                                 context.Writer.WriteString((value).AssemblyQualifiedName);
      50             :                         }
      51             :                 }
      52             : 
      53             :                 /// <summary>
      54             :                 /// Deserialises a value.
      55             :                 /// </summary>
      56             :                 /// <param name="context">The deserialisation context.</param>
      57             :                 /// <param name="args">The deserialisation arguments.</param>
      58             :                 /// <returns>
      59             :                 /// A deserialised value.
      60             :                 /// </returns>
      61             :                 Type IBsonSerializer<Type>.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
      62             :                 {
      63             :                         var bsonReader = context.Reader;
      64             :                         BsonType currentBsonType = bsonReader.CurrentBsonType;
      65             :                         if (currentBsonType == BsonType.Null)
      66             :                                 return null;
      67             : 
      68             :                         string assemblyQualifiedName;
      69             :                         switch (currentBsonType)
      70             :                         {
      71             :                                 case BsonType.Document:
      72             :                                         bsonReader.ReadStartDocument();
      73             :                                         assemblyQualifiedName = context.Reader.ReadString();
      74             :                                         // This moves the pointer forward.
      75             :                                         bsonReader.ReadBsonType();
      76             :                                         bsonReader.ReadEndDocument();
      77             :                                         break;
      78             :                                 default:
      79             :                                         assemblyQualifiedName = bsonReader.ReadString();
      80             :                                         break;
      81             :                         }
      82             :                         return Type.GetType(assemblyQualifiedName);
      83             :                 }
      84             : 
      85             :                 /// <summary>
      86             :                 /// Serializes a value.
      87             :                 /// </summary>
      88             :                 /// <param name="context">The serialization context.</param>
      89             :                 /// <param name="args">The serialization arguments.</param>
      90             :                 /// <param name="value">The value.</param>
      91           1 :                 public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
      92             :                 {
      93             :                         Serialize(context, args, (Type)value);
      94             :                 }
      95             : 
      96             :                 /// <summary>
      97             :                 /// Gets the type of the value.
      98             :                 /// </summary>
      99             :                 public Type ValueType { get; protected set; }
     100             : 
     101             :                 #endregion
     102             :         }
     103             : }

Generated by: LCOV version 1.12