DataTabletojsonUtility
/*-----------------------------------------------------------------
File Name : Utility.cs
Created By : Navin Kumar
Created on : Sep-2017
Revision History:
------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using System.Web.Script.Serialization;
namespace InsiderTrading.Infrastructure.Common
{/// <summary>
/// used to convert DataTable to Json.
/// </summary>
public sealed class Utility
{/// <summary>
///
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public static string DataTableToJsonWithStringBuilder(DataTable table)
{
var jsonString = new StringBuilder();
if (table.Rows.Count > 0)
{
jsonString.Append("[");
for (int i = 0; i < table.Rows.Count; i++)
{
jsonString.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
if (j < table.Columns.Count - 1)
{
jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
+ "\":" + "\""
+ table.Rows[i][j].ToString() + "\",");
}
else if (j == table.Columns.Count - 1)
{
jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
+ "\":" + "\""
+ table.Rows[i][j].ToString() + "\"");
}
}
if (i == table.Rows.Count - 1)
{
jsonString.Append("}");
}
else
{
jsonString.Append("},");
}
}
jsonString.Append("]");
}
return jsonString.ToString();
}
/// <summary>
/// Datas the table to json with java script serializer.
/// </summary>
/// <param name="table">The table.</param>
/// <returns></returns>
public static string DataTableToJsonWithJavaScriptSerializer(DataTable table)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow;
foreach (DataRow row in table.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return jsSerializer.Serialize(parentRow);
}
/// <summary>
/// Datas the table to json with json net.
/// </summary>
/// <param name="table">The table.</param>
/// <returns></returns>
public static string DataTableToJsonWithJsonNet(DataTable table)
{
string jsonString = string.Empty;
//jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(table);
return jsonString;
}
/// <summary>
/// Use For Url Decode
/// </summary>
/// <param name="url"></param>
/// <param name="param"></param>
/// <returns> string</returns>
public static string urlDecode(string url, string param)
{
string paramsv = "";
string[] paramparts = url.Split('?');
if (paramparts.Length > 1)
{
string[] paramsq = paramparts[1].Split('&');
if (param == "loginId")
{
paramsv = Utility.After(paramsq[0], param + "=");
}
else if (param == "domain")
{
paramsv = Utility.After(paramsq[1], param + "=");
}
else if (param == "Token")
{
paramsv = Utility.After(paramsq[2], param + "=");
}
else if (param == "RMode")
{
paramsv = Utility.After(paramsq[3], param + "=");
}
else if (param == "RComp")
{
paramsv = Utility.After(paramsq[4], param + "=");
}
else if (param == "IdeaID")
{
paramsv = Utility.After(paramsq[5], param + "=");
}
}
return paramsv;
}
/// <summary>
/// Use For character addition after some character
/// </summary>
/// <param name="value"></param>
/// <param name="a"></param>
/// <returns> string</returns>
public static string After(string value, string a)
{
string[] splitstr = value.Split('=');
string[] splitparam = a.Split('=');
if (splitstr[0] == splitparam[0])
{
int posA = value.LastIndexOf(a);
if (posA == -1)
{
return "";
}
int adjustedPosA = posA + a.Length;
if (adjustedPosA >= value.Length)
{
return "";
}
return value.Substring(adjustedPosA);
}
else
{
return "";
}
}
/// <summary>
/// use for Decrypt a string value
/// </summary>
/// <param name="cipherString"></param>
/// <param name="useHashing"></param>
/// <returns> string</returns>
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
//get the byte code of the string
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
//Key like a public key
string key = "CPPKey";
if (useHashing)
{
//if hashing was used get the hash code with regards to your key
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
//release any resource held by the MD5CryptoServiceProvider
hashmd5.Clear();
}
else
{
//if hashing was not implemented get the byte code of the key
keyArray = UTF8Encoding.UTF8.GetBytes(key);
}
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
//set the secret key for the tripleDES algorithm
tdes.Key = keyArray;
//mode of operation. there are other 4 modes.
//We choose ECB(Electronic code Book)
tdes.Mode = CipherMode.ECB;
//padding mode(if any extra byte added)
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(
toEncryptArray, 0, toEncryptArray.Length);
//Release resources held by TripleDes Encryptor
tdes.Clear();
//return the Clear decrypted TEXT
return UTF8Encoding.UTF8.GetString(resultArray);
}
public static string ReturnJsonStringFromTable(DataTable Dt)
{
JavaScriptSerializer serializer = new JavaScriptSerializer() { MaxJsonLength = 500000000 };
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
try
{
foreach (DataRow dr in Dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in Dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
}
catch (Exception)
{
throw;
}
return serializer.Serialize(rows);
}
public static string EncryptString(string Message)
{
byte[] Results;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
string SecretKey = "smgmapint#c26";
string Token = Message + SecretKey;
Results = UTF8.GetBytes(Token);
string encoded = Convert.ToBase64String(Results);
//var base64EncodedBytes = System.Convert.FromBase64String(encoded);
//return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
encoded = encoded.Replace("=", "_");
return encoded;
}
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
// Get the key from config file
string key = "CPPKey";
//System.Windows.Forms.MessageBox.Show(key);
//If hashing use get hashcode regards to your key
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
//Always release the resources and flush data
// of the Cryptographic service provide. Best Practice
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
//set the secret key for the tripleDES algorithm
tdes.Key = keyArray;
//mode of operation. there are other 4 modes.
//We choose ECB(Electronic code Book)
tdes.Mode = CipherMode.ECB;
//padding mode(if any extra byte added)
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
//transform the specified region of bytes array to resultArray
byte[] resultArray =
cTransform.TransformFinalBlock(toEncryptArray, 0,
toEncryptArray.Length);
//Release resources held by TripleDes Encryptor
tdes.Clear();
//Return the encrypted data into unreadable string format
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
}
}
Comments
Post a Comment