コード例 #1
0
ファイル: JT809Serializer.cs プロジェクト: yezy-h/JT809
 public byte[] AnalyzeJsonBuffer <T>(ReadOnlySpan <byte> bytes, JsonWriterOptions options = default, int minBufferSize = 8096)
 {
     byte[] buffer = JT809ArrayPool.Rent(minBufferSize);
     try
     {
         JT809MessagePackReader jT809MessagePackReader = new JT809MessagePackReader(bytes);
         if (CheckPackageType(typeof(T)))
         {
             jT809MessagePackReader.Decode(buffer);
         }
         var analyze = jT809Config.GetAnalyze <T>();
         using (MemoryStream memoryStream = new MemoryStream())
             using (Utf8JsonWriter utf8JsonWriter = new Utf8JsonWriter(memoryStream, options))
             {
                 if (!CheckPackageType(typeof(T)))
                 {
                     utf8JsonWriter.WriteStartObject();
                 }
                 analyze.Analyze(ref jT809MessagePackReader, utf8JsonWriter, jT809Config);
                 if (!CheckPackageType(typeof(T)))
                 {
                     utf8JsonWriter.WriteEndObject();
                 }
                 utf8JsonWriter.Flush();
                 return(memoryStream.ToArray());
             }
     }
     finally
     {
         JT809ArrayPool.Return(buffer);
     }
 }
コード例 #2
0
 public JT809Package Deserialize(ReadOnlySpan <byte> bytes, int minBufferSize = 4096)
 {
     byte[] buffer = JT809ArrayPool.Rent(minBufferSize);
     try
     {
         JT809MessagePackReader jT809MessagePackReader = new JT809MessagePackReader(bytes);
         jT809MessagePackReader.Decode(buffer);
         //todo:JT809PackageFormatter
         return(default);
コード例 #3
0
 public JT809HeaderPackage HeaderDeserialize(ReadOnlySpan <byte> bytes, int minBufferSize = 4096)
 {
     byte[] buffer = JT809ArrayPool.Rent(minBufferSize);
     try
     {
         JT809MessagePackReader jT808MessagePackReader = new JT809MessagePackReader(bytes);
         jT808MessagePackReader.Decode(buffer);
         return(JT809HeaderPackageFormatter.Instance.Deserialize(ref jT808MessagePackReader, jT809Config));
     }
     finally
     {
         JT809ArrayPool.Return(buffer);
     }
 }
コード例 #4
0
 public byte[] Serialize <T>(T obj, int minBufferSize = 4096)
 {
     byte[] buffer = JT809ArrayPool.Rent(minBufferSize);
     try
     {
         JT809MessagePackWriter jT809MessagePackWriter = new JT809MessagePackWriter(buffer);
         JT809MessagePackFormatterExtensions.GetFormatter <T>().Serialize(ref jT809MessagePackWriter, obj, jT809Config);
         return(jT809MessagePackWriter.FlushAndGetEncodingArray());
     }
     finally
     {
         JT809ArrayPool.Return(buffer);
     }
 }
コード例 #5
0
 public byte[] Serialize(JT809Package jT809Package, int minBufferSize = 4096)
 {
     byte[] buffer = JT809ArrayPool.Rent(minBufferSize);
     try
     {
         JT809MessagePackWriter jT809MessagePackWriter = new JT809MessagePackWriter(buffer);
         JT809PackageFormatter.Instance.Serialize(ref jT809MessagePackWriter, jT809Package, jT809Config);
         return(jT809MessagePackWriter.FlushAndGetEncodingArray());
     }
     finally
     {
         JT809ArrayPool.Return(buffer);
     }
 }
コード例 #6
0
ファイル: JT809Serializer.cs プロジェクト: ironmp/JT809
        public static byte[] Serialize <T>(T obj, int minBufferSize = 1024)
        {
            var formatter = JT809FormatterExtensions.GetFormatter <T>();

            byte[] buffer = JT809ArrayPool.Rent(minBufferSize);
            try
            {
                var len = formatter.Serialize(ref buffer, 0, obj);
                return(buffer.AsSpan(0, len).ToArray());
            }
            finally
            {
                JT809ArrayPool.Return(buffer);
            }
        }
コード例 #7
0
 public T Deserialize <T>(ReadOnlySpan <byte> bytes, int minBufferSize = 4096)
 {
     byte[] buffer = JT809ArrayPool.Rent(minBufferSize);
     try
     {
         JT809MessagePackReader jT809MessagePackReader = new JT809MessagePackReader(bytes);
         if (CheckPackageType(typeof(T)))
         {
             jT809MessagePackReader.Decode(buffer);
         }
         return(JT809MessagePackFormatterExtensions.GetFormatter <T>().Deserialize(ref jT809MessagePackReader, jT809Config));
     }
     finally
     {
         JT809ArrayPool.Return(buffer);
     }
 }
コード例 #8
0
 internal static ReadOnlySpan <byte> JT809DeEscape(ReadOnlySpan <byte> buffer)
 {
     byte[] tmpBuffer = JT809ArrayPool.Rent(buffer.Length - 1);
     try
     {
         int offset = 0;
         tmpBuffer[offset++] = buffer[0];
         for (int i = 1; i < buffer.Length - 1; i++)
         {
             byte first  = buffer[i];
             byte second = buffer[i + 1];
             if (first == 0x5a && second == 0x01)
             {
                 tmpBuffer[offset++] = 0x5b;
                 i++;
             }
             else if (first == 0x5a && second == 0x02)
             {
                 tmpBuffer[offset++] = 0x5a;
                 i++;
             }
             else if (first == 0x5e && second == 0x01)
             {
                 tmpBuffer[offset++] = 0x5d;
                 i++;
             }
             else if (first == 0x5e && second == 0x02)
             {
                 tmpBuffer[offset++] = 0x5e;
                 i++;
             }
             else
             {
                 tmpBuffer[offset++] = first;
             }
         }
         tmpBuffer[offset++] = buffer[buffer.Length - 1];
         return(tmpBuffer.AsSpan(0, offset).ToArray());
     }
     finally
     {
         JT809ArrayPool.Return(tmpBuffer);
     }
 }
コード例 #9
0
ファイル: JT809Serializer.cs プロジェクト: yezy-h/JT809
 public byte[] AnalyzeJsonBuffer(ReadOnlySpan <byte> bytes, JsonWriterOptions options = default, int minBufferSize = 8096)
 {
     byte[] buffer = JT809ArrayPool.Rent(minBufferSize);
     try
     {
         JT809MessagePackReader jT809MessagePackReader = new JT809MessagePackReader(bytes);
         jT809MessagePackReader.Decode(buffer);
         using (MemoryStream memoryStream = new MemoryStream())
             using (Utf8JsonWriter utf8JsonWriter = new Utf8JsonWriter(memoryStream, options))
             {
                 jT809Package.Analyze(ref jT809MessagePackReader, utf8JsonWriter, jT809Config);
                 utf8JsonWriter.Flush();
                 return(memoryStream.ToArray());
             }
     }
     finally
     {
         JT809ArrayPool.Return(buffer);
     }
 }