@@ -22,6 +22,8 @@ namespace Microsoft.Azure.Cosmos.Json
2222#endif
2323 abstract partial class JsonWriter : IJsonWriter
2424 {
25+ internal bool EnableEncodedStrings { get ; private set ; }
26+
2527 /// <summary>
2628 /// Executes the provided lambda and captures a copy of the written bytes for reuse.
2729 /// The lambda is executed at a field name, and should leave the reader in a state where
@@ -31,7 +33,7 @@ abstract partial class JsonWriter : IJsonWriter
3133 /// <returns>Blitted bytes.</returns>
3234 public static PreblittedBinaryJsonScope CapturePreblittedBinaryJsonScope ( Action < ITypedBinaryJsonWriter > scopeWriter )
3335 {
34- JsonBinaryWriter jsonBinaryWriter = new JsonBinaryWriter ( ) ;
36+ JsonBinaryWriter jsonBinaryWriter = new JsonBinaryWriter ( initialCapacity : 256 , serializeCount : false , enableEncodedStrings : false ) ;
3537 Contract . Requires ( ! jsonBinaryWriter . JsonObjectState . InArrayContext ) ;
3638 Contract . Requires ( ! jsonBinaryWriter . JsonObjectState . InObjectContext ) ;
3739 Contract . Requires ( ! jsonBinaryWriter . JsonObjectState . IsPropertyExpected ) ;
@@ -270,10 +272,13 @@ private enum RawValueType : byte
270272 /// </summary>
271273 /// <param name="initialCapacity">The initial capacity to avoid intermediary allocations.</param>
272274 /// <param name="serializeCount">Whether to serialize the count for object and array typemarkers.</param>
275+ /// <param name="enableEncodedStrings">enable reference string encoding</param>
273276 public JsonBinaryWriter (
274- int initialCapacity = 256 ,
275- bool serializeCount = false )
277+ int initialCapacity ,
278+ bool serializeCount ,
279+ bool enableEncodedStrings )
276280 {
281+ this . EnableEncodedStrings = enableEncodedStrings ;
277282 this . binaryWriter = new JsonBinaryMemoryWriter ( initialCapacity ) ;
278283 this . bufferedContexts = new Stack < ArrayAndObjectInfo > ( ) ;
279284 this . serializeCount = serializeCount ;
@@ -827,20 +832,23 @@ private void WriteFieldNameOrString(bool isFieldName, Utf8Span utf8Span)
827832 throw new InvalidOperationException ( $ "Unable to serialize a { nameof ( JsonBinaryEncoding . MultiByteTypeMarker ) } of length: { multiByteTypeMarker . Length } ") ;
828833 }
829834 }
830- else if ( isFieldName
835+ else if ( this . EnableEncodedStrings
836+ && isFieldName
831837 && ( utf8Span . Length >= MinReferenceStringLength )
832838 && this . TryRegisterStringValue ( utf8Span ) )
833839 {
834840 // Work is done in the check
835841 }
836- else if ( ! isFieldName
842+ else if ( this . EnableEncodedStrings
843+ && ! isFieldName
837844 && ( utf8Span . Length == JsonBinaryEncoding . GuidLength )
838845 && JsonBinaryEncoding . TryEncodeGuidString ( utf8Span . Span , this . binaryWriter . Cursor ) )
839846 {
840847 // Encoded value as guid string
841848 this . binaryWriter . Position += JsonBinaryEncoding . EncodedGuidLength ;
842849 }
843- else if ( ! isFieldName
850+ else if ( this . EnableEncodedStrings
851+ && ! isFieldName
844852 && ( utf8Span . Length == JsonBinaryEncoding . GuidWithQuotesLength )
845853 && ( utf8Span . Span [ 0 ] == '"' )
846854 && ( utf8Span . Span [ JsonBinaryEncoding . GuidWithQuotesLength - 1 ] == '"' )
@@ -851,13 +859,15 @@ private void WriteFieldNameOrString(bool isFieldName, Utf8Span utf8Span)
851859 this . binaryWriter . Cursor [ 0 ] = JsonBinaryEncoding . TypeMarker . DoubleQuotedLowercaseGuidString ;
852860 this . binaryWriter . Position += JsonBinaryEncoding . EncodedGuidLength ;
853861 }
854- else if ( ! isFieldName
862+ else if ( this . EnableEncodedStrings
863+ && ! isFieldName
855864 && JsonBinaryEncoding . TryEncodeCompressedString ( utf8Span . Span , this . binaryWriter . Cursor , out int bytesWritten ) )
856865 {
857866 // Encoded value as a compressed string
858867 this . binaryWriter . Position += bytesWritten ;
859868 }
860- else if ( ! isFieldName
869+ else if ( this . EnableEncodedStrings
870+ && ! isFieldName
861871 && ( utf8Span . Length >= MinReferenceStringLength )
862872 && ( utf8Span . Length <= MaxReferenceStringLength )
863873 && this . TryRegisterStringValue ( utf8Span ) )
0 commit comments