I'm implementing an abstraction layer between client software and a database. The software was originally written using OleDb and it's working well. We already deal with data access through a Data component but the client software still calls its procedures using OleDb based procedures. To be precise, we would still pass System.Data.OleDb.OleDbParameter[] arrays into the Data component and it would return System.Data.DataSet objects to the client.
We are now writing an API as an intermediate between database and Data Component, so in order to cause as little impact as possible to the client software we will, for starters anyway, still call its methods in exactly the same way. Internally, the Data Component will serialize the OleDbParameter[] arrays we pass in to JSON and call the API, which will in turn return the Data Sets serialized to JSON so that our Data Component can then Deserialize them back to a System.Data.DataSet and the Client software won't be any the wiser.
This is where we hit an odd snag.
Having tried System.Text.Json and System.Web.Script.Serialization I find that both don't work very happily with DataSets. Both of these appear to have problem with circular references when trying to serialize System.Data.DataSet object. Here, NewtonSoft.Json comes to the rescue:
Serializing the DataSet is easy with NewtonSoft.Json:
string sData = string.Empty;
Newtonsoft.Json.JsonSerializerSettings oSet = new Newtonsoft.Json.JsonSerializerSettings();
sData = Newtonsoft.Json.JsonConvert.SerializeObject(value: oDS, type: oDS.GetType(), settings: oSet);
And so is deserializing it back into a DataSet
System.Data.DataSet oDS = null;
oDS = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Data.DataSet>(value: sData);
So you'd think the choice is an easy one. NewtonSoft.Json it is, right. Except.... It gives me problems with the Parameters
string sParameters = string.Empty;
System.Data.OleDb.OleDbParameter oXML;
oXML = new System.Data.OleDb.OleDbParameter(name: "XML", dataType: System.Data.OleDb.OleDbType.VarWChar, size: -1);
oXML.Value = "NotRelevantToThisQuestion";
System.Data.OleDb.OleDbParameter oTotalRecords;
oTotalRecords = new System.Data.OleDb.OleDbParameter(name: "TotalRecords", dataType: System.Data.OleDb.OleDbType.Integer);
oTotalRecords.Value = 0;
oTotalRecords.Direction = System.Data.ParameterDirection.InputOutput;
System.Data.OleDb.OleDbParameter[] oParms;
oParms = new System.Data.OleDb.OleDbParameter[] {oXML, oTotalRecords };
Newtonsoft.Json.JsonSerializerSettings oSet = new Newtonsoft.Json.JsonSerializerSettings();
sParameters = Newtonsoft.Json.JsonConvert.SerializeObject(value: oParms, type: oParms.GetType(), settings: oSet);
It seems to produce a JSON string that lists the two parameters, but none of their properties are present. The resulting JSON is:
["XML","TotalRecords"]
I can get THAT one to work using System.Web.Serialization.JavaScriptSerializer
System.Web.Script.Serialization.JavaScriptSerializer oSer;
oSer = new System.Web.Script.Serialization.JavaScriptSerializer();
sParameters = oSer.Serialize(oParms);
This produces:
[{"DbType":16,"OleDbType":202,"ParameterName":"XML","Precision":0,"Scale":0,"Value":"NotRelevantToThisQuestion","Direction":1,"IsNullable":false,"Size":-1,"SourceColumn":"","SourceColumnNullMapping":false,"SourceVersion":512},{"DbType":11,"OleDbType":3,"ParameterName":"TotalRecords","Precision":0,"Scale":0,"Value":0,"Direction":3,"IsNullable":false,"Size":0,"SourceColumn":"","SourceColumnNullMapping":false,"SourceVersion":512}]
And after the call the Input/Output parameter gets a value which I can then also serialize, send back and deserialize at the receiving end to allow me to read its value, as well as the DataSet which I already deserialized using NewtonSoft.Json
System.Data.OleDb.OleDbParameter[] oParms;
System.Web.Script.Serialization.JavaScriptSerializer oSer;
oSer = new System.Web.Script.Serialization.JavaScriptSerializer();
oParms = oSer.Deserialize<System.Data.OleDb.OleDbParameter[]>(sParameters);
So if you were patient enough to read all the way through to here I can just about imagine what you're thinking now: "Just use NewtonSoft.Json for your DataSet, and use System.Web.Script.Serialization.JavaScriptSerializer for your parameters. What's your problem?"
Sure. But it sticks in my craw that I would have to use two different packages to do a job that one package should be able to handle. So with that said I can finally get to my question:
Is there any way to handle both Serialization/Deserialization requirements with a single package? As long as it's documented clearly I am happy to use any of the 3 packages I already mentioned or even another one you might present here.