/// <summary> /// 序列化 /// </summary> /// <param name="p_value"></param> /// <param name="p_writer"></param> public static void Serialize(object p_value, Utf8JsonWriter p_writer) { if (p_value == null) { p_writer.WriteNullValue(); return; } // 自定义序列化 if (p_value is IRpcJson xrs) { xrs.WriteRpcJson(p_writer); return; } Type tp = p_value.GetType(); // 匿名对象转成Dict if (tp.Namespace == null && tp.IsNotPublic) { Dict dt = new Dict(); foreach (var prop in tp.GetProperties()) { dt[prop.Name] = prop.GetValue(p_value); } ((IRpcJson)dt).WriteRpcJson(p_writer); return; } switch (Type.GetTypeCode(tp)) { case TypeCode.String: p_writer.WriteStringValue((string)p_value); break; case TypeCode.Boolean: p_writer.WriteBooleanValue((bool)p_value); break; case TypeCode.DateTime: p_writer.WriteStringValue((DateTime)p_value); break; case TypeCode.Int64: p_writer.WriteNumberValue((long)p_value); break; case TypeCode.Int32: p_writer.WriteNumberValue((int)p_value); break; case TypeCode.Double: p_writer.WriteNumberValue((double)p_value); break; case TypeCode.Single: p_writer.WriteNumberValue((float)p_value); break; case TypeCode.Decimal: p_writer.WriteNumberValue((decimal)p_value); break; case TypeCode.UInt64: p_writer.WriteNumberValue((ulong)p_value); break; case TypeCode.UInt32: p_writer.WriteNumberValue((uint)p_value); break; case TypeCode.Int16: p_writer.WriteNumberValue((short)p_value); break; case TypeCode.UInt16: p_writer.WriteNumberValue((ushort)p_value); break; case TypeCode.Byte: p_writer.WriteNumberValue((byte)p_value); break; case TypeCode.SByte: p_writer.WriteNumberValue((sbyte)p_value); break; case TypeCode.Object: if (tp == typeof(byte[])) { // 字节数组需要base64编码 p_writer.WriteStringValue(Convert.ToBase64String((byte[])p_value)); } else if (p_value is IEnumerable) { // 列表 SerializeArray((IEnumerable)p_value, p_writer); } else { SerializeObject(p_value, p_writer); } break; default: throw new Exception("未支持序列化类型" + tp.FullName); } }
/// <summary> /// 根据Api名称构造调用时的json串 /// </summary> /// <param name="p_methodName">Api名称</param> /// <returns></returns> public static string GetMethodCall(string p_methodName) { ApiMethod sm = GetMethod(p_methodName); if (sm == null) { return(null); } List <object> funParams = new List <object>(); foreach (ParameterInfo param in sm.Method.GetParameters()) { Type type = param.ParameterType; if (type == typeof(string)) { funParams.Add(""); } else if (type == typeof(Dict) || type == typeof(object)) { Dict dict = new Dict(); dict["键"] = "值"; funParams.Add(dict); } else if (type == typeof(Table)) { Table tbl = new Table { { "列名1" } }; tbl.NewRow(""); funParams.Add(tbl); } else if (type == typeof(bool)) { funParams.Add(true); } else if (type == typeof(Int32)) { funParams.Add(0); } else if (type == typeof(Int64)) { funParams.Add(0L); } else if (type == typeof(double)) { funParams.Add(0d); } else if (type == typeof(DateTime)) { funParams.Add(DateTime.Now); } else if (type == typeof(byte[])) { funParams.Add(new byte[0]); } else if (type == typeof(string[])) { funParams.Add(new string[1] { "" }); } else if (type == typeof(List <string>)) { List <string> ls = new List <string>(); ls.Add(""); funParams.Add(ls); } else if (type == typeof(List <int>)) { funParams.Add(new List <int>() { 1, 2, 3, 4 }); } else if (type == typeof(List <double>)) { funParams.Add(new List <double>() { 200.0d, 100d, 50.123d, 123.45d }); } else if (type.IsArray) { funParams.Add(Activator.CreateInstance(type, 1)); } else { funParams.Add(Activator.CreateInstance(type)); } } // 序列化Json串 RPC 调用请求,含有缩进 return(RpcKit.GetCallString(p_methodName, funParams, true)); }
/// <summary> /// 生成保存实体的sql及参数,返回Dict的结构:包含两个键text和params,text值为sql字符串,params值为sql参数Dict /// </summary> /// <param name="p_row">待保存的行</param> /// <returns>返回提交参数</returns> internal Dict GetSaveSql(Row p_row) { // 不再重复判断 //if (p_row == null) // throw new Exception(_saveError); //// 无需保存 //if (!p_row.IsAdded && !p_row.IsChanged) // return null; // 检查是否包含主键 foreach (var col in PrimaryKey) { if (!p_row.Contains(col.Name)) { throw new Exception(string.Format(_primaryError, p_row.GetType().Name, col.Name)); } } Dict dtParams = new Dict(); StringBuilder sql = new StringBuilder(); if (p_row.IsAdded) { // 插入 StringBuilder insertCol = new StringBuilder(); StringBuilder insertVal = new StringBuilder(); foreach (var col in PrimaryKey.Concat(Columns)) { if (!p_row.Contains(col.Name)) { continue; } if (insertCol.Length > 0) { insertCol.Append(","); } insertCol.Append(col.Name); if (insertVal.Length > 0) { insertVal.Append(","); } insertVal.Append("@"); insertVal.Append(col.Name); dtParams[col.Name] = p_row[col.Name]; } sql.Append("insert into "); sql.Append(Name); sql.Append("("); sql.Append(insertCol.ToString()); sql.Append(") values ("); sql.Append(insertVal.ToString()); sql.Append(")"); } else { // 更新 StringBuilder updateVal = new StringBuilder(); StringBuilder whereVal = new StringBuilder(); int updateIndex = 1; foreach (var col in PrimaryKey.Concat(Columns)) { if (!p_row.Contains(col.Name) || !p_row.Cells[col.Name].IsChanged) { continue; } // 只更新变化的列 if (updateVal.Length > 0) { updateVal.Append(", "); } updateVal.Append(col.Name); updateVal.Append("=@"); updateVal.Append(updateIndex); // 更新主键时避免重复 dtParams[updateIndex.ToString()] = p_row[col.Name]; updateIndex++; } // 主键 foreach (var col in PrimaryKey) { if (whereVal.Length > 0) { whereVal.Append(" and "); } whereVal.Append(col.Name); whereVal.Append("=@"); whereVal.Append(col.Name); // 主键可能被更新 dtParams[col.Name] = p_row.Cells[col.Name].OriginalVal; } sql.Append("update "); sql.Append(Name); sql.Append(" set "); sql.Append(updateVal.ToString()); sql.Append(" where "); sql.Append(whereVal.ToString()); } Dict dt = new Dict(); dt["text"] = sql.ToString(); dt["params"] = dtParams; return(dt); }