public override object Deserialize(SuperToken obj, SuperJsonSerializer serializer) { var typedObj = (SuperObject)obj; var delegateId = Guid.Parse(typedObj.TypedValue["ID"].Value.ToString()); var delegateType = Type.GetType(typedObj.TypedValue["DelegateType"].Value.ToString(), true); var argTypes = ((SuperArray)typedObj.TypedValue["ArgumentTypes"]) .TypedValue.Select(t => Type.GetType(t.Value.ToString())).ToArray(); var returnType = Type.GetType(typedObj.TypedValue["ReturnType"].Value.ToString()); Type generatedInterfaceType = null; if (returnType == typeof(void)) { generatedInterfaceType = Utils.GetActionWrapper <IDelegateActionWrapperBase>(argTypes); } else { generatedInterfaceType = Utils.GetFuncWrapper <IDelegateFuncWrapperBase>(returnType, argTypes); } //get interface instance from super using delegate id var inst = mSuper.GetInstance(generatedInterfaceType, delegateId); //create delegate from known method var result = Delegate.CreateDelegate(delegateType, inst, nameof(IDelegateActionWrapper.Invoke)); return(result); }
internal CallResult Call(CallInfo info) { var obj = info.ClassID == Guid.Empty ? mRegistred[info.TypeName] : mIdRegistred[info.ClassID]; var declaringType = obj?.GetType (); var method = declaringType.GetMethod(info.MethodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); object result = null; var call = new Action(delegate { result = method.Invoke(obj, method.GetParameters() .Select((p, i) => SuperJsonSerializer.ConvertResult(info.Args[i], p.ParameterType)).ToArray()); }); mContext.Invoke(call); if (method.ReturnType != result?.GetType()) result = new DeclarationWrapper(result, method.ReturnType); return new CallResult { CallID = info.CallID, Result = result }; }
public override CallResult SendCall(CallInfo info) { var tcs = new TaskCompletionSource <CallResult>(); mWaitingCalls.TryAdd(info.CallID, tcs); var method = info.GetMethodInfo(); for (int i = 0; i < info.Args.Length; i++) { var declaredType = method.GetParameters()[i].ParameterType; if (declaredType == info.Args[i].GetType()) { continue; } var declarationWrapper = new DeclarationWrapper(info.Args[i], declaredType); info.Args[i] = declarationWrapper; } SendData(info); mContext.Wait(tcs.Task); var result = tcs.Task.Result; result.Result = SuperJsonSerializer.ConvertResult(result.Result, method.ReturnType); return(result); }
public override SuperToken Serialize(object obj, Type declaredType, SuperJsonSerializer serializer) { if (obj is bool) { return(new SuperBool((bool)obj)); } return(new SuperNumber((double)SuperJsonSerializer.ConvertResult(obj, typeof(double)))); }
public override object Deserialize(SuperToken obj, SuperJsonSerializer serializer) { var typed = (SuperObject)obj; var instID = Guid.Parse(typed.TypedValue["ID"].Value.ToString()); var interfaceType = Type.GetType(typed.TypedValue["InterfaceType"].Value.ToString()); return(mSuper.GetInstance(interfaceType, instID)); }
public override object Deserialize(SuperToken obj, SuperJsonSerializer serializer) { var typed = (SuperObject)obj; var id = Guid.Parse(typed.TypedValue["ID"].Value.ToString()); var resultType = Type.GetType(typed.TypedValue["ResultType"].Value.ToString()); var tcsType = typeof(TaskCompletionSource <>).MakeGenericType(resultType); dynamic tcs = Activator.CreateInstance(tcsType); mSuper.WaitingTasks[id] = tcs; return(tcs.Task); }
public override SuperToken Serialize(object obj, Type declaredType, SuperJsonSerializer serializer) { var arr = (Array)obj; var resultList = new List <SuperToken>(); for (var i = 0; i < arr.Length; i++) { var elem = arr.GetValue(i); resultList.Add(serializer.Serialize(elem, obj.GetType().GetElementType())); } return(new SuperArray(resultList.ToArray())); }
public override SuperToken Serialize(object obj, Type declaredType, SuperJsonSerializer serializer) { var typedValue = (Task)obj; var resultType = obj.GetType().GetGenericArguments()[0]; var id = Guid.NewGuid(); typedValue.ContinueWith(t => { var taskResult = new TaskResult { TaskID = id }; if (t.IsCanceled) { taskResult.Status = TaskCompletionStatus.Canceled; taskResult.Result = false; } else if (t.IsFaulted) { taskResult.Status = TaskCompletionStatus.Exception; taskResult.Result = t.Exception; } else { var resultProp = t.GetType().GetProperty(nameof(Task <object> .Result)); taskResult.Status = TaskCompletionStatus.Result; taskResult.Result = resultProp.GetValue(t); } mSuper.SendData(taskResult); }); var result = new SuperObject { TypedValue = { { "$type", new SuperString("TaskWrapper") }, { "ID", new SuperString(id.ToString()) }, { "ResultType", new SuperString(resultType.AssemblyQualifiedName) } } }; return(result); }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); var obj = new TestClass(); for (int i = 0; i < 150; i++) { obj.ProjectOrders[i] = 0; obj.ChapterSctolls[i.ToString()] = i % 4; obj.ReadChapterUrls.Add(i.ToString()); } var ser = new SuperJsonSerializer(); var data = ser.Serialize(obj); }
public override SuperToken Serialize(object obj, Type declaredType, SuperJsonSerializer serializer) { var result = new SuperObject(); var id = Guid.NewGuid(); var typed = (Delegate)obj; var delegateParameters = typed.Method.GetParameters().Select(p => p.ParameterType).ToArray(); Type wrapperType = null; if (typed.Method.ReturnType == typeof(void)) { wrapperType = Utils.GetActionWrapper <DelegateActionWrapperBase>(delegateParameters); } else { wrapperType = Utils.GetFuncWrapper <DelegateFuncWrapperBase>(typed.Method.ReturnType, delegateParameters); } mSuper.Register(Activator.CreateInstance(wrapperType, typed), id); result.TypedValue.Add("$type", new SuperString("DelegateWrapper")); result.TypedValue.Add("ID", new SuperString(id.ToString())); result.TypedValue.Add("DelegateType", new SuperString(typed.GetType().AssemblyQualifiedName)); var listArgs = new List <SuperToken>(); foreach (var param in typed.Method.GetParameters()) { listArgs.Add(new SuperString(param.ParameterType.AssemblyQualifiedName)); } var args = new SuperArray(listArgs.ToArray()); result.TypedValue.Add("ArgumentTypes", args); result.TypedValue.Add("ReturnType", new SuperString(typed.Method.ReturnType.AssemblyQualifiedName)); return(result); }
public override SuperToken Serialize(object obj, Type declaredType, SuperJsonSerializer serializer) { var result = new SuperObject { TypedValue = { { "$type", new SuperString("InterfaceWrapper") } } }; var regType = declaredType; var regInst = obj; var registrationID = Guid.NewGuid(); var registerMethod = typeof(Super).GetMethod(nameof(Super.Register), BindingFlags.Instance | BindingFlags.Public); registerMethod = registerMethod.MakeGenericMethod(regType); registerMethod.Invoke(mSuper, new[] { regInst, registrationID }); result.TypedValue.Add("ID", new SuperString(registrationID.ToString())); result.TypedValue.Add("InterfaceType", new SuperString(regType.AssemblyQualifiedName)); return(result); }
void ReciveData(TaskResult result) { var tcs = WaitingTasks[result.TaskID]; switch (result.Status) { case TaskCompletionStatus.Canceled: tcs.SetCanceled(); break; case TaskCompletionStatus.Exception: tcs.SetException((Exception)result.Result); break; case TaskCompletionStatus.Result: tcs.SetResult(SuperJsonSerializer.ConvertResult(result.Result, tcs.GetType().GetGenericArguments()[0])); break; default: throw new Exception("Holy Moly!"); } }
public override SuperToken Serialize(object obj, Type declaredType, SuperJsonSerializer serializer) { var typed = (DeclarationWrapper)obj; return(serializer.Serialize(typed.Instance, Type.GetType(typed.TypeName))); }
public override SuperToken Serialize(object obj, Type declaredType, SuperJsonSerializer serializer) { return(new SuperString((string)obj)); }
public override SuperToken Serialize(object obj, Type declaredType, SuperJsonSerializer serializer) { return(new SuperNull()); }