Пример #1
0
        /// <summary>
        /// Convert to a Hashtable with key/value pairs from a Text string.
        /// ~ character in key/value is changed to ;
        /// ` character in key/value is changed to ,
        /// </summary>
        /// <param name="str">The string</param>
        /// <returns>Hashtable</returns>
        public static Hashtable FromText(string str)
        {
            Hashtable result = new Hashtable();

            if (str == null || str.Trim().Length == 0)
            {
                throw new ArgumentNullException("Cannot convert a NULL/empty string");
            }
            str = str.Trim();

            //json = AbstractStringUtils.Replace(json, '\r', ' ');
            //json = AbstractStringUtils.Replace(json, '\n', ' ');

            string[] keyVals = str.Split(new char[] { ';' });
            foreach (string keyValPair in keyVals)
            {
                if (keyValPair.Trim().Length == 0)
                {
                    continue;
                }
                string[] parts = keyValPair.Split(new char[] { '=' });
                parts[0] = AbstractStringUtils.Replace(parts[0], '\"', ' ').Trim();
                parts[1] = AbstractStringUtils.Replace(parts[1], '\"', ' ').Trim();
                //character replacements
                parts[0] = AbstractStringUtils.Replace(parts[0], '~', ';');
                parts[0] = AbstractStringUtils.Replace(parts[0], '`', ',');
                parts[1] = AbstractStringUtils.Replace(parts[1], '~', ';');
                parts[1] = AbstractStringUtils.Replace(parts[1], '`', ',');
                result.Add(parts[0], parts[1]);
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Convert a Hashtable entries into Text. The format is key=value;key=value
        /// Only number, string, boolean and datetime values are supported.
        /// Datetime is converted to ddMMyyyy HHmmss format in string
        /// If you want to pass an array, pass a comma separated list of values (as string).
        /// Semi-Colon (;) in data are converted to ~ and comma(,) in data are converted to `
        /// </summary>
        /// <param name="ht">Hashtable with Key/Value pairs</param>
        /// <returns>string</returns>
        public static string ToText(Hashtable ht)
        {
            if (ht == null || ht.Count == 0)
            {
                return("");
            }
            StringBuilder sb    = new StringBuilder();
            int           count = 0;

            foreach (string key in ht.Keys)
            {
                if (count > 0)
                {
                    sb.Append(";");
                }
                Object value  = ht[key];
                string modKey = AbstractStringUtils.Replace(key, ';', '~');
                modKey = AbstractStringUtils.Replace(modKey, ',', '`');
                if (value != null)
                {
                    sb.Append("\"").Append(modKey).Append("\"=").Append(TextResult.AsString(value));
                }
                else
                {
                    sb.Append("\"").Append(modKey).Append("\"= \"\"");
                }
                count++;
            }
            return(sb.ToString());
        }
Пример #3
0
        /// <summary>
        /// Convert a Hashtable entries into JSON
        /// Only number, string, boolean and datetime values are supported.
        /// Datetime is converted to ddMMyyyy HHmmss format in string
        /// If you want to pass an array, pass a comma separated list of values (as string).
        /// Colon (:) in data are converted to ~ and comma(,) in data are converted to `
        /// </summary>
        /// <param name="ht">Hashtable with Key/Value pairs</param>
        /// <returns>string</returns>
        public static string ToJSON(Hashtable ht)
        {
            if (ht == null || ht.Count == 0)
            {
                return("{}");
            }
            StringBuilder sb    = new StringBuilder();
            int           count = 0;

            sb.Append("{");
            foreach (string key in ht.Keys)
            {
                if (count > 0)
                {
                    sb.Append(",");
                }
                Object value  = ht[key];
                string modKey = AbstractStringUtils.Replace(key, ':', '~');
                modKey = AbstractStringUtils.Replace(modKey, ',', '`');
                if (value != null)
                {
                    sb.Append("\"").Append(modKey).Append("\":").Append(JSONResult.ToJSONString(value));
                }
                else
                {
                    sb.Append("\"").Append(modKey).Append("\": \"\"");
                }
                count++;
            }
            sb.Append("}");
            return(sb.ToString());
        }
Пример #4
0
        /// <summary>
        /// Convert an object into string representation.
        /// Dictionary, Enumerable, DictionaryEntry and Class are
        /// simply converted to string...(not supported essentially)
        /// DateTime is always returned as ddMMyyyy HHmmss
        /// Semi-Colon (;) in data are converted to ~ and comma(,) in data are converted to `
        /// </summary>
        /// <param name="obj">Object instance to convert to JSON</param>
        /// <returns>string</returns>
        protected static string AsString(Object obj)
        {
            if (obj == null)
            {
                return("");
            }
            Type   type     = obj.GetType();
            string objAsStr = obj.ToString();

            objAsStr = AbstractStringUtils.Replace(objAsStr, ';', '~');
            objAsStr = AbstractStringUtils.Replace(objAsStr, ',', '`');

            switch (type.Name)
            {
            case "Boolean":
                return(((bool)obj) ? "true" : "false");

            case "String":
            case "Char":
            case "Guid":
                return("\"" + objAsStr + "\"");

            case "Single":
            case "Double":
            case "Decimal":
            case "Float":
            case "Byte":
            case "SByte":
            case "Int16":
            case "UInt16":
            case "Int32":
            case "UInt32":
            case "Int64":
            case "UInt64":
                return(objAsStr);

            case "DateTime":
                DateTime dt = (DateTime)obj;
                return("\"" + AbstractStringUtils.PadLeft(dt.Day.ToString(), '0', 2) +
                       AbstractStringUtils.PadLeft(dt.Month.ToString(), '0', 2) +
                       AbstractStringUtils.PadLeft(dt.Year.ToString(), '0', 4) + " " +
                       AbstractStringUtils.PadLeft(dt.Hour.ToString(), '0', 2) +
                       AbstractStringUtils.PadLeft(dt.Minute.ToString(), '0', 2) +
                       AbstractStringUtils.PadLeft(dt.Second.ToString(), '0', 2) + "\"");

            default:
                return("\"" + objAsStr + "\"");
            }
        }
        /// <summary>
        /// Convert a string representing a hexadecimal number to UInt32
        /// </summary>
        /// <param name="hexStr">The hex string</param>
        /// <returns>The unsigned integer representation of the hex string</returns>
        public static UInt32 Hex2UInt32(string hexStr)
        {
            if (hexStr == null)
            {
                throw new ArgumentNullException("Hex string is null");
            }
            if (hexStr.Trim().Length == 0)
            {
                throw new ArgumentException("Hex string is empty");
            }

            hexStr = (hexStr.Trim().IndexOf("0x") == 0) ? hexStr.Trim().Substring(2).ToUpper() : hexStr.Trim().ToUpper();
            if (hexStr.Length > 8) //more than 4 bytes or 8-nibbles
            {
                throw new ArgumentException("Hex string too large for conversion");
            }

            ArrayList allowedHexChars = new ArrayList();

            char[] hexCharArr = "0123456789ABCDEF".ToCharArray();
            foreach (char hexChar in hexCharArr)
            {
                allowedHexChars.Add(hexChar);
            }

            //check if the hex string contains dis-allowed characters
            char[] hexChars = AbstractStringUtils.PadLeft(hexStr, '0', 8).ToCharArray();
            foreach (char hex in hexChars)
            {
                if (!allowedHexChars.Contains(hex))
                {
                    throw new ArgumentException("Input string does not represent hexadecimal characters");
                }
            }

            UInt32 mul    = 1;
            UInt32 result = 0;

            for (int count = hexChars.Length - 1; count >= 0; --count)
            {
                result += (UInt32)(mul * (allowedHexChars.IndexOf(hexChars[count])));
                mul     = (uint)(mul * allowedHexChars.Count);
            }

            return(result);
        }
Пример #6
0
        /// <summary>
        /// Convert to a Hashtable with key/value pairs from a JSON string.
        /// ~ character in key/value is changed to :
        /// ` character in key/value is changed to ,
        /// </summary>
        /// <param name="json">The JSON string</param>
        /// <returns>Hashtable</returns>
        public static Hashtable FromJSON(string json)
        {
            Hashtable result = new Hashtable();

            if (json == null || json.Trim().Length == 0)
            {
                throw new ArgumentNullException("Cannot convert a NULL/empty string");
            }
            json = json.Trim();
            if (json.IndexOf("{") == 0)
            {
                json = json.Substring(1);
            }
            if (json.LastIndexOf("}") == json.Length - 1)
            {
                json = json.Substring(0, json.LastIndexOf("}"));
            }
            //json = AbstractStringUtils.Replace(json, '\r', ' ');
            //json = AbstractStringUtils.Replace(json, '\n', ' ');

            string[] keyVals = json.Split(new char[] { ',' });
            foreach (string keyValPair in keyVals)
            {
                if (keyValPair.Trim().Length == 0)
                {
                    continue;
                }
                string[] parts = keyValPair.Split(new char[] { ':' });
                parts[0] = AbstractStringUtils.Replace(parts[0], '\"', ' ').Trim();
                parts[1] = AbstractStringUtils.Replace(parts[1], '\"', ' ').Trim();
                //character replacements
                parts[0] = AbstractStringUtils.Replace(parts[0], '~', ':');
                parts[0] = AbstractStringUtils.Replace(parts[0], '`', ',');
                parts[1] = AbstractStringUtils.Replace(parts[1], '~', ':');
                parts[1] = AbstractStringUtils.Replace(parts[1], '`', ',');
                result.Add(parts[0], parts[1]);
            }

            return(result);
        }