/// <summary>
        /// Parses/hydrates the envelope from its dehydrated state
        /// </summary>
        /// <param name="text"></param>
        public static SerializationEnvelope Parse(string text)
        {
            Condition.Requires(text).IsNotNullOrEmpty();

            SerializationEnvelope rv = new SerializationEnvelope();

            ((IStringable)rv).Parse(text);
            return(rv);
        }
        /// <summary>
        /// builds a new envelope from a raw object
        /// </summary>
        /// <param name="ser"></param>
        /// <param name="obj"></param>
        public static SerializationEnvelope New(ISerializationUtil ser, object obj)
        {
            Condition.Requires(ser).IsNotNull();
            Condition.Requires(obj).IsNotNull();

            SerializationEnvelope rv = new SerializationEnvelope();

            rv.Id             = ser.Id;
            rv.InstanceType   = obj.GetType();
            rv.SerializedData = ser.Serialize(obj);
            return(rv);
        }
Example #3
0
        public object Deserialize(string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                return(null);
            }

            SerializationEnvelope env = SerializationEnvelope.Parse(data);
            var rv = this.DeserializeEnvelopePayload(env);

            return(rv);
        }
Example #4
0
        /// <summary>
        /// Serializes with the specified serializer
        /// </summary>
        /// <param name="serializerId"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private SerializationEnvelope SerializeToEnvelope(string serializerId, object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            var ser = this.Store.GetAllById <ISerializationUtil>(serializerId).FirstOrDefault();

            Condition.Requires(ser).IsNotNull();

            SerializationEnvelope env = SerializationEnvelope.New(ser, obj);

            return(env);
        }
Example #5
0
        private object DeserializeEnvelopePayload(SerializationEnvelope env)
        {
            if (env == null)
            {
                return(null);
            }

            var ser = this.Store.GetAllById <ISerializationUtil>(env.Id).FirstOrDefault();

            Condition.Requires(ser).IsNotNull();

            var rv = env.DeserializePayload(ser);

            return(rv);
        }
Example #6
0
        /// <summary>
        /// Serializes with the first serializer that works
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private SerializationEnvelope SerializeToEnvelopeWithFirstApplicable(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            var sers = this.Store.GetAll <ISerializationUtil>();

            foreach (var ser in sers)
            {
                try
                {
                    SerializationEnvelope env = SerializationEnvelope.New(ser, obj);
                    return(env);
                }
                catch
                {
                    continue;
                }
            }

            throw new InvalidOperationException("No valid serializers found");
        }