Beispiel #1
0
        /// <summary>
        /// Read <see cref="ISerializerContext"/> context to the current object include the body and properties using <see cref="SerializeInfo"/>.
        /// </summary>
        /// <param name="context"></param>
        public void ReadContext(ISerializerContext context)
        {
            SerializeInfo info = context.ReadSerializeInfo();

            ReadContext(info);
        }
        /// <summary>
        /// Writes an object to the given stream.
        /// </summary>
        static public void Write <T>(T inObject, Stream inStream, OutputOptions inOptions = OutputOptions.None, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
        {
            if (inFormat == Format.AutoDetect)
            {
                inFormat = DefaultWriteFormat;
            }

            using (Serializer serializer = CreateWriter(inFormat))
            {
                serializer.Write <T>(ref inObject, inContext);
                serializer.AsStream(inStream, inOptions);
            }
        }
 /// <summary>
 /// Writes an object to PlayerPrefs.
 /// </summary>
 static public void WritePrefs <T>(T inObject, string inKey, OutputOptions inOptions = OutputOptions.None, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
 {
     PlayerPrefs.SetString(inKey, Write(inObject, inOptions, inFormat));
 }
        /// <summary>
        /// Reads an object from the given byte array.
        /// </summary>
        static public T Read <T>(byte[] inBytes, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
        {
            T obj = default(T);

            Read <T>(ref obj, inBytes, inFormat, inContext);
            return(obj);
        }
 /// <summary>
 /// Reads an object from PlayerPrefs.
 /// </summary>
 static public bool ReadPrefs <T>(ref T ioObject, string inKey, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
 {
     return(Read(ref ioObject, PlayerPrefs.GetString(inKey, string.Empty), inFormat, inContext));
 }
        /// <summary>
        /// Reads an object from the given UnityWebRequest.
        /// </summary>
        static public T Read <T>(UnityWebRequest inWWW, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
        {
            T obj = default(T);

            Read <T>(ref obj, inWWW, inFormat, inContext);
            return(obj);
        }
        /// <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);
            }
        }
 void IOnDeserialized.OnDeserialized(ISerializerContext context)
 {
     this.serializationManager = context.SerializationManager;
 }
Beispiel #9
0
 void IOnDeserialized.OnDeserialized(ISerializerContext context)
 {
     this.runtimeClient = context?.AdditionalContext as IRuntimeClient;
 }
Beispiel #10
0
        /// <inheritdoc/>
        public Task SerializeAsync(object message, Stream output, ISerializerContext context)
        {
            Serializer.Serialize(output, message);

            return(Task.CompletedTask);
        }
Beispiel #11
0
 /// <inheritdoc/>
 public Task <object> DeserializeAsync(Stream input, Type type, ISerializerContext context)
 {
     return(Task.FromResult(Serializer.Deserialize(type, input)));
 }
Beispiel #12
0
 /// <summary>
 /// Reads 'Metadata' from the metadata of the <paramref name="context"/> or throws <see cref="InvalidOperationException"/>.
 /// </summary>
 /// <param name="context">The context to read the metadata from.</param>
 /// <returns>The value associated with the 'Metadata' or throws <see cref="InvalidOperationException"/>.</returns>
 /// <exception cref="InvalidOperationException">When the 'Metadata' is not contained in the metadata of the <paramref name="context"/>.</exception>
 public static IReadOnlyKeyValueCollection GetEnvelopeMetadata(this ISerializerContext context)
 {
     Ensure.NotNull(context, "context");
     return(context.Metadata.Get <IReadOnlyKeyValueCollection>("EnvelopeMetadata"));
 }
Beispiel #13
0
 /// <summary>
 /// Tries to read 'Metadata' from the metadata of the <paramref name="context"/>.
 /// </summary>
 /// <param name="context">The context to read the metadata from.</param>
 /// <param name="value">The value of the 'Metadata'.</param>
 /// <returns><c>true</c> if 'Metadata' can is contained in the <paramref name="context"/>; <c>false</c> otherwise.</returns>
 public static bool TryGetEnvelopeMetadata(this ISerializerContext context, out IReadOnlyKeyValueCollection value)
 {
     Ensure.NotNull(context, "context");
     return(context.Metadata.TryGet("EnvelopeMetadata", out value));
 }
 /// <inheritdoc/>
 public async Task <object> DeserializeAsync(Stream input, Type type, ISerializerContext context)
 {
     return(await JsonSerializer
            .DeserializeAsync(input, type, this.options)
            .ConfigureAwait(false));
 }
Beispiel #15
0
 void ISerializedProxy <int> .SetProxyValue(int inValue, ISerializerContext inContext)
 {
     Value = inValue;
 }
Beispiel #16
0
 public void OnDeserialized(ISerializerContext context)
 {
     this.Context = context;
 }
        // Restore obsolete WWW warning
        #pragma warning restore 612, 618

        #endregion

        #region UnityWebRequest

        /// <summary>
        /// Reads an object from the given UnityWebRequest.
        /// </summary>
        static public bool Read <T>(ref T ioObject, UnityWebRequest inWWW, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
        {
            if (!String.IsNullOrEmpty(inWWW.error))
            {
                Debug.LogError("[BeauData] Error when reading object UnityWebRequest: " + inWWW.error);
                return(false);
            }

            using (MemoryStream stream = GetStream(inWWW.downloadHandler.data))
            {
                return(Read <T>(ref ioObject, stream, inFormat, inContext));
            }
        }
Beispiel #18
0
 public void OnDeserialized(ISerializerContext context)
 {
     _serializationManager = new OrleansSerializationManager(context.GetSerializationManager());
 }
        /// <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);
            }
        }
 public bool TrySerialize(object input, ISerializerContext context)
 {
     return(envelopeFormatter.TrySerialize(input, context));
 }
 /// <summary>
 /// Reads an object from the given byte array.
 /// </summary>
 static public bool Read <T>(ref T ioObject, byte[] inBytes, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
 {
     return(Read <T>(ref ioObject, GetStream(inBytes), inFormat, inContext));
 }
 public Task <bool> TrySerializeAsync(object input, ISerializerContext context)
 {
     return(envelopeFormatter.TrySerializeAsync(input, context));
 }
        /// <summary>
        /// Reads an object from a file.
        /// </summary>
        static public bool ReadFile <T>(ref T ioObject, string inFilePath, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
        {
            string filePath = PathUtility.CorrectPath(inFilePath, inFormat);

            if (!File.Exists(filePath))
            {
                Debug.LogWarning("[BeauData] Error when reading object: File '" + filePath + "' does not exist.");
                return(false);
            }

            using (FileStream stream = File.OpenRead(filePath))
            {
                return(Read(ref ioObject, stream, inFormat, inContext));
            }
        }
Beispiel #24
0
 void IOnDeserialized.OnDeserialized(ISerializerContext context)
 {
     _serializationManager = context.GetSerializationManager();
 }
        /// <summary>
        /// Reads an object from PlayerPrefs.
        /// </summary>
        static public T ReadPrefs <T>(string inKey, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
        {
            T obj = default(T);

            ReadPrefs <T>(ref obj, inKey, inFormat, inContext);
            return(obj);
        }
Beispiel #26
0
 void IOnDeserialized.OnDeserialized(ISerializerContext context)
 {
     this.serializer = MemoryMessageBodySerializerFactory <TSerializer> .GetOrCreateSerializer(context.ServiceProvider);
 }
        /// <summary>
        /// Writes an object to a file.
        /// </summary>
        static public void WriteFile <T>(T inObject, string inFilePath, OutputOptions inOptions = OutputOptions.None, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
        {
            string finalPath = PathUtility.CorrectPath(inFilePath, inFormat);

            using (FileStream stream = File.Open(finalPath, FileMode.Create))
            {
                Write(inObject, stream, inOptions, inFormat, inContext);
            }
        }
Beispiel #28
0
 int ISerializedProxy <int> .GetProxyValue(ISerializerContext inContext)
 {
     return(Value);
 }
        /// <summary>
        /// Reads an object from the given text asset.
        /// </summary>
        static public bool Read <T>(ref T ioObject, TextAsset inTextAsset, Format inFormat = Format.AutoDetect, ISerializerContext inContext = null) where T : ISerializedObject
        {
            if (inTextAsset == null)
            {
                Debug.LogError("[BeauData] Error when reading object: Provided TextAsset is null.");
                return(false);
            }

            using (MemoryStream stream = GetStream(inTextAsset.bytes))
            {
                return(Read <T>(ref ioObject, stream, inFormat, inContext));
            }
        }
Beispiel #30
0
 public Task <bool> TrySerializeAsync(object input, ISerializerContext context)
 => Task.Factory.StartNew(() => TrySerialize(input, context));