Skip to content

Commit cd48b64

Browse files
committed
Update VerifyJsonWriter.cs
1 parent c77cddd commit cd48b64

File tree

1 file changed

+94
-6
lines changed

1 file changed

+94
-6
lines changed

src/Verify/Serialization/VerifyJsonWriter.cs

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public override void WriteValue(CharSpan value)
138138
{
139139
WriteRawValue(value);
140140
}
141+
141142
base.Flush();
142143
builder.Remove(builderLength, 1);
143144
return;
@@ -266,6 +267,93 @@ public void WriteMember(object target, object? value, string name)
266267
WriteOrSerialize(value);
267268
}
268269

270+
/// <summary>
271+
/// Writes a property name and value while respecting other custom serialization settings.
272+
/// </summary>
273+
public void WriteMember(object target, string? value, string name)
274+
{
275+
if (value is null)
276+
{
277+
WriteNullMember(name);
278+
return;
279+
}
280+
281+
var declaringType = target.GetType();
282+
if (serialization.TryGetScrubOrIgnore(declaringType, typeof(string), name, out var scrubOrIgnore))
283+
{
284+
if (scrubOrIgnore != ScrubOrIgnore.Ignore)
285+
{
286+
WriteRawOrStrictMember(name, "Scrubbed");
287+
}
288+
289+
return;
290+
}
291+
292+
if (serialization.TryGetScrubOrIgnoreByInstance(value, out scrubOrIgnore))
293+
{
294+
if (scrubOrIgnore != ScrubOrIgnore.Ignore)
295+
{
296+
WriteRawOrStrictMember(name, "Scrubbed");
297+
}
298+
299+
return;
300+
}
301+
302+
var converter = VerifierSettings.GetMemberConverter(declaringType, name);
303+
if (converter is not null)
304+
{
305+
value = (string?) converter(target, value);
306+
if (value is null)
307+
{
308+
return;
309+
}
310+
}
311+
312+
WritePropertyName(name);
313+
WriteValue(value);
314+
}
315+
/// <summary>
316+
/// Writes a property name and value while respecting other custom serialization settings.
317+
/// </summary>
318+
public void WriteMember(object target, CharSpan value, string name)
319+
{
320+
var declaringType = target.GetType();
321+
if (serialization.TryGetScrubOrIgnore(declaringType, typeof(CharSpan), name, out var scrubOrIgnore))
322+
{
323+
if (scrubOrIgnore != ScrubOrIgnore.Ignore)
324+
{
325+
WriteRawOrStrictMember(name, "Scrubbed");
326+
}
327+
328+
return;
329+
}
330+
331+
//TODO: support instance scrubbing for CharSpan?
332+
// if (serialization.TryGetScrubOrIgnoreByInstance(value, out scrubOrIgnore))
333+
// {
334+
// if (scrubOrIgnore != ScrubOrIgnore.Ignore)
335+
// {
336+
// WriteRawOrStrictMember(name, "Scrubbed");
337+
// }
338+
//
339+
// return;
340+
// }
341+
342+
//TODO: support converters for CharSpan?
343+
// var converter = VerifierSettings.GetMemberConverter(declaringType, name);
344+
// if (converter is not null)
345+
// {
346+
// value = (string?) converter(target, value);
347+
// if (value is null)
348+
// {
349+
// return;
350+
// }
351+
// }
352+
353+
WritePropertyName(name);
354+
WriteValue(value);
355+
}
356+
269357
void WriteRawOrStrictMember(string name, string value)
270358
{
271359
WritePropertyName(name);
@@ -288,21 +376,21 @@ void WriteNullMember(string name)
288376
}
289377
}
290378

291-
void WriteOrSerialize(object converted)
379+
void WriteOrSerialize(object value)
292380
{
293-
if (converted is string convertedString)
381+
if (value is string stringValue)
294382
{
295-
WriteValue(convertedString);
383+
WriteValue(stringValue);
296384
return;
297385
}
298386

299-
if (converted.GetType().IsPrimitive)
387+
if (value.GetType().IsPrimitive)
300388
{
301-
WriteValue(converted);
389+
WriteValue(value);
302390
return;
303391
}
304392

305-
settings.Serializer.Serialize(this, converted);
393+
settings.Serializer.Serialize(this, value);
306394
}
307395

308396
/// <summary>

0 commit comments

Comments
 (0)