Exemple #1
0
        /// <summary>
        /// Deserializes the schema.
        /// </summary>
        /// <param name="schemaBody">The schema body.</param>
        /// <param name="schemaTypeName">Name of the schema type.</param>
        /// <returns></returns>
        public IValueSchema <object> DeserializeSchema(string schemaBody, string schemaTypeName)
        {
            Type schemaType = ResolveType(schemaTypeName);

            return(!string.IsNullOrEmpty(schemaBody) ?
                   JsonSchemaDeserializer.Deserialize(schemaType, schemaBody) : Create(typeof(object)).Wrap());
        }
        /// <summary>
        /// Deserialize schemas and collection elements
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static IPropertySetCollection Deserialize(this PersistentPropertyCollection collection)
        {
            PropertySchemaSet     schemaSet = new PropertySchemaSet(new PropertySchemaFactory());
            PropertySetCollection restored  = new PropertySetCollection(schemaSet);

            //restore schemas
            foreach (PersistentPropertyElement element in collection.Elements)
            {
                PersistentSchemaElement schemaElement = collection.Schemas.First(s => s.SchemaName == element.Name);
                var valueSchema            = JsonSchemaDeserializer.Deserialize(schemaElement.SchemaType, schemaElement.SchemaBody);
                SerializationTypeHint hint = (SerializationTypeHint)schemaElement.SerializationHint;
                Func <PersistentPropertyElement, object> valueRetriever;
                if (!_serializationMap.TryGetValue(hint, out valueRetriever))
                {
                    throw new ArgumentException($"{element.Name} {hint}");
                }
                object val = valueRetriever(element);
                switch (hint)
                {
                case SerializationTypeHint.JsonString:
                    val = valueSchema.Serializer.Deserialize(val.ToString());
                    break;

                case SerializationTypeHint.BinaryObject:
                case SerializationTypeHint.Object:
                    byte[] data = (byte[])val;
                    val = valueSchema.Serializer.Deserialize(data);
                    break;
                }
                restored.Add(element.Name, val, valueSchema);
            }
            return(restored);
        }
        public void TestDeserializeIntSchema()
        {
            IValueSchema <int?> schema = new IntSchema
            {
                AllowNull      = false,
                DefaultValue   = 10,
                MaxValue       = 100,
                MinValue       = 2,
                PossibleValues = new int?[] { 10, 20, 30, 100 }
            };
            SchemaJsonSerializationVisitor visitor = new SchemaJsonSerializationVisitor();

            schema.Accept(visitor);
            IValueSchema <object> vs = JsonSchemaDeserializer.Deserialize(visitor.SchemaType, visitor.JsonValue);

            Assert.IsNotNull(vs);
            Assert.AreEqual(typeof(int?), vs.Type);
        }
        public void TestDeserializeStringSchema()
        {
            IValueSchema <string> schema = new StringSchema
            {
                AllowNull      = true,
                DefaultValue   = "abc",
                MaxLength      = 100,
                MinLength      = 2,
                PossibleValues = new[] { "abc", "def" }
            };
            SchemaJsonSerializationVisitor visitor = new SchemaJsonSerializationVisitor();

            schema.Accept(visitor);

            IValueSchema <object> vs = JsonSchemaDeserializer.Deserialize(visitor.SchemaType, visitor.JsonValue);

            Assert.IsNotNull(vs);
            Assert.AreEqual(typeof(string), vs.Type);
        }