Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Serialize Dataset to Json and back
Message
From
24/11/2015 11:49:33
 
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 9.0
OS:
Windows 10
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01627834
Message ID:
01627852
Views:
44
>>>>Hi,
>>>>
>>>>I have been Googling on the topic of how to serialize a dataset to JSon, then store it in the local storage and then convert it back to the dataset.
>>>>
>>>>In some posts I see people use JsonConvert() and in some other cases there is a code to "manually' convert dataset to a string. Which method is more efficient, in your opinion? In one thread (on Stackoverflow) the person says that JsonConvert() stores the column name for each column and each row into a string. This seems to be a pretty big waste of storage (given that the local storage is limited).
>>>>What do you think?
>>
>>-- Copy of your message --
>>Firstly I wouldn't try to convert a dataset to Json - use as simple a class as possible. Don't know if you are using EF - that provides an easy way for this.
>>
>>WebApi automatically used NewtonSoft.Json to convert C# objects to Json if that type is requested (or set as the default type). The column name has to be repeated for each property - that's the nature of Json. In practice it's not a problem - still more compact to send over the wire than XML.
>>
>>I'm routinely storing arrays of 200~ objects (each with 10-12 properties) in local storage on the browser under one key. Only problem is that if you want to modify one object you have to load them all, make the change, and re-save - but again I've not found it too process intensive.
>>-- Copy of your message. End --
>>
>>I don't use EF. The reason I want/wanted to convert dataset to JSON is in my 1st version of implementation (current) I have methods that retrieve data from SQL Server and than fill datasets. And since now I am looking into changing it to a disconnected mode, I thought I would "reuse" the code of getting the dataset and then convert it to JSON and store in localStorage. And as you already know I don't use WebApi.
>>
>>I am listening to what you are saying. Maybe in the methods where I get the data from SQL server (not EF) I can convert it to JSON, instead of doing it via dataset. But then, an important for me is to be able to get the JSON from localStorage and convert to dataset. Because most of my drop-down lists/controls are based on binding them to datasets.
>>
>>Thank you.
>
>See my other post.
>One approach to convert the dataset here : http://stackoverflow.com/questions/1354034/how-do-i-convert-a-datatable-into-a-poco-object-in-asp-net-mvc

Another approach would be to cast the DataTable to a List of dynamic objects. The following class can be used to create a dynamic object from a data row:
public class DynamicDataRow : DynamicObject
	{
		private readonly DataRow _dataRow;
		public DataRow Row
		{
			get
			{
				return _dataRow;
			}
		}
		public int Index
		{
			get
			{
				return _dataRow.Table.Rows.IndexOf(_dataRow);
			}
		}
		public DynamicDataRow(DataRow dataRow)
		{
			_dataRow = dataRow;
		}
		public override bool TryGetMember(GetMemberBinder binder, out object result)
		{
			bool retVal = false;
			result = null;
			try
			{
				result = _dataRow.IsNull(binder.Name) ? null : _dataRow[binder.Name];
				retVal = true;
			}
			catch { }
			return retVal;
		}
		public override bool TrySetMember(SetMemberBinder binder, object value)
		{
			bool retVal = false;
			try
			{
				_dataRow[binder.Name] = value;
				retVal = true;
			}
			catch { }
			return retVal;
		}

		public override IEnumerable<string> GetDynamicMemberNames()
		{
			return _dataRow.Table.Columns.AsParallel().AsOrdered().OfType<DataColumn>().Select(dc => dc.ColumnName);
		}}
	}
And the following extension method can be used to convert the DataTable to a List of those objects:
public static class DataTableExtensions
{
	public static List<dynamic> ToDynamicList(this DataTable table)
	{
		var dictionaryList = table.AsEnumerable().AsParallel().AsOrdered()
			.Select(dataRow => new DynamicDataRow(dataRow)).Cast< dynamic>().ToList();

		return dictionaryList;
	}
}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform