public static string RenameCodeClass(string input) { string result = ""; result = Regex.Replace(input, "[^\\w_-]", ""); if (result.Contains <char>('-')) { string str = ""; result.Split(new char[] { '-' }).ToList <string>().ForEach((string x) => { if (x.Length > 1) { str = string.Concat(str, ConvertJsonToPoco.UpperCaseFirst(x)); } }); result = str; } if (result.Contains <char>('\u005F')) { string str1 = ""; result.Split(new char[] { '\u005F' }).ToList <string>().ForEach((string x) => { if (x.Length > 1) { str1 = string.Concat(str1, ConvertJsonToPoco.UpperCaseFirst(x)); } }); result = str1; } return(ConvertJsonToPoco.UpperCaseFirst(result)); }
private void AddJsonObject(JObject jObj, ref string className, string culture, string parent = "") { className = ConvertJsonToPoco.Classes.RenameCodeClass(className); if (jObj == null) { return; } ConvertJsonToPoco.ClassCode existed = null; string old = className; if (!this.classes.ContainsKey(className)) { this.classes.Add(className, new ConvertJsonToPoco.ClassCode() { Parent = parent }); } else if (this.classes[className].Parent != parent) { existed = this.classes[className]; while (this.classes.ContainsKey(className) && this.classes[className].Parent != parent) { char last = className.Last <char>(); if (char.IsNumber(last)) { className = className.Substring(0, className.Length - 1); } else { last = '0'; } int num = int.Parse(last.ToString(CultureInfo.InvariantCulture)) + 1; className = string.Concat(className, num.ToString()); } if (!this.classes.ContainsKey(className)) { this.classes.Add(className, new ConvertJsonToPoco.ClassCode() { Parent = parent }); } } foreach (KeyValuePair <string, JToken> item in jObj) { if (item.Value.Type == JTokenType.Object) { string name = ConvertJsonToPoco.UpperCaseFirst(item.Key); this.AddJsonObject(item.Value as JObject, ref name, culture, className); this.AddJsonProperty(className, item.Key, this.ToCsharpType(item.Value, name)); } else if (item.Value.Type != JTokenType.Array) { this.AddJsonProperty(className, item.Key, this.ToCsharpType(item.Value, "")); } else { JArray array = item.Value as JArray; JToken first = null; if (array.First != null) { first = ( from x in array orderby x.ToString().Length descending select x).First <JToken>(); } string type = this.ToCsharpType(item.Value, ConvertJsonToPoco.UpperCaseFirst(ConvertJsonToPoco.Singularize(item.Key, culture))); if (first == null || first.Type != JTokenType.Object) { type = this.ToCsharpType(item.Value, this.ToCsharpType(first, "")); } else { string name = ConvertJsonToPoco.UpperCaseFirst(ConvertJsonToPoco.Singularize(item.Key, culture)); this.AddJsonObject(first as JObject, ref name, culture, className); type = this.ToCsharpType(item.Value, name); } this.AddJsonProperty(className, item.Key, type); } } KeyValuePair <string, ConvertJsonToPoco.ClassCode> current = this.classes.LastOrDefault <KeyValuePair <string, ConvertJsonToPoco.ClassCode> >(); if (!current.Value.Properties.Keys.All <string>((string x) => Regex.IsMatch(x, "^\\d"))) { if (existed != null) { KeyValuePair <string, ConvertJsonToPoco.ClassCode> last = this.classes.LastOrDefault <KeyValuePair <string, ConvertJsonToPoco.ClassCode> >(); if (last.Value.Properties.DictionaryEqual <string, string>(existed.Properties)) { className = old; this.classes.Remove(last.Key); } } return; } List <IGrouping <string, KeyValuePair <string, string> > > r = ( from x in current.Value.Properties group x by x.Value).ToList <IGrouping <string, KeyValuePair <string, string> > >(); if (r.Count != 1) { className = "Dictionary<string,string>"; } else { className = string.Concat("Dictionary<string,", r.First <IGrouping <string, KeyValuePair <string, string> > >().Key, ">"); } this.classes.Remove(current.Key); }