2

I am trying to create custom json converter ,which can parse date using specified formats

public class MultiFormatDateConverter: JsonConverter {
    public override bool CanWrite => true;

    public override bool CanConvert(Type objectType) {
        return objectType == typeof (DateTime);
    }
    private readonly string[] _formats;

    public MultiFormatDateConverter(params string[] formats) {
        _formats = formats;
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
        ....
    }
}

this is how I am trying to use it

public class MyClass{
[JsonConverter(typeof(MultiFormatDateConverter),"dd/MM/yyyy","dd/MM/yy")]
        public DateTime Date{get;set;}
    
}

Getting error

Newtonsoft.Json.JsonException : Error creating 'MultiFormatDateConverter'. ----> Newtonsoft.Json.JsonException : No matching parameterized constructor found for 'MultiFormatDateConverter'.

What am I missing?

burnsi
  • 6,194
  • 13
  • 17
  • 27
  • You need to do `[JsonConverter(typeof(MultiFormatDateConverter), new object [] { new string [] { "dd/MM/yyyy","dd/MM/yy" } } )]` as shown in [this answer](https://stackoverflow.com/a/43376889/3744182) to [Web API - JsonConverter - Custom Attribute](https://stackoverflow.com/q/43376404/3744182). In fact I think this is a duplicate, agree? – dbc Jun 23 '22 at 17:56
  • I edited the title of the duplicate to make it more easy found via search. – dbc Jun 23 '22 at 18:24

1 Answers1

2

It looks like JsonConverterAttribute doesn't support passing in constructor parameters as a params array. Some options would be to pass in a single string with a delimiter or have multiple constructors for different numbers of parameters. For example:

With a single string, use a character that is unlikely to be used in the format string. This can be fragile though:

public MultiFormatDateConverter(string formats)
{
    _formats = formats.Split('~'); // Unlikely ~ would be used in a format string?
}

Multiple constructors:

public MultiFormatDateConverter(string format1)
{
    _formats = new[] { format1 };
}

public MultiFormatDateConverter(string format1, string format2)
{
    _formats = new[] { format1, format2 };
}

public MultiFormatDateConverter(string format1, string format2, string format3)
{
    _formats = new[] { format1, format2, format3 };
}

// etc.
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • Thanks for string separator idea! Since number of formats might change in my scenario, I am going to pass formats as json string and de-serialize to array in constructor. One more thing - I was referring to this , [link](https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm) it uses Type[] as parameter. – swapnil jajoo Jun 23 '22 at 03:29
  • 1
    Notice how that example manually instantiates the converter though. Using an attribute means the framework creates it instead. – DavidG Jun 23 '22 at 06:49
  • *It looks like JsonConverterAttribute doesn't support passing in constructor parameters as a params array.* -- actually it does, but because `params` is just compile-time syntax sugar that doesn't affect invocation by reflection, you need to explicitly pass in the array of strings as a single argument, as shown in [this answer](https://stackoverflow.com/a/43376889/3744182) to [Web API - JsonConverter - Custom Attribute](https://stackoverflow.com/q/43376404/3744182). – dbc Jun 23 '22 at 17:57