We get a lot of questions about JSON serialization so we thought we would do a blog post on it. This code was executed using EntitySpaces 2010 but it should work the same for EntitySpaces 2009. We used the the NET 3.5 JSON serializer "DataContractJsonSerializer" located in the “System.Runtime.Serialization.Json” namespace which is in the “System.ServiceModel.Web” assembly.
Let’s take a look at a easy sample.
Employees emp = new Employees();emp.Query.es.Top = 1;emp.Query.Select( emp.Query.EmployeeID, emp.Query.FirstName, emp.Query.LastName, emp.Query.HireDate, (emp.Query.LastName + ", " + emp.Query.FirstName).As("Fullname"));emp.Query.Load(); // Modify the first name ...emp.FirstName = "Freddy"; // Serialize it using our EmployeesProxyStubDataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(EmployeesProxyStub));MemoryStream ms = new MemoryStream();ser.WriteObject(ms, (EmployeesProxyStub)emp); // Cast it (we have a built in cast operator) string json = Encoding.Default.GetString(ms.ToArray());ms.Close(); // Convert it back into an objectMemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)); ser = new DataContractJsonSerializer(typeof(EmployeesProxyStub));EmployeesProxyStub empProxy = ser.ReadObject(ms) as EmployeesProxyStub;ms.Close(); Employees emp2 = empProxy.Entity; // Now we have a reconstituted object
Employees emp = new Employees();emp.Query.es.Top = 1;emp.Query.Select( emp.Query.EmployeeID, emp.Query.FirstName, emp.Query.LastName, emp.Query.HireDate, (emp.Query.LastName + ", " + emp.Query.FirstName).As("Fullname"));emp.Query.Load();
// Modify the first name ...emp.FirstName = "Freddy";
// Serialize it using our EmployeesProxyStubDataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(EmployeesProxyStub));MemoryStream ms = new MemoryStream();ser.WriteObject(ms, (EmployeesProxyStub)emp); // Cast it (we have a built in cast operator)
string json = Encoding.Default.GetString(ms.ToArray());ms.Close();
// Convert it back into an objectMemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
ser = new DataContractJsonSerializer(typeof(EmployeesProxyStub));EmployeesProxyStub empProxy = ser.ReadObject(ms) as EmployeesProxyStub;ms.Close();
Employees emp2 = empProxy.Entity; // Now we have a reconstituted object
This is what the Empoyee object looks like when serialized in JSON …
{"EmployeeID":1,"LastName":"Burns","FirstName":"Freddy","HireDate":"\/Date(704692800000-0400)\/","esRowState":"Modified","ModifiedColumns":["FirstName"],"ExtraColumns":[{"Key":"Fullname","Value":"Burns, Fred"}]}
It also deserializes just fine. You can do the same thing with our Collections, you just would just use the EmployeesCollectionProxyStub and the EmployeesCollection classes.
From Mobile Devices to large scale enterprise solutions in need of serious transaction support, EntitySpaces can meet your needs. Whether you’re writing an ASP.NET application with Medium Trust requirements, a Silverlight/WCF application, a Mono application, or a Windows.Forms application, the EntitySpaces architecture is there for you. EntitySpaces is provider independent, which means that you can run the same binary code against any of the supported databases. EntitySpaces is available in both C# and VB.NET. EntitySpaces uses no reflection, no XML files, and sports a tiny foot print of less than 200k. Pound for pound, EntitySpaces is one tough, dependable .NET architecture.
EntitySpaces LLCPersistence Layer and Business Objects for Microsoft .NEThttp://www.entityspaces.net
Page rendered at Saturday, July 31, 2010 6:01:25 PM (Eastern Standard Time, UTC-05:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.