Skip to content

Commit

Permalink
Trim incoming string values
Browse files Browse the repository at this point in the history
  • Loading branch information
VILLAN3LL3 committed Oct 24, 2022
1 parent 0100468 commit 06cf5f7
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 115 deletions.
21 changes: 21 additions & 0 deletions Orso.Arpa.Api/ModelBinding/TrimmedStringConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Orso.Arpa.Api.ModelBinding
{
public class TrimmedStringConverter : JsonConverter<string>
{
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(string);

public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.GetString() is string value ? value.Trim() : null;
}

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
writer.WriteStringValue(value);
}
}
}
Loading

0 comments on commit 06cf5f7

Please sign in to comment.