Example #1
0
        /// <summary>
        /// Reads an object from the given stream.
        /// </summary>
        static public bool Read <T>(ref T ioObject, Stream inStream, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
        {
            if (inStream == null)
            {
                Debug.LogError("[BeauData] Error when reading object: Provided stream is null.");
                return(false);
            }
            if (inStream.Length == 0)
            {
                Debug.LogWarning("[BeauData] Error when reading object: Empty stream");
                return(false);
            }

            MemoryStream memoryStream = inStream is MemoryStream ? (MemoryStream)inStream : GetBytes(inStream);

            if (inFormat == Format.AutoDetect)
            {
                inFormat = DetectFileFormatFromContents(memoryStream);
            }

            memoryStream.Position = 0;

            try
            {
                Serializer serializer;
                switch (inFormat)
                {
                case Format.JSON:
                {
                    JSON root = JSON.Parse(System.Text.Encoding.UTF8.GetString(memoryStream.GetBuffer()));
                    serializer = new JSONSerializer(root);
                    break;
                }

                case Format.XML:
                {
                    XmlDocument document = new XmlDocument();
                    document.LoadXml(System.Text.Encoding.UTF8.GetString(memoryStream.GetBuffer()));
                    serializer = new XMLSerializer(document);
                    break;
                }

                case Format.Binary:
                {
                    serializer = new BinarySerializer(memoryStream);
                    break;
                }

                case Format.GZIP:
                {
                    serializer = new GZIPSerializer(memoryStream);
                    break;
                }

                default:
                {
                    Debug.LogError("[BeauData] Unknown file format");
                    return(false);
                }
                }

                using (serializer)
                {
                    serializer.Read <T>(ref ioObject, inContext);
                    if (serializer.HasErrors)
                    {
                        Debug.LogError("[BeauData] Error when reading object:\n" + serializer.Errors);
                    }
                    return(!serializer.HasErrors);
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Debug.LogError("[BeauData] Exception when reading object");
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// Reads an object from the given text.
        /// </summary>
        static public bool Read <T>(ref T ioObject, string inText, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
        {
            if (string.IsNullOrEmpty(inText))
            {
                Debug.LogWarning("[BeauData] Error when reading object: Empty string");
                return(false);
            }

            if (inFormat == Format.AutoDetect)
            {
                inFormat = DetectFileFormatFromContents(inText);
            }

            try
            {
                Serializer serializer;
                switch (inFormat)
                {
                case Format.JSON:
                {
                    JSON root = JSON.Parse(inText);
                    serializer = new JSONSerializer(root);
                    break;
                }

                case Format.XML:
                {
                    XmlDocument document = new XmlDocument();
                    document.LoadXml(inText);
                    serializer = new XMLSerializer(document);
                    break;
                }

                case Format.Binary:
                {
                    byte[]       buffer = System.Text.Encoding.UTF8.GetBytes(inText);
                    MemoryStream stream = GetStream(buffer);
                    serializer = new BinarySerializer(stream);
                    break;
                }

                case Format.GZIP:
                {
                    byte[]       buffer = System.Text.Encoding.UTF8.GetBytes(inText);
                    MemoryStream stream = GetStream(buffer);
                    serializer = new GZIPSerializer(stream);
                    break;
                }

                default:
                {
                    Debug.LogError("[BeauData] Unknown file format");
                    return(false);
                }
                }

                using (serializer)
                {
                    serializer.Read <T>(ref ioObject, inContext);
                    if (serializer.HasErrors)
                    {
                        Debug.LogError("[BeauData] Error when reading object:\n" + serializer.Errors);
                    }
                    return(!serializer.HasErrors);
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                Debug.LogError("[BeauData] Exception when reading object");
                return(false);
            }
        }