alesdo / datasetconverters Goto Github PK
View Code? Open in Web Editor NEWDataSet JsonConverters for JSON.Net that preserve all the state
License: MIT License
DataSet JsonConverters for JSON.Net that preserve all the state
License: MIT License
During editing - after calling BeginEdit()
but before calling EndEdit()
on a DataRow
- the .NET framework will serialize the dataset with the new values, while this library will serialize the dataset with the old values.
This unit test demonstrates this:
using System.Data;
using System.IO;
using System.Xml.Serialization;
using Newtonsoft.Json;
using NUnit.Framework;
namespace DataSetConvertersTests
{
[TestFixture]
public class DataSetConvertersTest
{
[Test]
public void DataSetSerialization()
{
var dataSet = new DataSet();
var dataTable = dataSet.Tables.Add("table");
dataTable.Columns.Add("Value");
var row = dataTable.Rows.Add("Old");
dataSet.AcceptChanges();
row.BeginEdit();
row[0] = "New";
Assert.That(row[0], Is.EqualTo("New"));
Assert.That(row[0, DataRowVersion.Default], Is.EqualTo("New"));
Assert.That(row[0, DataRowVersion.Original], Is.EqualTo("Old"));
Assert.That(row[0, DataRowVersion.Current], Is.EqualTo("Old"));
Assert.That(row[0, DataRowVersion.Proposed], Is.EqualTo("New"));
var dataSetBeforeEndEdit = SerializeAndDeserializeXml(dataSet);
var rowBeforeEndEdit = dataSetBeforeEndEdit.Tables[0].Rows[0];
// These four asserts succeed for SerializeAndDeserializeXml, but fail for SerializeAndDeserializeJson
Assert.That(rowBeforeEndEdit[0], Is.EqualTo("New"));
Assert.That(rowBeforeEndEdit[0, DataRowVersion.Default], Is.EqualTo("New"));
Assert.That(rowBeforeEndEdit[0, DataRowVersion.Original], Is.EqualTo("New")); // Yes, xml only serializes the new value here (loses the Original value)
Assert.That(rowBeforeEndEdit[0, DataRowVersion.Current], Is.EqualTo("New"));
Assert.That(() => row[0, DataRowVersion.Proposed], Throws.Exception);
row.EndEdit();
Assert.That(row[0], Is.EqualTo("New"));
Assert.That(row[0, DataRowVersion.Default], Is.EqualTo("New"));
Assert.That(row[0, DataRowVersion.Original], Is.EqualTo("Old"));
Assert.That(row[0, DataRowVersion.Current], Is.EqualTo("New"));
Assert.That(() => row[0, DataRowVersion.Proposed], Throws.Exception);
var dataSetAfterEndEdit = SerializeAndDeserializeXml(dataSet);
var rowAfterEndEdit = dataSetAfterEndEdit.Tables[0].Rows[0];
Assert.That(rowAfterEndEdit[0], Is.EqualTo("New"));
Assert.That(rowAfterEndEdit[0, DataRowVersion.Default], Is.EqualTo("New"));
Assert.That(rowAfterEndEdit[0, DataRowVersion.Original], Is.EqualTo("Old"));
Assert.That(rowAfterEndEdit[0, DataRowVersion.Current], Is.EqualTo("New"));
}
private DataSet SerializeAndDeserializeXml(DataSet dataSet)
{
XmlSerializer xmlSerializer = new(typeof(DataSet));
using StringWriter writer = new();
xmlSerializer.Serialize(writer, dataSet);
var xml = writer.ToString();
var reader = new StringReader(xml);
return (DataSet)xmlSerializer.Deserialize(reader);
}
private static readonly JsonSerializerSettings SerializerSettings = new()
{
Converters = new JsonConverter[]
{
new Json.Net.DataSetConverters.DataSetConverter(),
new Json.Net.DataSetConverters.DataTableConverter()
}
};
private DataSet SerializeAndDeserializeJson(DataSet dataSet)
{
var json = JsonConvert.SerializeObject(dataSet, SerializerSettings);
return JsonConvert.DeserializeObject<DataSet>(json, SerializerSettings);
}
}
}
In the enterprise environment I currently work in, all projects are signed, and I'm currently unable to directly use this project. Adding a strong name would be very helpful.
First off, I'm very happy with your work, I'm pretty much in the exact spot you were in (but with remoting instead of WCF..)
Issue is as said in the title. I'll add a PR to fix it
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.