Skip to content

Is there a way to combine custom converters from different projects? #81894

Answered by eiriktsarpalis
edsonaf asked this question in General
Discussion options

You must be logged in to vote

The JsonSerializerOptions.Converters property is an IList, so there's no AddRange method you can use out of the box. I would recommend writing your own extension method:

public static ListExtensions
{
    public static void AddRange(this IList<T> list, IEnumerable<T> range)
    {
         foreach (var T item in range)
             list.Add(item);
    }
}

And then use that to combine converters from different JsonSerializerOptions:

var options = new JsonSerializerOptions();
options.Converters.AddRange(options2.Converters);
options.Converters.AddRange(options3.Converters);

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by edsonaf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
Converted from issue

This discussion was converted from issue #81883 on February 09, 2023 15:36.