diff --git a/packages/Thoth.Json.Core/CHANGELOG.md b/packages/Thoth.Json.Core/CHANGELOG.md index d28f294..06b35a5 100644 --- a/packages/Thoth.Json.Core/CHANGELOG.md +++ b/packages/Thoth.Json.Core/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added * Add `resizeArray` support ([GH-182](https://github.com/thoth-org/Thoth.Json/issues/182)) +* Add `IEncoderHelpers.encodeResizeArray` ([GH-199](https://github.com/thoth-org/Thoth.Json/issues/199)) ### Changed diff --git a/packages/Thoth.Json.Core/Encode.fs b/packages/Thoth.Json.Core/Encode.fs index cea7965..77e89a1 100644 --- a/packages/Thoth.Json.Core/Encode.fs +++ b/packages/Thoth.Json.Core/Encode.fs @@ -74,9 +74,12 @@ module Encode = let resizeArray (values: IEncodable ResizeArray) = { new IEncodable with member _.Encode(helpers) = - values - |> Seq.map (fun v -> v.Encode(helpers)) - |> helpers.encodeSeq + let result = ResizeArray(values.Count) + + for v in values do + result.Add(v.Encode(helpers)) + + helpers.encodeResizeArray result } let dict (values: Map) : IEncodable = diff --git a/packages/Thoth.Json.Core/Types.fs b/packages/Thoth.Json.Core/Types.fs index 776d321..e670c7a 100644 --- a/packages/Thoth.Json.Core/Types.fs +++ b/packages/Thoth.Json.Core/Types.fs @@ -29,6 +29,7 @@ type IEncoderHelpers<'JsonValue> = abstract encodeArray: 'JsonValue array -> 'JsonValue abstract encodeList: 'JsonValue list -> 'JsonValue abstract encodeSeq: 'JsonValue seq -> 'JsonValue + abstract encodeResizeArray: ResizeArray<'JsonValue> -> 'JsonValue // See https://github.com/thoth-org/Thoth.Json/issues/187 for more information // about why we make a distinction between signed and unsigned integral numbers // when encoding them. diff --git a/packages/Thoth.Json.JavaScript/Encode.fs b/packages/Thoth.Json.JavaScript/Encode.fs index d58f56a..65d8ce7 100644 --- a/packages/Thoth.Json.JavaScript/Encode.fs +++ b/packages/Thoth.Json.JavaScript/Encode.fs @@ -28,6 +28,9 @@ module Encode = member _.encodeList values = JS.Constructors.Array.from values member _.encodeSeq values = JS.Constructors.Array.from values + member _.encodeResizeArray values = + JS.Constructors.Array.from values + member _.encodeSignedIntegralNumber value = box value member _.encodeUnsignedIntegralNumber value = box value } diff --git a/packages/Thoth.Json.Newtonsoft/Encode.fs b/packages/Thoth.Json.Newtonsoft/Encode.fs index bbfd1e8..3415e91 100644 --- a/packages/Thoth.Json.Newtonsoft/Encode.fs +++ b/packages/Thoth.Json.Newtonsoft/Encode.fs @@ -27,6 +27,7 @@ module Encode = member _.encodeArray values = JArray(values) member _.encodeList values = JArray(values) member _.encodeSeq values = JArray(values) + member _.encodeResizeArray values = JArray(values) member _.encodeSignedIntegralNumber(value: int32) = JValue(value) diff --git a/packages/Thoth.Json.Python/Encode.fs b/packages/Thoth.Json.Python/Encode.fs index 6006ea4..899f44e 100644 --- a/packages/Thoth.Json.Python/Encode.fs +++ b/packages/Thoth.Json.Python/Encode.fs @@ -32,6 +32,9 @@ module Encode = member this.encodeSeq values = values |> Seq.toArray |> this.encodeArray + member this.encodeResizeArray values = + values.ToArray() |> this.encodeArray + member _.encodeSignedIntegralNumber value = box value member _.encodeUnsignedIntegralNumber value = box value }