public object Encode(object o, Type type, JsonTypeMap typeMap, Func <object, bool, object> enc) { var oDict = (IDictionary)o; var jsTypeName = typeMap.GetTypeName(type); var comparerObj = type.InvokeMember("Comparer", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty, null, o, new object[0]); var comparer = enc(comparerObj, false); var keys = new List <object>(oDict.Count); var values = new List <object>(oDict.Count); foreach (DictionaryEntry entry in oDict) { var key = enc(entry.Key, false); var value = enc(entry.Value, false); keys.Add(key); values.Add(value); } var ret = new Dictionary <string, object> { { "_", jsTypeName }, { "__", 2 }, { "a", keys }, { "b", values }, { "c", comparer }, { "d", typeMap.GetTypeName(type) }, }; return(ret); }
public object Encode(object o, Type type, JsonTypeMap typeMap, Func <object, bool, object> enc) { var oList = (IList)o; var listElementType = type.GetGenericArguments()[0]; var innerArray = (Array)Activator.CreateInstance(listElementType.MakeArrayType(), oList.Count); int index = 0; foreach (var element in oList) { innerArray.SetValue(element, index++); } var innerArrayEnc = enc(innerArray, false); var jsTypeName = typeMap.GetTypeName(type); var jsFieldName = typeMap.GetFieldName(type, "array"); var ret = new Dictionary <string, object> { { "_", jsTypeName }, { jsFieldName, innerArrayEnc } }; return(ret); }