protected static T InternalDeserialize <T>(Dictionary <string, string> keyval) { Dictionary <string, object> root = new Dictionary <string, object>(); foreach (KeyValuePair <string, string> k in keyval) { string itemKey = k.Key.Replace("\"", ""); string[] parts = itemKey.Split('.'); object current = root; int counter = 0; foreach (string p in parts) { counter++; // last one if (counter == parts.Length) { // an empty list; if (k.Value == "[]") { if (current is Dictionary <string, object> ) { (current as Dictionary <string, object>).Add(p, new List <int>()); } else if (current is List <object> ) { (current as List <object>).Add(new List <int>()); } } // an empty object; else if (k.Value == "{}") { if (current is Dictionary <string, object> ) { (current as Dictionary <string, object>).Add(p, new Dictionary <string, object>()); } else if (current is List <object> ) { (current as List <object>).Add(new Dictionary <string, object>()); } } // a value else { object value = null; string lowerval = k.Value.ToLower(); if (lowerval == "true") { value = true; } else if (lowerval == "false") { value = false; } else if (lowerval == "null") { value = null; } else if (Misc.IsInt(lowerval)) { value = Int32.Parse(lowerval); } else if (Misc.IsDouble(lowerval)) { value = Double.Parse(lowerval); } else { string strval = k.Value; if (strval.StartsWith("\"")) { strval = strval.Substring(1); } if (strval.EndsWith("\"")) { strval = strval.Substring(0, strval.Length - 1); } value = strval; } if (current is Dictionary <string, object> ) { (current as Dictionary <string, object>).Add(p, value); } else if (current is List <object> ) { (current as List <object>).Add(value); } } } // in middle, .. key.key .. // current is dict, child is dict else if (Misc.TryParseInt(p, -1) == -1 && Misc.TryParseInt(parts[counter], -1) == -1) { // current must be a dict in this situation... // x.y.0.z.w,5 // { // x: { // y: [ // { // z : { // w : 5; Dictionary <string, object> dcurrent = current as Dictionary <string, object>; if (dcurrent.ContainsKey(p)) { current = dcurrent[p]; } else { dcurrent.Add(p, new Dictionary <string, object>()); current = dcurrent[p]; } } // in middle, .. key.number ... // current is dict, child is list else if (Misc.TryParseInt(p, -1) == -1 && Misc.TryParseInt(parts[counter], -1) >= 0) { Dictionary <string, object> dcurrent = current as Dictionary <string, object>; if (dcurrent.ContainsKey(p)) { current = dcurrent[p]; } else { dcurrent.Add(p, new List <object>()); current = dcurrent[p]; } } // in middle, .. number.key .. // current is list, child is dict else if (Misc.TryParseInt(p, -1) >= 0 && Misc.TryParseInt(parts[counter], -1) == -1) { int index = Misc.TryParseInt(p, -1); List <object> lcurrent = current as List <object>; if (lcurrent.Count > index) { current = lcurrent[index]; } else { for (int i = lcurrent.Count; i < index + 1; i++) { lcurrent.Add(new Dictionary <string, object>()); } current = lcurrent[index]; } } // in middle, .. number.number .. // current is list, child is list else if (Misc.TryParseInt(p, -1) >= 0 && Misc.TryParseInt(parts[counter], -1) >= 0) { int index = Misc.TryParseInt(p, -1); List <object> lcurrent = current as List <object>; if (lcurrent.Count > index) { current = lcurrent[index]; } else { for (int i = lcurrent.Count; i < index + 1; i++) { lcurrent.Add(new List <object>()); } current = lcurrent[index]; } } } } string json = JsonObjectSerializer.Serialize(root); return(JsonObjectSerializer.Deserialize <T>(json)); }
protected static string InternalSerialize(object x) { Dictionary <string, string> dict = new Dictionary <string, string>(); string s = JsonObjectSerializer.Serialize(x); // ensure proper separation s = s.Replace("}", " } "); s = s.Replace("{", " { "); s = s.Replace("]", " ] "); s = s.Replace("[", " [ "); s = s.Replace(",", " , "); bool inq = false; bool backslash = false; for (int i = 0; i < s.Length; i++) { if (s[i] == '\\') { backslash = true; } if (backslash) { continue; } if (s[i] == '"') { inq = !inq; } if (s[i] == ' ' && inq) { if (i < s.Length - 1) { s = s.Substring(0, i) + '+' + s.Substring(i + 1); } else { s = s.Substring(0, i) + '+'; } } if (s[i] == ':' && !inq) { if (i < s.Length - 1) { s = s.Substring(0, i) + ' ' + s.Substring(i + 1); } else { s = s.Substring(0, i) + ' '; } } } string[] segments = s.Split(new char[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); List <string> prefixes = new List <string>(); string currentName = ""; int counter = 0; bool inarray = false; bool arrayHasName = false; string lastSegment = ""; foreach (string str in segments) { string xkey = ""; string xval = ""; try { // begin a sub-object if (str == "{") { if (currentName != "") { prefixes.Add(currentName); currentName = ""; } else if (inarray == true) { prefixes.Add(counter.ToString()); counter++; } continue; } // end a sub-object if (str == "}") { if (lastSegment == "{") { xkey = string.Join(".", prefixes.ToArray()); xval = "{}"; } if (prefixes.Count > 0) { prefixes.RemoveAt(prefixes.Count - 1); } continue; } // begin an array if (str == "[") { arrayHasName = false; if (currentName != "") { prefixes.Add(currentName); currentName = ""; arrayHasName = true; } counter = 0; inarray = true; continue; } // end an array if (str == "]") { if (lastSegment == "[") { xkey = string.Join(".", prefixes.ToArray()); xval = "[]"; } if (currentName != "") { xkey = string.Join(".", prefixes.ToArray()) + "." + counter.ToString(); xval = currentName; } if (arrayHasName) { if (prefixes.Count > 0) { prefixes.RemoveAt(prefixes.Count - 1); } } counter = 0; inarray = false; continue; } if (str == ":") { continue; } // key/value if (currentName != "") { if (str == ",") { if (inarray) { xkey = string.Join(".", prefixes.ToArray()) + "." + counter.ToString(); xval = currentName; counter++; } else { continue; } } else { xkey = string.Join(".", prefixes.ToArray()) + "." + currentName; if (prefixes.Count == 0) { xkey = xkey.Substring(1); } xval = str; } currentName = ""; continue; } if (str == ",") { continue; } if (currentName == "") { currentName = str; } } finally { lastSegment = str; if (xkey != "") { xkey = xkey.Replace("\"", ""); xkey = xkey.Replace("+", " "); xkey = "\"" + xkey + "\""; xval = xval.Replace("+", " "); Console.WriteLine("Adding: " + xkey); dict.Add(xkey, xval); } } } string header = string.Join(",", dict.Keys.ToArray()); string value = string.Join(",", dict.Values.ToArray()); return(header + "\n" + value);; }