/// <summary>
        /// Writes the type.
        /// </summary>
        /// <param name="ms">Ms.</param>
        /// <param name="type">Type.</param>
        private void WriteType(MemoryStream ms, Type type)
        {
            var typeName  = TypeHelper.BuildTypeName(type);
            var typeArray = Encoding.UTF8.GetBytes(typeName);

            var len = typeArray.Length;

            // BinaryWrite Int32
            ms.WriteByte((byte)len);
            ms.WriteByte((byte)(len >> 8));
            ms.WriteByte((byte)(len >> 16));
            ms.WriteByte((byte)(len >> 24));
            // BinaryWrite String
            ms.Write(typeArray, 0, len);
        }
Beispiel #2
0
        /// <summary>
        /// Serializes the object.
        /// </summary>
        /// <returns>The object.</returns>
        /// <param name="obj">Object.</param>
        public ArraySegment <byte> SerializeObject(object obj)
        {
            var typeName = TypeHelper.BuildTypeName(obj.GetType());

            using (var ms = new MemoryStream())
                using (var jw = new Utf8JsonWriter(ms))
                {
                    jw.WriteStartArray();
                    jw.WriteStringValue(typeName);

                    JsonSerializer.Serialize(jw, obj, jsonSerializerOption);

                    jw.WriteEndArray();

                    jw.Flush();

                    return(new ArraySegment <byte>(ms.ToArray(), 0, (int)ms.Length));
                }
        }
        /// <summary>
        /// Serializes the object.
        /// </summary>
        /// <returns>The object.</returns>
        /// <param name="obj">Object.</param>
        public ArraySegment <byte> SerializeObject(object obj)
        {
            var typeName = TypeHelper.BuildTypeName(obj.GetType()); // Get type

            using (var ms = new MemoryStream())
                using (var tw = new StreamWriter(ms))
                    using (var jw = new JsonTextWriter(tw))
                    {
                        jw.WriteStartArray();              // [
                        jw.WriteValue(typeName);           // "type",
                        jsonSerializer.Serialize(jw, obj); // obj

                        jw.WriteEndArray();                // ]

                        jw.Flush();

                        return(new ArraySegment <byte>(ms.ToArray(), 0, (int)ms.Length));
                    }
        }