Example #1
0
        public static void DeserializeInto(this SerializationData data, ref object instance, bool forceReflected = false)
        {
            try
            {
                if (string.IsNullOrEmpty(data.json))
                {
                    instance = null;
                    return;
                }

#if DEBUG_SERIALIZATION
                Debug.Log(data.ToString($"<color=#3388FF>Deserializing into: <b>{instance?.GetType().Name ?? "null"} [{instance?.GetHashCode().ToString() ?? "N/A"}]</b></color>"));
#endif

                var operation = StartOperation();
                operation.objectReferences.AddRange(data.objectReferences);
                DeserializeJson(operation.serializer, data.json, ref instance, forceReflected);
                EndOperation(operation);
            }
            catch (Exception ex)
            {
                try
                {
                    Debug.LogWarning(data.ToString("Deserialization Failure Data"), instance as UnityObject);
                }
                catch (Exception ex2)
                {
                    Debug.LogWarning("Failed to log deserialization failure data:\n" + ex2, instance as UnityObject);
                }

                throw new SerializationException($"Deserialization into '{instance?.GetType().ToString() ?? "null"}' failed.", ex);
            }
        }
Example #2
0
        public static object Deserialize(this SerializationData data, bool forceReflected = false)
        {
            object instance = null;

            DeserializeInto(data, ref instance, forceReflected);
            return(instance);
        }
        void ISerializationCallbackReceiver.OnBeforeSerialize()
        {
            // Ignore the FullSerializer callback, but still catch the Unity callback
            if (Serialization.isCustomSerializing)
            {
                return;
            }

            Serialization.isUnitySerializing = true;

            // Starting in Unity 2018.3.0b7 apparently, the editor window tries to serialize
            // its title content and along the way, its image, which becomes an invalid reference.
            // But instead of setting it to actual null, it sets it to an invalid Unity Object,
            // which the UnityObjectConverter complains about and can't reliably detect off
            // the main thread.
            var titleImage = titleContent.image;

            titleContent.image = null;

            try
            {
                OnBeforeSerialize();
                _data = this.Serialize(true);
            }
            catch (Exception ex)
            {
                // Don't abort the whole serialization thread because this one object failed
                Debug.LogError($"Failed to serialize editor window.\n{ex}", this);
            }

            titleContent.image = titleImage;

            Serialization.isUnitySerializing = false;
        }
        public override void Load()
        {
            try
            {
                value = new SerializationData(EditorPrefs.GetString(namespacedKey)).Deserialize();
            }
            catch (Exception)
            {
                Debug.LogWarning($"Failed to deserialize editor pref '{configuration.plugin.id}.{key}', reverting to default.");
                value = defaultValue;
                Save();
            }

            if (!definedType.IsAssignableFrom(valueType))
            {
                Debug.LogWarning($"Failed to deserialize editor pref '{configuration.plugin.id}.{key}' as '{definedType.CSharpName()}', reverting to default.");
                value = defaultValue;
                Save();
            }
        }
Example #5
0
        public static SerializationData Serialize(this object value, bool forceReflected = false)
        {
            try
            {
                var operation        = StartOperation();
                var json             = SerializeJson(operation.serializer, value, forceReflected);
                var objectReferences = operation.objectReferences.ToArray();
                var data             = new SerializationData(json, objectReferences);
                EndOperation(operation);

#if DEBUG_SERIALIZATION
                Debug.Log(data.ToString($"<color=#88FF00>Serialized: <b>{value?.GetType().Name ?? "null"} [{value?.GetHashCode().ToString() ?? "N/A"}]</b></color>"));
#endif

                return(data);
            }
            catch (Exception ex)
            {
                throw new SerializationException($"Serialization of '{value?.GetType().ToString() ?? "null"}' failed.", ex);
            }
        }
        void ISerializationCallbackReceiver.OnBeforeSerialize()
        {
            if (Serialization.isCustomSerializing)
            {
                return;
            }

            Serialization.isUnitySerializing = true;

            try
            {
                OnBeforeSerialize();
                _data = this.Serialize(true);
                OnAfterSerialize();
            }
            catch (Exception ex)
            {
                // Don't abort the whole serialization thread because this one object failed
                Debug.LogError($"Failed to serialize behaviour.\n{ex}", this);
            }

            Serialization.isUnitySerializing = false;
        }
        void ISerializationCallbackReceiver.OnBeforeSerialize()
        {
            // Ignore the FullSerializer callback, but still catch the Unity callback
            if (Serialization.isCustomSerializing)
            {
                return;
            }

            Serialization.isUnitySerializing = true;

            try
            {
                OnBeforeSerialize();
                _data = this.Serialize(true);
                OnAfterSerialize();
            }
            catch (Exception ex)
            {
                // Don't abort the whole serialization thread because this one object failed
                Debug.LogError($"Failed to serialize scriptable object.\n{ex}", this);
            }

            Serialization.isUnitySerializing = false;
        }