/// <summary> /// Serializes the schema of a DataSet to a Tiferix JsonDataSetSchema stream. The function will utilize the JsonDataWriter object to extract all /// data from the DataSet and write it the appropriate format into the Tiferix.JsonDataSetSchema data stream. All .Net DataSet objects will first be /// extracted and serialized into JsonDataSetSchema objects which will then be written to the JsonDataSchema data stream. /// </summary> /// <param name="streamSchema">JsonDataSetSchema data stream that will be used to serialize the schema information from the DataSet.</param> /// <param name="dsSchema">DataSet with schema to be serialized to JsonDataSetSchema data stream.</param> public virtual void SerializeDataSetSchema(Stream streamSchema, DataSet dsSchema) { try { JsonDataSetSchema dataSchema = SerializeDataSetSchema(dsSchema); JsonDataWriter jwrtSchema = new JsonDataWriter(streamSchema); //Writes opening '{' object bracket of Json file. jwrtSchema.WriteBeginObject(); //"DataSet": "DataSetName", jwrtSchema.WritePropDataValue("DataSet", dataSchema.DataSetName, true); //"Tables": [ jwrtSchema.WritePropertyName("Tables"); jwrtSchema.WriteBeginArray(true); //Loops through each table schema and writes it to the stream. for (int iTableIndex = 0; iTableIndex < dataSchema.Tables.Count; iTableIndex++) { SerializeTableSchema(jwrtSchema, dataSchema.Tables[iTableIndex]); if (iTableIndex < dataSchema.Tables.Count - 1) { //Writes comma field delimiter after the table end object to separate each table record. jwrtSchema.WriteFieldDelimiter(); } else { //The final table record will have a closing object bracket without a comma. jwrtSchema.WriteNewLine(); } }//next //Writes closing ']' of tables array. jwrtSchema.WriteEndArray(); //Writes closing '}' object bracket of Json file. jwrtSchema.WriteEndObject(); } catch (Exception err) { ErrorHandler.ShowErrorMessage(err, "Error in SerializeDataSetSchema Overload 2 function of JsonDataSetSchemaSerializer class."); } }
/// <summary> /// Parses and deserializes the JsonDataSetSchema data stream into a DataSet object with the schema information stored in the data stream. /// The JsonDataSetSchemaParser class will be used to extract all the schema data in the stream into a JsonDataSetSchema object which then /// will be deserialized and used to initialize the schema of the DataSet object. /// </summary> /// <param name="streamSchema">The stream that will access the JsonDataSetSchema file data.</param> /// <param name="dsData">A reference to a DataSet that will be initialized with the schema data contained in the JsonDataSetSchema data stream.</param> /// <returns></returns> public virtual bool DeserializeJsonDataSetSchema(Stream streamSchema, ref DataSet dsData) { JsonDataSetSchemaParser jsParser = null; try { jsParser = new JsonDataSetSchemaParser(streamSchema); JsonDataSetSchema dataSchema = jsParser.ParseJsonSchema(); if (dataSchema == null) { throw new InvalidDataException("Json Data Schema file invalid. Failed to parse Json Schema file."); } dsData = new DataSet(); dsData.DataSetName = dataSchema.DataSetName; foreach (JsonTableSchema tblSchema in dataSchema.Tables) { DataTable dtTable = DeserializeJsonTableSchema(tblSchema); dsData.Tables.Add(dtTable); }//next tblSchema return(true); } catch (Exception err) { ErrorHandler.ShowErrorMessage(err, "Error in DeserializeJsonDataSetSchema Overload 1 function of JsonDataSetSchemaSerializer class."); return(false); } finally { if (jsParser != null) { jsParser.Dispose(); } } }