﻿namespace Dike.Core.Json
{
    using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;

    /// <summary>
    /// Extension methods for <see cref="object"/> class.
    /// </summary>
    public static class JsonExtensions
    {
        #region Methods

        /// <summary>
        /// Converts the object to a JSON string.
        /// </summary>
        /// <param name="source">The object to convert.</param>
        /// <param name="camelCase">Set to <c>true</c> to use camel case in the JSON.</param>
        /// <param name="indented">Set to <c>true</c> to indent the JSON.</param>
        /// <returns>The JSON string.</returns>
        public static string ToJsonString(this object source, bool camelCase = false, bool indented = false)
        {
            JsonSerializerSettings options = new JsonSerializerSettings();

            if (camelCase)
            {
                options.ContractResolver = new CamelCasePropertyNamesContractResolver();
            }

            if (indented)
            {
                options.Formatting = Formatting.Indented;
            }

            options.Converters.Insert(0, new DateTimeConverter());

            return JsonConvert.SerializeObject(source, options);
        }

        #endregion Methods
    }
}