Exemple #1
0
        /**
         * Determine the type of a value view and fully deserialize it into the correct buffer.
         */
        JsonKeyHandle DeserializeValueView(SerializedValueView valueView)
        {
            JsonKeyHandle jsonKeyHandle;

            switch (valueView.Type)
            {
            case TokenType.Primitive:
                jsonKeyHandle = DeserializeToPrimitiveBuffer(valueView.AsPrimitiveView());
                break;

            case TokenType.String:
                jsonKeyHandle = DeserializeToStringBuffer(valueView.AsStringView());
                break;

            case TokenType.Object:
                jsonKeyHandle = DeserializeToObjectBuffer(valueView.AsObjectView());
                break;

            case TokenType.Array:
                jsonKeyHandle = DeserializeToArrayBuffer(valueView.AsArrayView());
                break;

            default:
                jsonKeyHandle = default;
                break;
            }

            return(jsonKeyHandle);
        }
        public Platform Deserialize(SerializedValueView view)
        {
            if (view.Type != TokenType.String)
            {
                return(null);
            }

            var json = view.AsStringView().ToString();

            if (string.IsNullOrEmpty(json))
            {
                return(null);
            }

            return(Platform.GetPlatformByName(json));
        }
        public Platform Deserialize(SerializedValueView view)
        {
            if (view.Type != TokenType.String)
            {
                return(null);
            }

            var json = view.AsStringView().ToString();

            if (string.IsNullOrEmpty(json))
            {
                return(null);
            }

            return(TypeCacheHelper.ConstructTypeDerivedFrom <Platform>(platform => platform.Name == json, false));
        }
Exemple #4
0
        public BuildPipelineBase Deserialize(SerializedValueView view)
        {
            if (view.Type != TokenType.String)
            {
                return(null);
            }

            var json = view.AsStringView().ToString();

            if (string.IsNullOrEmpty(json))
            {
                return(null);
            }

            if (TypeConstructionUtility.TryConstructFromAssemblyQualifiedTypeName <BuildPipelineBase>(json, out var step))
            {
                return(step);
            }

            throw new ArgumentException($"Failed to construct type. Could not resolve type from TypeName=[{json}].");
        }
Exemple #5
0
        static bool TryDeserializeLazyLoadReference <TValue>(SerializedValueView view, ref TValue value, List <DeserializationEvent> events)
        {
            if (!RuntimeTypeInfoCache <TValue> .IsLazyLoadReference)
            {
                return(false);
            }

            if (view.Type != TokenType.String)
            {
                return(false);
            }

            var json = view.AsStringView().ToString();

            if (json == s_EmptyGlobalObjectId) // Workaround issue where GlobalObjectId.TryParse returns false for empty GlobalObjectId
            {
                return(true);
            }

            if (UnityEditor.GlobalObjectId.TryParse(json, out var id))
            {
#if UNITY_2020_1_OR_NEWER
                var instanceID = UnityEditor.GlobalObjectId.GlobalObjectIdentifierToInstanceIDSlow(id);
                PropertyContainer.SetValue(ref value, k_LazyLoadReference_InstanceID, instanceID);
#else
                var asset = UnityEditor.GlobalObjectId.GlobalObjectIdentifierToObjectSlow(id);
                if ((asset == null || !asset) && !id.assetGUID.Empty())
                {
                    throw new InvalidOperationException($"An error occured while deserializing asset reference GUID=[{id.assetGUID.ToString()}]. Asset is not yet loaded and will result in a null reference.");
                }

                var instanceID = asset.GetInstanceID();
                PropertyContainer.SetValue(ref value, k_LazyLoadReference_InstanceID, instanceID);
#endif
                return(true);
            }

            events.Add(new DeserializationEvent(EventType.Error, $"An error occured while deserializing asset reference Value=[{json}]."));
            return(false);
        }
        /// <summary>
        /// Reads the specified <see cref="SerializedValueView"/> in to the given reference.
        /// </summary>
        /// <param name="value">The existing reference to read in to.</param>
        /// <param name="view">The serialized value.</param>
        /// <typeparam name="TValue">The value type.</typeparam>
        public void Read <TValue>(ref TValue value, SerializedValueView view)
        {
            switch (view.Type)
            {
            case TokenType.String:
            {
                TypeConversion.TryConvert(view.AsStringView().ToString(), out value);
                break;
            }

            case TokenType.Primitive:
            {
                var p = view.AsPrimitiveView();

                if (p.IsIntegral())
                {
                    if (p.IsSigned())
                    {
                        TypeConversion.TryConvert(p.AsInt64(), out value);
                    }
                    else
                    {
                        TypeConversion.TryConvert(p.AsUInt64(), out value);
                    }
                }
                else if (p.IsDecimal() || p.IsInfinity() || p.IsNaN())
                {
                    TypeConversion.TryConvert(p.AsFloat(), out value);
                }
                else if (p.IsBoolean())
                {
                    TypeConversion.TryConvert(p.AsBoolean(), out value);
                }
                else if (p.IsNull())
                {
                    value = default;
                }

                break;
            }

            case TokenType.Object:
            case TokenType.Array:
            {
                var serializedType = !RuntimeTypeInfoCache <TValue> .IsAbstractOrInterface
                        ? typeof(TValue)
                        : null;

                using (m_Visitor.CreateSerializedTypeScope(serializedType))
                    using (m_Visitor.CreateViewScope(view.AsUnsafe()))
                        using (m_Visitor.CreateDisableRootMigrationScope(true))
                        {
                            var container = new PropertyWrapper <TValue>(value);
                            PropertyContainer.Visit(ref container, m_Visitor, out _);
                            value = container.Value;
                        }

                break;
            }

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #7
0
 FileInfo IJsonAdapter <FileInfo> .Deserialize(SerializedValueView view)
 {
     return(view.AsStringView().Equals("null") ? null : new FileInfo(view.ToString()));
 }