@@ -76,14 +76,26 @@ private void WriteJson(Stream stream)
7676 {
7777 if ( ! Changes . HasChanges )
7878 {
79- Write ( stream , _original . Span ) ;
79+ WriteOriginal ( stream ) ;
8080 return ;
8181 }
8282
8383 using Utf8JsonWriter writer = new ( stream ) ;
8484 RootElement . WriteTo ( writer ) ;
8585 }
8686
87+ private void WriteOriginal ( Stream stream )
88+ {
89+ if ( _original . Length == 0 )
90+ {
91+ using Utf8JsonWriter writer = new ( stream ) ;
92+ _originalDocument . WriteTo ( writer ) ;
93+ return ;
94+ }
95+
96+ Write ( stream , _original . Span ) ;
97+ }
98+
8799 private void WritePatch ( Stream stream )
88100 {
89101 if ( ! Changes . HasChanges )
@@ -142,6 +154,18 @@ public static MutableJsonDocument Parse(ReadOnlyMemory<byte> utf8Json, JsonSeria
142154 return new MutableJsonDocument ( doc , utf8Json , serializerOptions ) ;
143155 }
144156
157+ /// <summary>
158+ /// Parses JSON into a <see cref="MutableJsonDocument"/>.
159+ /// </summary>
160+ /// <param name="reader">Reader holding the JSON value.</param>
161+ /// <param name="serializerOptions">Serializer options used to serialize and deserialize any changes to the JSON.</param>
162+ /// <returns>A <see cref="MutableJsonDocument"/> representation of the value.</returns>
163+ public static MutableJsonDocument Parse ( ref Utf8JsonReader reader , JsonSerializerOptions ? serializerOptions = default )
164+ {
165+ JsonDocument doc = JsonDocument . ParseValue ( ref reader ) ;
166+ return new MutableJsonDocument ( doc , default , serializerOptions ) ;
167+ }
168+
145169 /// <summary>
146170 /// Parses a UTF-8 encoded string representing a single JSON value into a <see cref="MutableJsonDocument"/>.
147171 /// </summary>
@@ -175,34 +199,18 @@ public void Dispose()
175199 _originalDocument . Dispose ( ) ;
176200 }
177201
178- internal MutableJsonDocument ( JsonDocument document , JsonSerializerOptions ? serializerOptions ) : this ( document , GetBytesFromDocument ( document ) , serializerOptions )
179- {
180- }
181-
182- internal MutableJsonDocument ( JsonDocument document , ReadOnlyMemory < byte > utf8Json , JsonSerializerOptions ? serializerOptions )
202+ private MutableJsonDocument ( JsonDocument document , ReadOnlyMemory < byte > utf8Json , JsonSerializerOptions ? serializerOptions )
183203 {
184- _original = utf8Json ;
185204 _originalDocument = document ;
205+ _original = utf8Json ;
186206 _serializerOptions = serializerOptions ?? new JsonSerializerOptions ( ) ;
187207 }
188208
189- private static ReadOnlyMemory < byte > GetBytesFromDocument ( JsonDocument document )
190- {
191- using MemoryStream stream = new ( ) ;
192- using ( Utf8JsonWriter writer = new ( stream ) )
193- {
194- document . WriteTo ( writer ) ;
195- }
196-
197- return new ReadOnlyMemory < byte > ( stream . GetBuffer ( ) , 0 , ( int ) stream . Position ) ;
198- }
199-
200209 private class MutableJsonDocumentConverter : JsonConverter < MutableJsonDocument >
201210 {
202211 public override MutableJsonDocument Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
203212 {
204- JsonDocument document = JsonDocument . ParseValue ( ref reader ) ;
205- return new MutableJsonDocument ( document , options ) ;
213+ return Parse ( ref reader ) ;
206214 }
207215
208216 public override void Write ( Utf8JsonWriter writer , MutableJsonDocument value , JsonSerializerOptions options )
0 commit comments