Example #1
0
        /// <summary>
        /// Gets the data from this component, serialized to the specified type. The data is
        /// retrieved from the base component, serialized with JSON, and reconstituted as type
        /// T in the memory space of the caller.
        ///
        /// The target type must exist and be a [JsonObject] in both this assembly and the
        /// target component's assembly.
        ///
        /// This method is somewhat slow and memory intensive, and should be used sparingly.
        /// </summary>
        /// <typeparam name="T">The data type to retrieve and into which to convert.</typeparam>
        /// <param name="defValue">The default value if the instance data is unset.</param>
        /// <returns>The data, or defValue if the instance data has not been set or cannot be serialized.</returns>
        public T GetInstanceDataSerialized <T>(T defValue = default)
        {
            var remoteData = InstanceData;
            T   result     = defValue;

            using (var buffer = new MemoryStream(1024)) {
                try {
                    var writer = new StreamWriter(buffer, Encoding.UTF8);
                    SerializationSettings.Serialize(writer, remoteData);
                    writer.Flush();
                    buffer.Position = 0L;
                    var reader = new StreamReader(buffer, Encoding.UTF8);
                    if (SerializationSettings.Deserialize(reader, typeof(T)) is T decoded)
                    {
                        result = decoded;
                    }
                } catch (JsonException e) {
                    PUtil.LogError("Unable to serialize instance data for component " + ID +
                                   ":");
                    PUtil.LogException(e);
                    result = defValue;
                }
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// Gets the shared data between components with this ID, serialized to the specified
        /// type. The shared data is retrieved, serialized with JSON, and reconstituted as type
        /// T in the memory space of the caller.
        ///
        /// The target type must exist and be a [JsonObject] in both this assembly and the
        /// target component's assembly.
        ///
        /// This method is somewhat slow and memory intensive, and should be used sparingly.
        /// </summary>
        /// <typeparam name="T">The data type to retrieve and into which to convert.</typeparam>
        /// <param name="defValue">The default value if the shared data is unset.</param>
        /// <returns>The data, or defValue if the shared data has not been set or cannot be serialized.</returns>
        public T GetSharedDataSerialized <T>(T defValue = default)
        {
            var remoteData = PRegistry.Instance.GetSharedData(ID);
            T   result     = defValue;

            using (var buffer = new MemoryStream(1024)) {
                try {
                    SerializationSettings.Serialize(new StreamWriter(buffer, Encoding.UTF8),
                                                    remoteData);
                    buffer.Position = 0L;
                    if (SerializationSettings.Deserialize(new StreamReader(buffer,
                                                                           Encoding.UTF8), typeof(T)) is T decoded)
                    {
                        result = decoded;
                    }
                } catch (JsonException e) {
                    PUtil.LogError("Unable to serialize shared data for component " + ID +
                                   ":");
                    PUtil.LogException(e);
                    result = defValue;
                }
            }
            return(result);
        }