コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <param name="serializationOptions">The serialization options. When the value is null the
        /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
        /// <returns></returns>

        public override byte[] Serialize <T>(T item, ApiClientSerializationOptions serializationOptions)
        {
            // When no options passed use the default.
            if (serializationOptions == null)
            {
                serializationOptions = DefaultOptions;
            }

            var jsonSettings = new JsonSerializerSettings
            {
                NullValueHandling = serializationOptions.IgnoreNullValues ? NullValueHandling.Ignore : NullValueHandling.Include
            };

            if (serializationOptions.UseStringEnumConversion)
            {
                var stringEnumConverter = new StringEnumConverter();
                jsonSettings.Converters.Add(stringEnumConverter);
            }

            if (serializationOptions.UseCamelCasePropertyNames)
            {
                jsonSettings.ContractResolver = new CamelCasePropertyNamesExceptDictionaryContractResolver();
            }

            string json = JsonConvert.SerializeObject(item, jsonSettings);

            return(Encoding.UTF8.GetBytes(json));
        }
コード例 #2
0
 /// <summary>
 /// Serializes the specified object to a sequence of bytes,
 /// following the provided rules for camel case property name and null value handling.
 /// </summary>
 /// <typeparam name="T">The type of the object to serialize.</typeparam>
 /// <param name="item">The object to serialize.</param>
 /// <param name="serializationOptions">The serialization options. When the value is null the
 /// the serialization options should be provided by the serializer, otherwise the given options should be used.</param>
 /// <returns></returns>
 public abstract byte[] Serialize <T>(T item, ApiClientSerializationOptions serializationOptions);