THRIFT-5210 further refactoring and performance optimizations
Client: netstd
Patch: Jens Geyer

This closes #2146
diff --git a/lib/netstd/Thrift/Protocol/TJSONProtocol.cs b/lib/netstd/Thrift/Protocol/TJSONProtocol.cs
index 3b2ed06..2f1ccdb 100644
--- a/lib/netstd/Thrift/Protocol/TJSONProtocol.cs
+++ b/lib/netstd/Thrift/Protocol/TJSONProtocol.cs
@@ -127,7 +127,7 @@
                     }
                     else
                     {
-                        await Trans.WriteAsync(bytes.ToArray(), i, 1, cancellationToken);
+                        await Trans.WriteAsync(bytes, i, 1, cancellationToken);
                     }
                 }
                 else
@@ -315,12 +315,10 @@
             await WriteJsonObjectEndAsync(cancellationToken);
         }
 
-        public override async Task WriteFieldStopAsync(CancellationToken cancellationToken)
+        public override Task WriteFieldStopAsync(CancellationToken cancellationToken)
         {
-            if (cancellationToken.IsCancellationRequested)
-            {
-                await Task.FromCanceled(cancellationToken);
-            }
+            cancellationToken.ThrowIfCancellationRequested();
+            return Task.CompletedTask;
         }
 
         public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken)
@@ -679,7 +677,8 @@
         public override async ValueTask<TStruct> ReadStructBeginAsync(CancellationToken cancellationToken)
         {
             await ReadJsonObjectStartAsync(cancellationToken);
-            return new TStruct();
+
+            return AnonymousStruct;
         }
 
         public override async Task ReadStructEndAsync(CancellationToken cancellationToken)
@@ -689,18 +688,19 @@
 
         public override async ValueTask<TField> ReadFieldBeginAsync(CancellationToken cancellationToken)
         {
-            var field = new TField();
             var ch = await Reader.PeekAsync(cancellationToken);
             if (ch == TJSONProtocolConstants.RightBrace[0])
             {
-                field.Type = TType.Stop;
+                return StopField;
             }
-            else
+
+            var field = new TField()
             {
-                field.ID = (short) await ReadJsonIntegerAsync(cancellationToken);
-                await ReadJsonObjectStartAsync(cancellationToken);
-                field.Type = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken));
-            }
+                ID = (short)await ReadJsonIntegerAsync(cancellationToken)
+            };
+
+            await ReadJsonObjectStartAsync(cancellationToken);
+            field.Type = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken));
             return field;
         }
 
@@ -845,20 +845,16 @@
                 Proto = proto;
             }
 
-            public virtual async Task WriteConditionalDelimiterAsync(CancellationToken cancellationToken)
+            public virtual Task WriteConditionalDelimiterAsync(CancellationToken cancellationToken)
             {
-                if (cancellationToken.IsCancellationRequested)
-                {
-                    await Task.FromCanceled(cancellationToken);
-                }
+                cancellationToken.ThrowIfCancellationRequested();
+                return Task.CompletedTask;
             }
 
-            public virtual async Task ReadConditionalDelimiterAsync(CancellationToken cancellationToken)
+            public virtual Task ReadConditionalDelimiterAsync(CancellationToken cancellationToken)
             {
-                if (cancellationToken.IsCancellationRequested)
-                {
-                    await Task.FromCanceled(cancellationToken);
-                }
+                cancellationToken.ThrowIfCancellationRequested();
+                return Task.CompletedTask;
             }
 
             public virtual bool EscapeNumbers()
@@ -978,10 +974,7 @@
             /// </summary>
             public async ValueTask<byte> ReadAsync(CancellationToken cancellationToken)
             {
-                if (cancellationToken.IsCancellationRequested)
-                {
-                    return await Task.FromCanceled<byte>(cancellationToken);
-                }
+                cancellationToken.ThrowIfCancellationRequested();
 
                 if (_hasData)
                 {
@@ -1001,10 +994,7 @@
             /// </summary>
             public async ValueTask<byte> PeekAsync(CancellationToken cancellationToken)
             {
-                if (cancellationToken.IsCancellationRequested)
-                {
-                    return await Task.FromCanceled<byte>(cancellationToken);
-                }
+                cancellationToken.ThrowIfCancellationRequested();
 
                 if (!_hasData)
                 {