示例#1
0
 public ReadOnlySpan <byte> SerializeReadOnlySpan <T>(T obj, JT808Version version = JT808Version.JTT2013, int minBufferSize = 4096)
 {
     byte[] buffer = JT808ArrayPool.Rent(minBufferSize);
     try
     {
         var formatter = jT808Config.GetMessagePackFormatter <T>();
         JT808MessagePackWriter jT808MessagePackWriter = new JT808MessagePackWriter(buffer, version);
         formatter.Serialize(ref jT808MessagePackWriter, obj, jT808Config);
         return(jT808MessagePackWriter.FlushAndGetEncodingReadOnlySpan());
     }
     finally
     {
         JT808ArrayPool.Return(buffer);
     }
 }
示例#2
0
        public static byte[] Serialize <T>(T obj, int minBufferSize = 1024)
        {
            var formatter = JT808FormatterExtensions.GetFormatter <T>();

            byte[] buffer = JT808ArrayPool.Rent(minBufferSize);
            try
            {
                var len = formatter.Serialize(ref buffer, 0, obj);
                return(buffer.AsSpan(0, len).ToArray());
            }
            finally
            {
                JT808ArrayPool.Return(buffer);
            }
        }
示例#3
0
 public byte [] Serialize <T>(T obj, int minBufferSize = 4096)
 {
     byte[] buffer = JT808ArrayPool.Rent(minBufferSize);
     try
     {
         var formatter = jT808Config.GetMessagePackFormatter <T>();
         JT808MessagePackWriter jT808MessagePackWriter = new JT808MessagePackWriter(buffer);
         formatter.Serialize(ref jT808MessagePackWriter, obj, jT808Config);
         return(jT808MessagePackWriter.FlushAndGetEncodingArray());
     }
     finally
     {
         JT808ArrayPool.Return(buffer);
     }
 }
示例#4
0
 public dynamic Deserialize(ReadOnlySpan <byte> bytes, Type type, JT808Version version = JT808Version.JTT2013, int minBufferSize = 4096)
 {
     byte[] buffer = JT808ArrayPool.Rent(minBufferSize);
     try
     {
         var formatter = jT808Config.GetMessagePackFormatterByType(type);
         JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes, version);
         if (CheckPackageType(type))
         {
             jT808MessagePackReader.Decode(buffer);
         }
         return(JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(formatter, ref jT808MessagePackReader, jT808Config));
     }
     finally
     {
         JT808ArrayPool.Return(buffer);
     }
 }
示例#5
0
 public T Deserialize <T>(ReadOnlySpan <byte> bytes, JT808Version version = JT808Version.JTT2013, int minBufferSize = 4096)
 {
     byte[] buffer = JT808ArrayPool.Rent(minBufferSize);
     try
     {
         JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes, version);
         if (CheckPackageType(typeof(T)))
         {
             jT808MessagePackReader.Decode(buffer);
         }
         var formatter = jT808Config.GetMessagePackFormatter <T>();
         return(formatter.Deserialize(ref jT808MessagePackReader, jT808Config));
     }
     finally
     {
         JT808ArrayPool.Return(buffer);
     }
 }
示例#6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="bytes"></param>
 /// <param name="version"></param>
 /// <param name="options"></param>
 /// <param name="minBufferSize"></param>
 /// <returns></returns>
 public byte[] AnalyzeJsonBuffer(ReadOnlySpan <byte> bytes, JT808Version version = JT808Version.JTT2013, JsonWriterOptions options = default, int minBufferSize = 8096)
 {
     byte[] buffer = JT808ArrayPool.Rent(minBufferSize);
     try
     {
         JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes, version);
         jT808MessagePackReader.Decode(buffer);
         using MemoryStream memoryStream     = new MemoryStream();
         using Utf8JsonWriter utf8JsonWriter = new Utf8JsonWriter(memoryStream, options);
         jT808Package.Analyze(ref jT808MessagePackReader, utf8JsonWriter, jT808Config);
         utf8JsonWriter.Flush();
         return(memoryStream.ToArray());
     }
     finally
     {
         JT808ArrayPool.Return(buffer);
     }
 }
示例#7
0
        /// <summary>
        /// 转义
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public static ReadOnlySpan <byte> JT808DeEscape(ReadOnlySpan <byte> buf)
        {
            var bytes = JT808ArrayPool.Rent(buf.Length);

            try
            {
                int i      = 0;
                int offset = 0;
                int len    = 0 + buf.Length;
                while (i < len)
                {
                    if (buf[i] == 0x7d)
                    {
                        if (len > i + 1)
                        {
                            if (buf[i + 1] == 0x01)
                            {
                                bytes[offset++] = 0x7d;
                                i++;
                            }
                            else if (buf[i + 1] == 0x02)
                            {
                                bytes[offset++] = 0x7e;
                                i++;
                            }
                            else
                            {
                                bytes[offset++] = buf[i];
                            }
                        }
                    }
                    else
                    {
                        bytes[offset++] = buf[i];
                    }
                    i++;
                }
                return(bytes.AsSpan(0, offset).ToArray());
            }
            finally
            {
                JT808ArrayPool.Return(bytes);
            }
        }