/// <summary> /// Parses the data stored for the current field of the current record of the DataTable in the JsonDataSet data stream being parsed by the /// JsonDataSetParser class. The function will parse the data string stored in the appropriate format in the JsonDataSet data file and extract /// it into the appropriate .Net data type based on the .Net data type specified in the function's parameter (which should match that of the /// DataTable column being loaded). All types of data will be processed and appropriately converted, including null values, escape characters, /// numeric, string, date/time and binary values. /// </summary> /// <param name="dataType">The .Net Data Type of the field to be extracted from the JsonDataSet data.</param> /// <param name="iCurParseIndex">The current index of the JsonDataSet data stream being parsed.</param> /// <returns></returns> protected virtual object GetFieldValue(Type dataType, ref int iCurParseIndex) { try { object oValue = null; string strValue = ""; char chrNext = (char)0; bool blParseData = true; if (m_strJsonData[iCurParseIndex] == 'n') { strValue = m_strJsonData.Substring(iCurParseIndex, iCurParseIndex + 4); if (strValue == "null") { iCurParseIndex += 4; return(DBNull.Value); } //end if } //end if if (dataType == typeof(string) || dataType == typeof(DateTime)) { iCurParseIndex++; while (blParseData) { chrNext = m_strJsonData[iCurParseIndex]; if (chrNext == '\\') { string strEscapeChar = chrNext.ToString() + m_strJsonData[iCurParseIndex + 1].ToString(); if (JsonDataUtils.IsEscapeChar(strEscapeChar)) { strValue += JsonDataUtils.ConvertEscapeChar(strEscapeChar); iCurParseIndex += 2; } else { strValue += chrNext; iCurParseIndex++; }//end if } else if (chrNext == '\"') { blParseData = false; iCurParseIndex++; } else { strValue += chrNext; iCurParseIndex++; } //end if } //end while } else { while (blParseData) { chrNext = m_strJsonData[iCurParseIndex]; if (chrNext == ',' || chrNext == ' ' || chrNext == '}') { blParseData = false; } else { strValue += chrNext; iCurParseIndex++; } //end if } //end while } //end if if (dataType == typeof(string)) { return(strValue); } else { oValue = JsonDataUtils.ConvertDataFromJson(strValue, dataType); return(oValue); }//end if } catch (Exception err) { ErrorHandler.ShowErrorMessage(err, "Error in GetFieldValue function of JsonDataSetParser class."); return(null); } }
/// <summary> /// An internal function that will use a JsonDataWriter object to serialize the data stored in a JsonTableSchema object to a JsonDataSetSchema /// data stream or file. The JsonTableSchema object will have been previously loaded before the function is called and the JsonDataWriter /// will be positioned at the next table schema record in the data stream. /// </summary> /// <param name="jwrtSchema">JsonDataWriter object used for serializing the schema information of the DataTable to the JsonDataSetSchema /// data stream.</param> /// <param name="tblSchema">The JsonTableSchema object containing all the schema information of a DataTable to serialize to the JsonDataSetSchema /// data stream.</param> protected virtual void SerializeTableSchema(JsonDataWriter jwrtSchema, JsonTableSchema tblSchema) { try { //Writes opening '{' object bracket of either Tables schema array (of DataSet Schema file) or of the entire table schema file. jwrtSchema.WriteBeginObject(); //"Table": "Table Name", jwrtSchema.WritePropDataValue("Table", tblSchema.TableName, true); //"Columns": [ jwrtSchema.WritePropertyName("Columns"); jwrtSchema.WriteBeginArray(true); //Loops through each column of the table schema and serializes the data to the stream. for (int iColIndex = 0; iColIndex < tblSchema.Columns.Count; iColIndex++) { JsonColumnSchema colSchema = tblSchema.Columns[iColIndex]; //Write opening '{' object bracket for the column schema. jwrtSchema.WriteBeginObject(); jwrtSchema.WritePropDataValue("ColumnName", colSchema.ColumnName, true); jwrtSchema.WritePropDataValue("DataType", JsonDataUtils.ConvertToJsonDataType(colSchema.DataType), true); jwrtSchema.WritePropDataValue("PrimaryKey", colSchema.PrimaryKey, true); jwrtSchema.WritePropDataValue("Unique", colSchema.Unique, true); jwrtSchema.WritePropDataValue("MaxLength", colSchema.MaxLength, true); jwrtSchema.WritePropDataValue("AllowDBNull", colSchema.AllowDBNull, true); jwrtSchema.WritePropDataValue("AutoIncrement", colSchema.AutoIncrement, true); jwrtSchema.WritePropDataValue("AutoIncrementSeed", colSchema.AutoIncrementSeed, true); jwrtSchema.WritePropDataValue("AutoIncrementStep", colSchema.AutoIncrementStep, true); jwrtSchema.WritePropDataValue("Caption", colSchema.Caption, true); jwrtSchema.WritePropDataValue("DateTimeMode", colSchema.DateTimeMode.ToString(), true); if (colSchema.DefaultValue != null) { jwrtSchema.WritePropDataValue("DefaultValue", colSchema.DefaultValue.ToString(), true); } else { jwrtSchema.WritePropDataNullValue("DefaultValue", true); } jwrtSchema.WritePropDataValue("ReadOnly", colSchema.ReadOnly); //Write closing '}' object bracket for the column schema. jwrtSchema.WriteEndObject(); if (iColIndex < tblSchema.Columns.Count - 1) { //Writes comma field delimiter after the column end object to separate each column record. jwrtSchema.WriteFieldDelimiter(); } else { //The final column record will have a closing object bracket without a comma. jwrtSchema.WriteNewLine(); } }//next iColIndex //Writes closing ']' of columns array. jwrtSchema.WriteEndArray(); //Writes closing '}' object bracket of either Tables schema array (of DataSet Schema file) or of the entire table schema file. jwrtSchema.WriteEndObject(); } catch (Exception err) { ErrorHandler.ShowErrorMessage(err, "Error in SerializeTableSchema Overload 2 function of JsonDataSetSchemaSerializer class."); } }
/// <summary> /// Parses the current JsonColumnSchema information of the current JsonTableSchema record contained in the JsonDataSetSchema data stream /// linked o the JsonDataSetSchemaParser class. The function will read the column schema and extract the column information into the /// JstonColumnSchema object passed to the function. All field schema properties and values will be converted into the appropriate format and /// loaded into the JsonColumnSchema object associatd with the current field of the current table being parsed in the JsonDataSchemaParser /// class. /// </summary> /// <param name="schemaJsonCol">A reference to the JsonColumnSchema object to be loaded with the column schema information from the /// JsonDataSetSchema file being parsed in the class.</param> /// <param name="iCurParseIndex">The current index of the JsonDataSetSchema data stream being parsed.</param> /// <returns></returns> protected virtual bool ParseJsonColumnSchema(ref JsonColumnSchema schemaJsonCol, ref int iCurParseIndex) { try { schemaJsonCol = new JsonColumnSchema(); string strExtractData = ""; int iEndParseIndex = 0; bool blColSchemaItemFound = true; while (blColSchemaItemFound) { iCurParseIndex = m_strJsonDSSchema.IndexOf('\"', iCurParseIndex) + 1; iEndParseIndex = m_strJsonDSSchema.IndexOf('\"', iCurParseIndex); string strColSchemaItem = m_strJsonDSSchema.Substring(iCurParseIndex, iEndParseIndex - iCurParseIndex).ToUpper(); iCurParseIndex = m_strJsonDSSchema.IndexOf(':', iCurParseIndex) + 1; switch (strColSchemaItem) { case "COLUMNNAME": case "DATATYPE": case "CAPTION": case "EXPRESSION": case "DATETIMEMODE": iCurParseIndex = m_strJsonDSSchema.IndexOf('\"', iCurParseIndex) + 1; iEndParseIndex = m_strJsonDSSchema.IndexOf('\"', iCurParseIndex); strExtractData = m_strJsonDSSchema.Substring(iCurParseIndex, iEndParseIndex - iCurParseIndex); if (strColSchemaItem == "COLUMNNAME") { schemaJsonCol.ColumnName = strExtractData; } else if (strColSchemaItem == "DATATYPE") { schemaJsonCol.DataType = JsonDataUtils.ConvertFromJsonDataType(strExtractData); } else if (strColSchemaItem == "CAPTION") { schemaJsonCol.Caption = strExtractData; } else if (strColSchemaItem == "EXPRESSION") { schemaJsonCol.Expression = strExtractData; } else if (strColSchemaItem == "DATETIMEMODE") { schemaJsonCol.DateTimeMode = JsonDataUtils.ConvertToADODataSetDateTimeEnum(strExtractData); } iCurParseIndex = iEndParseIndex + 1; break; case "PRIMARYKEY": case "UNIQUE": case "ALLOWDBNULL": case "AUTOINCREMENT": case "READONLY": while (m_strJsonDSSchema[iCurParseIndex] != 'T' && m_strJsonDSSchema[iCurParseIndex] != 't' && m_strJsonDSSchema[iCurParseIndex] != 'F' && m_strJsonDSSchema[iCurParseIndex] != 'f') { iCurParseIndex++; } bool blValue; if (m_strJsonDSSchema[iCurParseIndex].ToString().ToUpper() == "T") { blValue = true; iCurParseIndex += "true".Length; } else { blValue = false; iCurParseIndex += "false".Length; } //end if if (strColSchemaItem == "PRIMARYKEY") { schemaJsonCol.PrimaryKey = blValue; } else if (strColSchemaItem == "UNIQUE") { schemaJsonCol.Unique = blValue; } else if (strColSchemaItem == "ALLOWDBNULL") { schemaJsonCol.AllowDBNull = blValue; } else if (strColSchemaItem == "AUTOINCREMENT") { schemaJsonCol.AutoIncrement = blValue; } else if (strColSchemaItem == "READONLY") { schemaJsonCol.ReadOnly = blValue; } break; case "MAXLENGTH": case "AUTOINCREMENTSEED": case "AUTOINCREMENTSTEP": while (m_strJsonDSSchema[iCurParseIndex] == ' ' || m_strJsonDSSchema[iCurParseIndex] == '\"') { iCurParseIndex++; } string strVal = ""; while (char.IsNumber(m_strJsonDSSchema[iCurParseIndex])) { strVal += m_strJsonDSSchema[iCurParseIndex]; iCurParseIndex++; } //end while schemaJsonCol.MaxLength = Convert.ToInt32(strVal); break; case "DEFAULTVALUE": while (m_strJsonDSSchema[iCurParseIndex] != ' ') { iCurParseIndex++; } string strDefaultValue = ""; bool blDataFound = true; while (blDataFound) { if (m_strJsonDSSchema[iCurParseIndex] != ',' && m_strJsonDSSchema[iCurParseIndex] != '}' && m_strJsonDSSchema[iCurParseIndex] != ' ') { strDefaultValue += m_strJsonDSSchema[iCurParseIndex]; } else { blDataFound = false; } } //end while schemaJsonCol.DefaultValue = JsonDataSetParser.ParseVarTypeJsonData(strDefaultValue); break; }//end switch if (m_strJsonDSSchema.IndexOf(',', iCurParseIndex) != -1) { if (m_strJsonDSSchema.IndexOf(',', iCurParseIndex) < m_strJsonDSSchema.IndexOf('}', iCurParseIndex)) { iCurParseIndex = m_strJsonDSSchema.IndexOf(',', iCurParseIndex) + 1; } else { blColSchemaItemFound = false; } } else { blColSchemaItemFound = false; } if (!blColSchemaItemFound) { iCurParseIndex = m_strJsonDSSchema.IndexOf('}', iCurParseIndex) + 1; } //end if } //end while bool blColFound = MoveNextSchemaRecord(ref iCurParseIndex); return(blColFound); } catch (Exception err) { ErrorHandler.ShowErrorMessage(err, "Error in ParseJsonColumnSchema function of JsonDataSetSchemaParser class."); return(false); } }