public static Dictionary <K, V> Deserialize <K, V>(string state, Func <string, K> fkey, Func <string, V> fVal) { Dictionary <K, V> dict = new Dictionary <K, V>(); List <string> tokens = DeserializeTokens(state); int start = 1; int pos = start; do { pos = GetClosingIndex(state, start, '(', ')'); tokens = DeserializeTokens(state.Substring(start, pos - start)); if (tokens[1].Contains(":")) { int indextype = tokens[1].IndexOf(':'); string type = tokens[1].Substring(0, indextype); Type t = GetType(type); object o = Activator.CreateInstance(t); IDescription idescr = o as IDescription; if (idescr != null) { idescr.Deserialize(tokens[1].Substring(indextype + 1)); dict.Add(fkey(tokens[0]), (V)idescr); } } else { dict.Add(fkey(tokens[0]), fVal(tokens[1])); } start = pos + 1; }while (++pos < state.Length); return(dict); }
public static List <T> Deserialize <T>(string state, Func <string, T> f) { List <T> list = new List <T>(); int start = 1; int pos = start; do { if (state[pos] == ':') { string type = state.Substring(start, pos - start); Type t = GetType(type); object o = Activator.CreateInstance(t); IDescription idescr = o as IDescription; if (idescr != null) { int st = pos + 1; int end = GetClosingIndex(state, st, '{', '}'); idescr.Deserialize(state.Substring(st, end - st)); list.Add((T)idescr); pos = end; } } else if (state[pos] == '(') { pos = GetClosingIndex(state, start, '(', ')'); list.Add(f(state.Substring(start, pos - start))); start = pos + 1; } else if (state[pos] == '[') { pos = GetClosingIndex(state, start, '[', ']'); list.Add(f(state.Substring(start, pos - start))); start = pos + 1; } else if (state[pos] == '{') { pos = GetClosingIndex(state, start, '{', '}'); list.Add(f(state.Substring(start, pos - start))); start = pos + 1; } else if (state[pos] == ',' || state[pos] == ')' || state[pos] == ']' || state[pos] == '}') { if (state.Substring(start, pos - start).Any()) { list.Add(f(state.Substring(start, pos - start))); } start = pos + 1; } }while (++pos < state.Length); return(list); }