Esempio n. 1
0
        public void JsonSerializerContextCtor()
        {
            // Pass no options.
            MyJsonContext         context = new();
            JsonSerializerOptions options = context.Options; // New options instance created and binded at this point.

            Assert.NotNull(options);

            // Pass options.
            options = new JsonSerializerOptions();
            context = new MyJsonContext(options); // Provided options are binded at this point.
            Assert.Same(options, context.Options);
        }
Esempio n. 2
0
        public void OptionsImmutableAfterBinding()
        {
            // Bind via AddContext
            JsonSerializerOptions options = new();

            options.PropertyNameCaseInsensitive = true;
            options.AddContext <MyJsonContext>();
            CauseInvalidOperationException(() => options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase);

            // Bind via context ctor
            options = new JsonSerializerOptions();
            MyJsonContext context = new MyJsonContext(options);

            Assert.Same(options, context.Options);
            CauseInvalidOperationException(() => options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase);
        }