Exemple #1
0
        static void Main(string[] args)
        {
            Tracer tracer = new Tracer();
            Foo    foo    = new Foo(tracer);

            foo.MyMethod();

            TraceResult traceResult = tracer.GetTraceResult();

            ISerialize serialize1, serialize2;

            serialize1 = new XMLSerialize();

            byte[] data1 = serialize1.Serialize(traceResult);
            Output.WriteToStream(data1, Console.OpenStandardOutput());
            using (FileStream fs = new FileStream("output.txt", FileMode.OpenOrCreate, FileAccess.Write))
                Output.WriteToStream(data1, fs);

            serialize2 = new JSONSerialize();
            byte[] data2 = serialize2.Serialize(traceResult);
            Output.WriteToStream(data2, Console.OpenStandardOutput());
            using (FileStream fs = new FileStream("output.txt", FileMode.Open, FileAccess.Write))
                Output.WriteToStream(data2, fs);

            Console.ReadLine();
        }
        public void CheckJSONAssemblyModel()
        {
            JSONSerialize jsonSerialize = new JSONSerialize();

            if (File.Exists(JSONSerializer.Properties.Settings.Default.SaveFileName))
            {
                File.Delete(JSONSerializer.Properties.Settings.Default.SaveFileName);
            }

            jsonSerialize.Save(assemblyModel);
            AssemblyModel newAssemblyModel = jsonSerialize.Read();

            Assert.IsTrue(newAssemblyModel.Name.Equals(assemblyModel.Name));
        }
