16

I use Newtonsoft.Json library

Is there a way to trim spaces from any string data during deserialization?

class Program
{
    class Person
    {
        [JsonProperty("name")]
        public string Name;
    }
    static void Main(string[] args)
    {
        var p = JsonConvert.DeserializeObject<Person>(@"{ name: "" John "" }");
        Console.WriteLine("Name is: \"{0}\"", p.Name);
    }
}

Added:

Finally, I've got solution with custom converter. Not nice, but better then property with Trim().

If anyone has any ideas how to do it in more natural way, please welcome.

class Program
{
    sealed class TrimAttribute : Attribute
    { }

    class TrimConverter<T> : JsonConverter where T : new()
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jObject = JObject.Load(reader);
            var obj = new T();
            serializer.Populate(jObject.CreateReader(), obj);

            var props = objectType.GetFields(BindingFlags.Instance | BindingFlags.Public)
                .Where(p => p.FieldType == typeof(string))
                .Where(p => Attribute.GetCustomAttributes(p).Any(u => (Type) u.TypeId == typeof(TrimAttribute)))
                ;

            foreach (var fieldInfo in props)
            {
                var val = (string) fieldInfo.GetValue(obj);
                fieldInfo.SetValue(obj, val.Trim());
            }

            return obj;
        }

        public override bool CanConvert(Type objectType)
        {
            return objectType.IsAssignableFrom(typeof (T));
        }
    }

    [JsonConverter(typeof(TrimConverter<Person>))]
    class Person
    {
        [JsonProperty("name")]
        [Trim]
        public string Name;

        [JsonProperty("surname")]
        public string Surname;
    }
    static void Main(string[] args)
    {
        var p = JsonConvert.DeserializeObject<Person>(@"{ name: "" John "", surname: "" Smith "" }");
        Console.WriteLine("Name is: \"{0}\", \"{1}\"", p.Name, p.Surname);
    }
}
Sam Fisher
  • 171
  • 1
  • 2
  • 5

3 Answers3

26

You could write your own JsonConverter:

public class TrimmingConverter : JsonConverter
{
    public override bool CanRead => true;
    public override bool CanWrite => false;

    public override bool CanConvert(Type objectType) => objectType == typeof(string);

    public override object ReadJson(JsonReader reader, Type objectType,
                                    object existingValue, JsonSerializer serializer)
    {
        return ((string)reader.Value)?.Trim();
    }

    public override void WriteJson(JsonWriter writer, object value, 
                                   JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

You can use it like this to apply to all string fields:

var json = @"{ name:"" John "" }"
var p = JsonConvert.DeserializeObject<Person>(json, new TrimmingConverter());
Console.WriteLine("Name is: \"{0}\"", p.Name);
//Name is: "John"

Or you can apply this to certain fields only:

public class Person
{
    [JsonProperty("name")]
    [JsonConverter(typeof(TrimmingConverter))] // <-- that's the important line
    public string Name { get; set; }
    [JsonProperty("other")]
    public string Other { get; set; }
}

var json = @"{ name:"" John "", other:"" blah blah blah "" }"
var p = JsonConvert.DeserializeObject<Person>(json);
Console.WriteLine("Name is: \"{0}\"", p.Name);
Console.WriteLine("Other is: \"{0}\"", p.Other);

//Name is: "John"
//Other is: " blah blah blah "
Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • thanx, look interesting. I wonder, how come this converter skips any other fields? – Sam Fisher Oct 09 '13 at 13:20
  • @SamFisher It's based on the `CanConvert`: currently it will do this on all `string` fields. You can modify `CanConvert` and `ReadJson` (`reader` has lots of details about the field you're deserializing) to be more specific if you want to. E.g. in your example, Json.net asks if it `CanConvert` both `Person` and `string` - based on the response, it only uses this converter on the `string` field. – Tim S. Oct 09 '13 at 14:24
  • @SamFisher I've added an example of how to only apply it to certain properties. – Tim S. Oct 09 '13 at 14:43
10

In .NET Core 3.1 you can use System.Text.Json to achieve this.

/// <summary>
/// Trim leading and trailing whitespace.
/// </summary>
public class TrimmingConverter : JsonConverter<string>
{
    /// <summary>
    /// Trim the input string
    /// </summary>
    /// <param name="reader">reader</param>
    /// <param name="typeToConvert">Object type</param>
    /// <param name="options">Existing Value</param>
    /// <returns></returns>
    public override string Read(
        ref Utf8JsonReader reader,
        Type typeToConvert,
        JsonSerializerOptions options) => reader.GetString()?.Trim();

    /// <summary>
    /// Trim the output string
    /// </summary>
    /// <param name="writer">Writer</param>
    /// <param name="value">value</param>
    /// <param name="options">serializer</param>
    public override void Write(
        Utf8JsonWriter writer,
        string value,
        JsonSerializerOptions options) => writer.WriteStringValue(value?.Trim());
}
janw
  • 8,758
  • 11
  • 40
  • 62
Ravi Kumar Mistry
  • 1,063
  • 1
  • 13
  • 24
0

The solution provide above didnt work for me. I modified Sam Fisher solution and combined it with Timmerz solution. how to get both fields and properties in single call via reflection?

public sealed class TrimAttribute : Attribute {
}

public static class TrimConverterExtension {
  public static void SetValue(this MemberInfo member, object property, object value) {
     switch (member.MemberType) {
        case MemberTypes.Property:
           ((PropertyInfo)member).SetValue(property, value, null);
           break;
        case MemberTypes.Field:
           ((FieldInfo)member).SetValue(property, value);
           break;
        default:
           throw new Exception("Property must be of type FieldInfo or PropertyInfo");
     }
  }

  public static object GetValue(this MemberInfo member, object property) {
     switch (member.MemberType) {
        case MemberTypes.Property:
           return ((PropertyInfo)member).GetValue(property, null);
        case MemberTypes.Field:
           return ((FieldInfo)member).GetValue(property);
        default:
           throw new Exception("Property must be of type FieldInfo or PropertyInfo");
     }
  }

  public static Type GetMemberType(this MemberInfo member) {
     switch (member.MemberType) {
        case MemberTypes.Field:
           return ((FieldInfo)member).FieldType;
        case MemberTypes.Property:
           return ((PropertyInfo)member).PropertyType;
        case MemberTypes.Event:
           return ((EventInfo)member).EventHandlerType;
        default:
           throw new ArgumentException("MemberInfo must be if type FieldInfo, PropertyInfo or EventInfo", "member");
     }
  }
}

public class TrimConverter<T> : JsonConverter where T : new() {
  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
  }

  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
     var jObject = JObject.Load(reader);
     var obj = new T();
     serializer.Populate(jObject.CreateReader(), obj);

     //Looks for the trim attribute on the property
     const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;
     IEnumerable<MemberInfo> members = objectType.GetFields(bindingFlags).Cast<MemberInfo>()
         .Concat(objectType.GetProperties(bindingFlags))
         .Where(p => p.GetMemberType() == typeof(string))
         .Where(p => Attribute.GetCustomAttributes(p).Any(u => (Type)u.TypeId == typeof(TrimAttribute)))

         .ToArray();

     foreach (var fieldInfo in members) {
        var val = (string)fieldInfo.GetValue(obj);
        if (!string.IsNullOrEmpty(val)) {
           fieldInfo.SetValue(obj, val.Trim());
        }
     }

     return obj;
  }

  public override bool CanConvert(Type objectType) {
     return objectType.IsAssignableFrom(typeof(T));
  }
}
Community
  • 1
  • 1
Lyon
  • 711
  • 8
  • 11