Exemple #1
0
    public override void WriteJson(JsonWriter writer, object?value, JsonSerializer serializer)
    {
        if (value == null)
        {
            return;
        }

        var valueAsString = (string)value;

        if (!string.IsNullOrWhiteSpace(valueAsString))
        {
            if (guidScrubber != null)
            {
                if (Guid.TryParse(valueAsString, out var guid))
                {
                    guidScrubber.WriteValue(writer, guid);
                    return;
                }
            }

            if (dateTimeOffsetScrubber != null)
            {
                foreach (var format in datetimeOffsetFormats)
                {
                    if (DateTimeOffset.TryParseExact(valueAsString, format, null, DateTimeStyles.None, out var dateTimeOffset))
                    {
                        dateTimeOffsetScrubber.WriteValue(writer, dateTimeOffset);
                        return;
                    }
                }
            }

            if (dateTimeScrubber != null)
            {
                foreach (var format in datetimeFormats)
                {
                    if (DateTime.TryParseExact(valueAsString, format, null, DateTimeStyles.None, out var dateTime))
                    {
                        dateTimeScrubber.WriteValue(writer, dateTime);
                        return;
                    }
                }
            }
        }

        writer.WriteValue(valueAsString);
    }