Exemple #3
0
 /// <summary>
 /// Serialize an object into JSON
 /// </summary>
 /// <typeparam name="T">The type of object to be serialized</typeparam>
 /// <param name="toWrite">the object to be serialized</param>
 /// <returns>A string containing the serialized JSON</returns>
 public static string Write <T>(T toWrite)
 {
     return(JSONSerialize.WriteValue(typeof(T), toWrite));
 }
        private void ServiceThread()
        {
            while (true)
            {
                try
                {
                    if (!isrun)
                    {
                        Thread.CurrentThread.Abort();
                        return;
                    }

                    HttpListenerContext context = listener.GetContext();
                    context.Response.StatusCode = 200;

                    string str_resp_err = "";
                    string str_resp     = "";
                    byte[] buffer       = new byte[1024];

                    #region test
                    //UploadFlowDataReqEntity testentity = new UploadFlowDataReqEntity();
                    //testentity.action = "uploadflowdata";
                    //testentity.TerData = new List<UpLoadFlowDataEntity>();
                    //UpLoadFlowDataEntity testdata1 = new UpLoadFlowDataEntity();
                    //testdata1.terid = "1";
                    //testdata1.flowvalue = "100.123";
                    //testdata1.flowinverted = "2344.0";
                    //testdata1.flowinstant = "233.23";
                    //testdata1.collTime = DateTime.Now.ToString();
                    //testentity.TerData.Add(testdata1);
                    //string strttt = SmartWaterSystem.JSONSerialize.JsonSerialize<UploadFlowDataReqEntity>(testentity);
                    //long timestamp = 0;
                    //TimeSpan tsp = (TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Now) - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)));
                    //timestamp = (long)tsp.TotalMilliseconds;
                    //string md51 = MD5Encrypt.MD5(System.Web.HttpUtility.UrlEncode(strttt + timestamp + Settings.Instance.GetString(SettingKeys.HTTPMD5Key)).ToLower());
                    //HTTPEntity ttpentity = new HTTPEntity();
                    //ttpentity.timestamp = timestamp.ToString();
                    //ttpentity.Params = strttt;
                    //ttpentity.digest = md51;
                    //string reqtemp = SmartWaterSystem.JSONSerialize.JsonSerialize<HTTPEntity>(ttpentity);
                    //string urltemp = System.Web.HttpUtility.UrlEncode(reqtemp, Encoding.UTF8);
                    #endregion

                    if (context.Request.HttpMethod.ToLower().Equals("get"))
                    {
                        //GET请求处理
                        str_resp_err = "不支持GET方法";
                    }
                    else if (context.Request.HttpMethod.ToLower().Equals("post"))
                    {
                        //这是在POST请求时必须传参的判断默认注释掉
                        if (!context.Request.HasEntityBody)
                        {
                            str_resp_err = "请传入参数";
                        }
                        else
                        {
                            //POST请求处理
                            Stream SourceStream = context.Request.InputStream;
                            int    readcount    = -1;

                            List <byte> lstbytes = new List <byte>();
                            while ((readcount = SourceStream.Read(buffer, 0, 1024)) > 0)
                            {
                                for (int i = 0; i < readcount; i++)
                                {
                                    lstbytes.Add(buffer[i]);
                                }
                            }
                            string strrequest = Encoding.UTF8.GetString(lstbytes.ToArray());
                            try
                            {
                                strrequest = System.Web.HttpUtility.UrlDecode(strrequest, Encoding.UTF8);                  //UrlDecode
                                OnReceiveMsg("接收到请求[" + DateTime.Now.ToString() + "]:" + strrequest);
                                HTTPEntity httpentity = JSONSerialize.JsonDeserialize_Newtonsoft <HTTPEntity>(strrequest); //jsondeSerialize

                                if (httpentity == null)
                                {
                                    str_resp_err = "无效数据,解析失败!";
                                    goto err;
                                }
                                if (string.IsNullOrEmpty(httpentity.Params))
                                {
                                    str_resp_err = "无效params,解析失败!";
                                    goto err;
                                }
                                if (string.IsNullOrEmpty(httpentity.timestamp))
                                {
                                    str_resp_err = "时间戳不能为空!";
                                    goto err;
                                }
                                else
                                {
                                    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
                                    DateTime        dtime     = startTime.AddMilliseconds(Convert.ToDouble(httpentity.timestamp));
                                    TimeSpan        ts        = DateTime.Now - dtime;
                                    if (Math.Abs(ts.TotalMinutes) > Settings.Instance.GetInt(SettingKeys.HTTPReqSuviceTime))
                                    {
                                        str_resp_err = "该请求已失效!";
                                        goto err;
                                    }
                                }

                                //MD5(body +时间戳+Key)
                                string md5 = MD5Encrypt.MD5(System.Web.HttpUtility.UrlEncode(httpentity.Params + httpentity.timestamp + Settings.Instance.GetString(SettingKeys.HTTPMD5Key)).ToLower());
                                if (md5 != httpentity.digest)
                                {
                                    str_resp_err = "MD5校验失败!";
                                    goto err;
                                }

                                str_resp_err = "无效action";
                                string action = "";

                                foreach (Match m in Regex.Matches(httpentity.Params, "\"action\" ?: ?\"(?<title>.*?)\"", RegexOptions.IgnoreCase))
                                {
                                    if (m.Success)
                                    {
                                        action = m.Groups["title"].Value;
                                    }
                                }

                                try
                                {
                                    switch (action)
                                    {
                                    case "getgroups":
                                        str_resp_err = "";
                                        GetGroupsRespEntity getgrouprespentity = bll.GetGroupsInfo();
                                        str_resp = SmartWaterSystem.JSONSerialize.JsonSerialize <GetGroupsRespEntity>(getgrouprespentity);
                                        break;

                                    case "uploadnoisedata":      //上传噪声数据
                                        str_resp_err = "";
                                        UploadNoiseDataReqEntity parmentity       = SmartWaterSystem.JSONSerialize.JsonDeserialize_Newtonsoft <UploadNoiseDataReqEntity>(httpentity.Params);
                                        HTTPRespEntity           uploadrespentity = bll.UploadGroups(parmentity.TerData);
                                        str_resp = SmartWaterSystem.JSONSerialize.JsonSerialize <HTTPRespEntity>(uploadrespentity);
                                        break;

                                    case "uploadflowdata":      //未使用
                                        str_resp_err = "";
                                        UploadFlowDataReqEntity parmflowentity = SmartWaterSystem.JSONSerialize.JsonDeserialize_Newtonsoft <UploadFlowDataReqEntity>(httpentity.Params);

                                        if (parmflowentity != null && parmflowentity.TerData != null)
                                        {
                                            foreach (UpLoadFlowDataEntity flowentity in parmflowentity.TerData)
                                            {
                                                GPRSFlowFrameDataEntity framedata = new GPRSFlowFrameDataEntity();
                                                framedata.TerId      = flowentity.terid;
                                                framedata.ModifyTime = DateTime.Now;
                                                framedata.Frame      = "";
                                                GPRSFlowDataEntity data = new GPRSFlowDataEntity();
                                                data.Forward_FlowValue = Convert.ToDouble(flowentity.flowvalue);
                                                data.Reverse_FlowValue = Convert.ToDouble(flowentity.flowinverted);
                                                data.Instant_FlowValue = Convert.ToDouble(flowentity.flowinstant);
                                                data.ColTime           = Convert.ToDateTime(flowentity.collTime);
                                                framedata.lstFlowData.Add(data);
                                                GlobalValue.Instance.GPRS_FlowFrameData.Enqueue(framedata);      //通知存储线程处理
                                            }
                                        }
                                        GlobalValue.Instance.SocketSQLMag.Send(SQLType.InsertFlowValue);

                                        uploadrespentity      = new HTTPRespEntity();
                                        uploadrespentity.code = 1;
                                        str_resp = SmartWaterSystem.JSONSerialize.JsonSerialize <HTTPRespEntity>(uploadrespentity);
                                        break;
                                    }
                                }
                                catch
                                {
                                    str_resp_err = "解析异常";
                                }
                            }
                            catch (Exception ex)
                            {
                                str_resp_err = "无效参数类型";
                            }
                        }
                    }

err:
                    if (!string.IsNullOrEmpty(str_resp_err) || string.IsNullOrEmpty(str_resp))
                    {
                        HTTPRespEntity respent = new HTTPRespEntity();
                        respent.code = -1;
                        if (!string.IsNullOrEmpty(str_resp_err))
                        {
                            respent.msg = str_resp_err;
                        }
                        respent.data = "";

                        str_resp = SmartWaterSystem.JSONSerialize.JsonSerialize <HTTPRespEntity>(respent);
                    }
                    OnReceiveMsg(DateTime.Now.ToString() + " 响应内容:" + str_resp);
                    str_resp = System.Web.HttpUtility.UrlEncode(str_resp);
                    byte[] buffer_resp = Encoding.UTF8.GetBytes(str_resp);
                    context.Response.OutputStream.Write(buffer_resp, 0, buffer_resp.Length);
                    context.Response.OutputStream.Flush();
                    context.Response.OutputStream.Close();

                    context.Response.Close();
                }
                catch (Exception ex)
                {
                    logger.ErrorException("ServiceThread", ex);
                }
            }
        }