1234567891011121314151617181920212223242526272829303132333435363738394041 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using parkMonitor.entity;
- //using System.Runtime.Serialization.Formatters.Binary;
- namespace parkMonitor.Tools
- {
- public class JsonByByteToObjectTools
- {
- public static byte[] ObjToJsonByte(AbstractMessage message)
- {
- string objstr = JsonConvert.SerializeObject(message);
- byte[] buffer = Encoding.UTF8.GetBytes(objstr);
- return buffer;
- }
- public static AbstractMessage JsonByteToObj<AbstractMessage>(byte[] bstr) where AbstractMessage : class
- {
- int count = 0;
- for (int i = 0; i < bstr.Length; i++)
- {
- if (bstr[i] == 0)
- {
- count = i;
- break;
- }
- }
- string str = new UTF8Encoding().GetString(bstr);
- AbstractMessage msg = JsonConvert.DeserializeObject<AbstractMessage>(str);
- return msg;
- }
- }
- }
|