I use Newtonsoft.Json library
Is there a way to trim spaces from any string data during deserialization?
class Program
{
class Person
{
In .net core 3.1 you can use the System.Text.Json
to achieve this.
///
/// Trim spaces
///
public class TrimmingConverter : JsonConverter
{
///
/// Trim the input string
///
/// reader
/// Object type
/// Existing Value
///
public override string Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options) => reader.GetString()?.Trim();
///
/// Trim the output string
///
/// Writer
/// value
/// serializer
public override void Write(
Utf8JsonWriter writer,
string dateTimeValue,
JsonSerializerOptions options) => writer.WriteStringValue(dateTimeValue?.Trim());
}