/// <summary> /// Converts an array of bytes into a URL-encoded array of bytes, starting at the specified position in the array and continuing for the specified number of bytes. /// </summary> /// <param name="bytes">The array of bytes to encode.</param> /// <param name="offset">The position in the byte array at which to begin encoding.</param> /// <param name="count">The number of bytes to encode.</param> /// <param name="preferUppercaseHexadecimalEncoding">When <c>true</c>, the <paramref name="bytes"/> is encoded using upper-case hexadecimal characters; otherwise lower-case hexadecimal characters.</param> /// <param name="encoding">The text encoding to use when writing the encoded bytes. The default is <see cref="UTF8Encoding"/>.</param> /// <returns>An encoded array of bytes.</returns> public static byte[] UrlEncodeToBytes(byte[] bytes, int offset, int count, bool preferUppercaseHexadecimalEncoding, Encoding encoding = null) { if (bytes == null) { throw new ArgumentNullException(nameof(bytes)); } int blen = bytes.Length; if (blen == 0) { return(new byte[0]); } if (offset < 0 || offset >= blen) { throw new ArgumentOutOfRangeException(nameof(offset)); } if (count < 0 || count > blen - offset) { throw new ArgumentOutOfRangeException(nameof(count)); } using (Stream result = StreamWriterUtility.CreateStream(UrlEncodeCharWriter, bytes, offset, count, preferUppercaseHexadecimalEncoding ? HexadecimalCharactersUpperCase : HexadecimalCharactersLowerCase, options => options.Encoding = encoding ?? new UTF8Encoding())) { return(result.ToByteArray()); } }
/// <summary> /// Serializes the specified <paramref name="source"/> to an object of <see cref="string"/>. /// </summary> /// <param name="source">The object to serialize to JSON format.</param> /// <param name="sourceType">The type of the object to serialize.</param> /// <returns>A string of the serialized <paramref name="source"/>.</returns> /// <remarks>This method will serialize, in the order specified, using one of either:<br/> /// 1. the explicitly defined <see cref="JsonFormatterOptions.WriterFormatter"/> delegate<br/> /// 2. the implicit or explicit defined delegate in <see cref="JsonFormatterOptions.WriterFormatters"/> dictionary<br/> /// 3. if neither was specified, a default JSON writer implementation will be used on <see cref="JsonConverter"/>. /// </remarks> public override Stream Serialize(object source, Type sourceType) { Validator.ThrowIfNull(source, nameof(source)); Validator.ThrowIfNull(sourceType, nameof(sourceType)); var converter = Options.Converter; if (converter == null) { var formatter = Options.ParseWriterFormatter(sourceType); converter = DynamicJsonConverter.Create(sourceType, Options.Settings, formatter); } var serializer = JsonSerializer.Create(Options.Settings); // there is a bug in the JsonConvert.SerializeObject, why we had to make our own implementation serializer.Converters.Add(converter); return(StreamWriterUtility.CreateStream(writer => { using (JsonTextWriter jsonWriter = new JsonTextWriter(writer)) { jsonWriter.CloseOutput = false; jsonWriter.Formatting = serializer.Formatting; serializer.Serialize(jsonWriter, source, sourceType); } })); }
/// <summary> /// Serializes the specified <paramref name="source"/> to an object of <see cref="string"/>. /// </summary> /// <param name="source">The object to serialize to JSON format.</param> /// <param name="objectType">The type of the object to serialize.</param> /// <returns>A string of the serialized <paramref name="source"/>.</returns> public override Stream Serialize(object source, Type objectType) { Validator.ThrowIfNull(source, nameof(source)); Validator.ThrowIfNull(objectType, nameof(objectType)); var serializer = Options.SynchronizeWithJsonConvert ? JsonSerializer.CreateDefault() : JsonSerializer.Create(Options.Settings); // there is a bug in the JsonConvert.SerializeObject, why we had to make our own implementation return(StreamWriterUtility.CreateStream(writer => { using (var jsonWriter = new JsonTextWriter(writer)) { jsonWriter.CloseOutput = false; jsonWriter.Formatting = serializer.Formatting; serializer.Serialize(jsonWriter, source, objectType); } })); }