Skip to content

Commit

Permalink
feat: expose a dedicated encodeResizeArray helpers for encoding `Re…
Browse files Browse the repository at this point in the history
…sizeArray`

[core][javascript][newtonsoft][python]

Fix #199
  • Loading branch information
MangelMaxime committed Jun 15, 2024
1 parent 1fab3ff commit d9f50fe
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/Thoth.Json.Core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions packages/Thoth.Json.Core/Encode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, IEncodable>) : IEncodable =
Expand Down
1 change: 1 addition & 0 deletions packages/Thoth.Json.Core/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions packages/Thoth.Json.JavaScript/Encode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions packages/Thoth.Json.Newtonsoft/Encode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions packages/Thoth.Json.Python/Encode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit d9f50fe

Please sign in to comment.