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); }
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); }
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); }
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); }
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(); }
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(); }
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); }
public void ShouldCreateDatabase() { using (var session = _store.CreateSession()) { var doc = new Document {Type = "Product", Content = "{}"}; session.Save(doc); } }
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(); }
public void ShouldThrowWhenNull(Type serializerType) { var serializer = Activator.CreateInstance(serializerType) as IDocumentSerializer; var doc = new Document(); Assert.Throws<ArgumentNullException>(() => serializer.Serialize(null, ref doc)); }
public void ShouldReturnNullWhenEmpty(Type serializerType) { var serializer = Activator.CreateInstance(serializerType) as IDocumentSerializer; var doc = new Document {Content = String.Empty}; Assert.Null(serializer.Deserialize(doc)); }