Skip to content

Commit

Permalink
Add ToEncodedUtf8
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Jan 4, 2025
1 parent 52f35b8 commit 33371e0
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/FsCodec.SystemTextJson/Encoding.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,25 @@ module private Impl =
use output = new System.IO.MemoryStream()
compressedBytes |> alg output
output.ToArray() |> post
let decode_ direct expand struct (encoding, data: JsonElement) =
let decode_ direct expand (struct (encoding, data: JsonElement) as x) =
match encoding, data.ValueKind with
| Encoding.Deflate, JsonValueKind.String -> data.GetBytesFromBase64() |> expand inflateTo
| Encoding.Brotli, JsonValueKind.String -> data.GetBytesFromBase64() |> expand brotliDecompressTo
| _ -> data |> direct
let decode = decode_ id (unpack InteropHelpers.Utf8ToJsonElement)
| Encoding.Brotli, JsonValueKind.String -> data.GetBytesFromBase64() |> expand brotliDecompressTo
| _ -> direct data
let decode = decode_ unbox (unpack InteropHelpers.Utf8ToJsonElement)
let private blobToBase64StringJsonElement = Convert.ToBase64String >> JsonSerializer.SerializeToElement
let direct (raw: JsonElement): EncodedBody = Encoding.Direct, raw
let recode struct (encoding, data: ReadOnlyMemory<byte>): EncodedBody =
let ofEncodedBody struct (encoding, data: ReadOnlyMemory<byte>): EncodedBody =
match encoding with
| Encoding.Deflate -> Encoding.Deflate, data.ToArray() |> blobToBase64StringJsonElement
| Encoding.Brotli -> Encoding.Brotli, data.ToArray() |> blobToBase64StringJsonElement
| _ -> Encoding.Direct, data.ToArray() |> blobToBase64StringJsonElement
| Encoding.Brotli -> Encoding.Brotli, data.ToArray() |> blobToBase64StringJsonElement
| _ -> Encoding.Direct, data |> InteropHelpers.Utf8ToJsonElement
let decodeUtf8 = decode_ InteropHelpers.JsonElementToUtf8 (unpack ReadOnlyMemory<byte>)
let toEncodedBody struct (encoding, data: JsonElement): FsCodec.EncodedBody =
match encoding, data.ValueKind with
| Encoding.Deflate, JsonValueKind.String -> Encoding.Deflate, data.GetBytesFromBase64() |> ReadOnlyMemory
| Encoding.Brotli, JsonValueKind.String -> Encoding.Brotli, data.GetBytesFromBase64() |> ReadOnlyMemory
| _ -> Encoding.Direct, data |> InteropHelpers.JsonElementToUtf8

(* Conditional compression logic: triggered as storage layer pulls Data/Meta fields
Bodies under specified minimum size, or not meeting a required compression gain are stored directly, equivalent to if compression had not been wired in *)
Expand Down Expand Up @@ -85,7 +90,7 @@ type Encoding private () =
static member OfUtf8Compress(options, x: ReadOnlyMemory<byte>): EncodedBody =
Impl.compressUtf8 options.minSize options.minGain x
static member OfEncodedUtf8(x: FsCodec.EncodedBody): EncodedBody =
Impl.recode x
Impl.ofEncodedBody x
static member ByteCount((_encoding, data): EncodedBody) =
data.GetRawText() |> System.Text.Encoding.UTF8.GetByteCount
static member ByteCountExpanded(x: EncodedBody) =
Expand All @@ -94,6 +99,8 @@ type Encoding private () =
Impl.decode x
static member ToUtf8(x: EncodedBody): ReadOnlyMemory<byte> =
Impl.decodeUtf8 x
static member ToEncodedUtf8(x: EncodedBody): FsCodec.EncodedBody =
Impl.toEncodedBody x
static member ToStream(ms: System.IO.Stream, x: EncodedBody) =
Impl.decode_ (fun el -> JsonSerializer.Serialize(ms, el)) (fun dec -> dec ms) x

Expand Down Expand Up @@ -138,7 +145,7 @@ type Encoder private () =
let encode = shouldCompress |> function
| None -> fun _x (d: JsonElement) -> Encoding.OfJsonElementCompress(opts, d)
| Some predicate -> fun x d -> if predicate.Invoke x then Encoding.OfJsonElementCompress(opts, d) else Encoding.OfJsonElement d
FsCodec.Core.EventCodec.mapBodies_ encode Encoding.ToJsonElement native
FsCodec.Core.EventCodec.mapBodies_ encode Encoding.ToJsonElement native

/// <summary>Adapts an <c>IEventCodec</c> rendering to <c>int * JsonElement</c> Event Bodies to render and/or consume uncompressed <c>ReadOnlyMemory&lt;byte&gt;</c>.</summary>
[<Extension>]
Expand Down

0 comments on commit 33371e0

Please sign in to comment.