Beispiel #1
0
 /// <summary>
 /// Gets the JSON serializer settings and sets the naming strategy supplied.
 /// </summary>
 /// <param name="strategy">The naming strategy to be used during serialization.</param>
 /// <param name="nullValueHandling">Tells whether to include or ignore null values when (de)serializing.</param>
 /// <returns>The serializer settings of Newtonsoft.Json.</returns>
 private static JsonSerializerSettings GetJsonSerializerSettings(NamingStrategyEnum strategy, NullValueHandling nullValueHandling)
 {
     return(new JsonSerializerSettings()
     {
         NullValueHandling = nullValueHandling,
         DateFormatHandling = DateFormatHandling.IsoDateFormat,
         // TODO: test DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind,
         ContractResolver = new DefaultContractResolver()
         {
             NamingStrategy = GetNamingStrategy(strategy)
         }
     });
 }
Beispiel #2
0
        /// <summary>
        /// Gets the naming strategy of Newtonsoft.Json from our <see cref="NamingStrategyEnum"/>.
        /// </summary>
        /// <param name="strategy">Enumeration value of naming strategy.</param>
        /// <returns>The naming strategy to be used by Newtonsoft serializer.</returns>
        private static NamingStrategy GetNamingStrategy(NamingStrategyEnum strategy)
        {
            if (strategy == NamingStrategyEnum.CamelCase)
            {
                return(new CamelCaseNamingStrategy(processDictionaryKeys: true, overrideSpecifiedNames: true));
            }

            if (strategy == NamingStrategyEnum.SnakeCase)
            {
                return(new SnakeCaseNamingStrategy(processDictionaryKeys: true, overrideSpecifiedNames: true));
            }

            return(new DefaultNamingStrategy()
            {
                ProcessDictionaryKeys = true,
                OverrideSpecifiedNames = true
            });
        }
Beispiel #3
0
        /// <summary>
        /// Tries to deserializes a response body based on response <c>Content-Type</c> header.
        /// </summary>
        /// <typeparam name="TResponse">Type to deserialize the response body.</typeparam>
        /// <param name="headers">Headers with information </param>
        /// <param name="responseBody">The string of response body to be deserialized.</param>
        /// <param name="namingStrategy">The stategy to use when serializing property names.
        /// Default is <see cref="NamingStrategyEnum.CamelCase"/>.
        /// </param>
        /// <param name="nullValueHandling">Tells whether to include or ignore null values when (de)serializing.</param>
        /// <returns>The deserialized object or default value of type.</returns>
        /// <remarks>
        /// Currently supports JSON and XML. If Content-Type is not supported or if deserialization
        /// fails the default value of type is returned.
        /// </remarks>
        public static TResponse TryDeserializeResponseBody <TResponse>(
            NameValueCollection headers,
            string responseBody,
            NamingStrategyEnum namingStrategy   = NamingStrategyEnum.CamelCase,
            NullValueHandling nullValueHandling = NullValueHandling.Include)
        {
            if (headers != null && string.IsNullOrEmpty(responseBody) == false)
            {
                string contentType = headers.Get("Content-Type");

                if (contentType?.Contains("application/json") == true)
                {
                    return(TryDeserializeJson <TResponse>(responseBody, GetJsonSerializerSettings(namingStrategy, nullValueHandling)));
                }

                if (contentType?.Contains("application/xml") == true)
                {
                    return(TryDeserializeXml <TResponse>(responseBody, Encoding.UTF8));
                }
            }

            return(default(TResponse));
        }