コード例 #1
0
        public void ShouldDeserializeType(Type serializerType)
        {
            var serializer = Activator.CreateInstance(serializerType) as IDocumentSerializer;

            Assert.NotNull(serializer);

            const string content =
                @"{
                Firstname : 'Bill',
                Lastname : 'Gates',
                Address : {
                    Street : '1 Microsoft Way',
                    City : 'Redmond'
                }
            }";

            var doc = new Document
            {
                Content = content,
                Id = 0,
                Type = typeof (Person).SimplifiedTypeName()
            };

            var bill = serializer.Deserialize(doc) as Person;

            Assert.NotNull(bill);
            Assert.Equal("Bill", bill.Firstname);
            Assert.Equal("Gates", bill.Lastname);
        }
コード例 #2
0
ファイル: JSonSerializer.cs プロジェクト: alacpi40/yessql
        public object Deserialize(Document doc)
        {
            // if a CLR type is specified, use it during deserialization
            if (!String.IsNullOrEmpty(doc.Type))
            {
                var type = Type.GetType(doc.Type, false);
                var des = _serializer.Deserialize(doc.Content, type);

                // if the document has an Id property, set it back
                var idInfo = des.GetType().GetProperty("Id");
                if (idInfo != null)
                {
                    idInfo.SetValue(des, doc.Id, null);
                }

                return des;
            }

            var obj = _serializer.DeserializeObject(doc.Content) as IDictionary<String, object>;

            if (obj == null)
            {
                throw new InvalidCastException("Could not convert serialized object");
            }

            return ConvertToDynamic(obj);
        }
コード例 #3
0
        public void ShouldDeserializeAnonymousType(Type serializerType)
        {
            var serializer = Activator.CreateInstance(serializerType) as IDocumentSerializer;

            Assert.NotNull(serializer);

            const string content = @"{
                Firstname : 'Bill',
                Lastname : 'Gates',
                Address : {
                    Street : '1 Microsoft Way',
                    City : 'Redmond'
                }
            }";

            var doc = new Document
            {
                Content = content,
                Id = 0,
                Type = string.Empty
            };

            dynamic bill = serializer.Deserialize(doc);

            Assert.NotNull(bill);
            Assert.Equal("Bill", (string)bill.Firstname);
            Assert.Equal("Gates", (string)bill.Lastname);

            Assert.NotNull(bill.Address);
            Assert.Equal("1 Microsoft Way", (string)bill.Address.Street);
            Assert.Equal("Redmond", (string)bill.Address.City);
        }
コード例 #4
0
ファイル: JSonSerializer.cs プロジェクト: rajeshpillai/yessql
        public object Deserialize(Document doc)
        {
            if (String.IsNullOrEmpty(doc.Content))
            {
                return null;
            }

            // if a CLR type is specified, use it during deserialization
            if (!String.IsNullOrEmpty(doc.Type))
            {
                var type = Type.GetType(doc.Type, false);
                var des = _serializer.Deserialize(doc.Content, type);

                return des;
            }

            var obj = _serializer.DeserializeObject(doc.Content) as IDictionary<String, object>;

            if (obj == null)
            {
                throw new InvalidCastException("Could not convert serialized object");
            }

            return ConvertToDynamic(obj);
        }
コード例 #5
0
ファイル: JSonSerializer.cs プロジェクト: rajeshpillai/yessql
        public void Serialize(object obj, ref Document doc)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            var objType = obj.GetType();
            doc.Content = _serializer.Serialize(obj);
            doc.Type = objType.IsAnonymousType() ? String.Empty : objType.SimplifiedTypeName();
        }
コード例 #6
0
        public void Serialize(object obj, ref Document doc)
        {
            if(obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            var objType = obj.GetType();
            doc.Content = JsonConvert.SerializeObject(obj, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
            doc.Type = objType.IsAnonymousType() ? String.Empty : objType.SimplifiedTypeName();
        }
コード例 #7
0
        public object Deserialize(Document doc)
        {
            if (String.IsNullOrEmpty(doc.Content))
            {
                return null;
            }

            // if a CLR type is specified, use it during deserialization
            if (!String.IsNullOrEmpty(doc.Type))
            {
                var type = Type.GetType(doc.Type, false);
                var des = JsonConvert.DeserializeObject(doc.Content, type, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });

                return des;
            }

            return JObject.Parse(doc.Content);
        }
コード例 #8
0
ファイル: CoreTests.cs プロジェクト: valmont/yessql
        public void ShouldCreateDatabase()
        {
            using (var session = _store.CreateSession())
            {
                var doc = new Document {Type = "Product", Content = "{}"};

                session.Save(doc);
            }
        }
コード例 #9
0
ファイル: JSonSerializer.cs プロジェクト: alacpi40/yessql
 public void Serialize(object obj, ref Document doc)
 {
     var objType = obj.GetType();
     doc.Content = _serializer.Serialize(obj);
     doc.Type = IsAnonymousType(objType) ? String.Empty : objType.SimplifiedTypeName();
 }
コード例 #10
0
        public void ShouldThrowWhenNull(Type serializerType)
        {
            var serializer = Activator.CreateInstance(serializerType) as IDocumentSerializer;

            var doc = new Document();
            Assert.Throws<ArgumentNullException>(() => serializer.Serialize(null, ref doc));
        }
コード例 #11
0
        public void ShouldReturnNullWhenEmpty(Type serializerType)
        {
            var serializer = Activator.CreateInstance(serializerType) as IDocumentSerializer;

            var doc = new Document {Content = String.Empty};
            Assert.Null(serializer.Deserialize(doc));
        }