Example #1
0
        private static IDataWriter GetCachedWriter(out IDisposable cache, DataFormat format, Stream stream, SerializationContext context)
        {
            IDataWriter writer;

            if (format == DataFormat.Binary)
            {
                var binaryCache = Cache <BinaryDataWriter> .Claim();

                var binaryWriter = binaryCache.Value;

                binaryWriter.Stream  = stream;
                binaryWriter.Context = context;
                binaryWriter.PrepareNewSerializationSession();

                writer = binaryWriter;
                cache  = binaryCache;
            }
            else if (format == DataFormat.JSON)
            {
                var jsonCache = Cache <JsonDataWriter> .Claim();

                var jsonWriter = jsonCache.Value;

                jsonWriter.Stream  = stream;
                jsonWriter.Context = context;
                jsonWriter.PrepareNewSerializationSession();

                writer = jsonWriter;
                cache  = jsonCache;
            }
            else if (format == DataFormat.Nodes)
            {
                throw new InvalidOperationException("Cannot automatically create a writer for the format '" + DataFormat.Nodes + "', because it does not use a stream.");
            }
            else
            {
                throw new NotImplementedException(format.ToString());
            }

            return(writer);
        }
Example #2
0
        /// <summary>
        /// Serializes the given value to a given stream in the specified format.
        /// </summary>
        /// <typeparam name="T">The type of the value to serialize.</typeparam>
        /// <param name="value">The value to serialize.</param>
        /// <param name="stream">The stream to serialize to.</param>
        /// <param name="format">The format to serialize in.</param>
        /// <param name="unityObjects">A list of the Unity objects which were referenced during serialization.</param>
        /// <param name="context">The context.</param>
        public static void SerializeValue <T>(T value, Stream stream, DataFormat format, out List <UnityEngine.Object> unityObjects, SerializationContext context = null)
        {
            IDisposable cache;
            var         writer = GetCachedWriter(out cache, format, stream, context);

            try
            {
                if (context != null)
                {
                    SerializeValue(value, writer, out unityObjects);
                }
                else
                {
                    using (var con = Cache <SerializationContext> .Claim())
                    {
                        writer.Context = con;
                        SerializeValue(value, writer, out unityObjects);
                    }
                }
            }
            finally
            {
                cache.Dispose();
            }
        }
Example #3
0
 /// <summary>
 /// Serializes the given value using the specified format and returns the result as a byte array.
 /// </summary>
 /// <typeparam name="T">The type of the value to serialize.</typeparam>
 /// <param name="value">The value to serialize.</param>
 /// <param name="format">The format to use.</param>
 /// <param name="unityObjects">A list of the Unity objects which were referenced during serialization.</param>
 /// <param name="context">The context to use.</param>
 /// <returns>A byte array containing the serialized value.</returns>
 public static byte[] SerializeValue <T>(T value, DataFormat format, out List <UnityEngine.Object> unityObjects, SerializationContext context = null)
 {
     using (var stream = CachedMemoryStream.Claim())
     {
         SerializeValue(value, stream.Value.MemoryStream, format, out unityObjects, context);
         return(stream.Value.MemoryStream.ToArray());
     }
 }
Example #4
0
        /// <summary>
        /// Serializes the given value to a given stream in the specified format.
        /// </summary>
        /// <param name="value">The value to serialize.</param>
        /// <param name="stream">The stream to serialize to.</param>
        /// <param name="format">The format to serialize in.</param>
        /// <param name="context">The context.</param>
        public static void SerializeValueWeak(object value, Stream stream, DataFormat format, SerializationContext context = null)
        {
            IDisposable cache;
            var         writer = GetCachedWriter(out cache, format, stream, context);

            try
            {
                if (context != null)
                {
                    SerializeValueWeak(value, writer);
                }
                else
                {
                    using (var con = Cache <SerializationContext> .Claim())
                    {
                        writer.Context = con;
                        SerializeValueWeak(value, writer);
                    }
                }
            }
            finally
            {
                cache.Dispose();
            }
        }