コード例 #1
0
        /// <summary>
        /// Returns the example from the specified provider formatted as JSON.
        /// </summary>
        /// <param name="provider">The example provider to format the examples for.</param>
        /// <returns>
        /// An <see cref="object"/> representing the formatted example.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="provider"/> is <see langword="null"/>.
        /// </exception>
        private object FormatAsJson(IExampleProvider provider)
        {
            var examples = provider.GetExample();

            // Apply any formatting rules configured for the API (e.g. camel casing)
            var json = JsonConvert.SerializeObject(examples, _settings);

            return(JsonConvert.DeserializeObject(json));
        }
コード例 #2
0
        /// <summary>
        /// Returns the example from the specified provider formatted as JSON.
        /// </summary>
        /// <param name="provider">The example provider to format the examples for.</param>
        /// <returns>
        /// An <see cref="object"/> representing the formatted example.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="provider"/> is <see langword="null"/>.
        /// </exception>
        private IOpenApiAny FormatAsJson(IExampleProvider provider)
        {
            var examples = provider.GetExample();

            // Apply any formatting rules configured for the API (e.g. camel casing)
            var json = JsonSerializer.Serialize(examples, _settings);

            using var document = JsonDocument.Parse(json);

            var result = new OpenApiObject();

            // Recursively build up the example from the properties of the JObject
            foreach (var token in document.RootElement.EnumerateObject())
            {
                if (TryParse(token.Value, out var any))
                {
                    result[token.Name] = any;
                }
            }

            return(result);
        }