From 3eb77ef330663c970793ac6f163e31cfc023dfc8 Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Thu, 18 Dec 2025 23:24:23 +0100 Subject: [PATCH] feat: add typed array support to Json serialization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add support for serializing F# typed arrays (Int32Array, Int64Array, etc.) to JSON by converting them to Python lists. Supported array types: - Generic arrays: FSharpArray, GenericArray - Typed arrays: Int8Array, Int16Array, Int32Array, Int64Array, UInt8Array, UInt16Array, UInt32Array, UInt64Array, Float32Array, Float64Array 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- release-please-config.json | 2 +- src/stdlib/Json.fs | 18 ++++++++++++++++++ test/TestJson.fs | 24 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/release-please-config.json b/release-please-config.json index 005a527..aa93e33 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -3,7 +3,7 @@ "packages": { ".": { "release-type": "simple", - "release-as": "5.0.0-alpha.21.1", + "release-as": "5.0.0-alpha.21.2", "include-component-in-tag": false, "include-v-in-tag": true, "changelog-path": "CHANGELOG.md" diff --git a/src/stdlib/Json.fs b/src/stdlib/Json.fs index 79886ec..d87043d 100644 --- a/src/stdlib/Json.fs +++ b/src/stdlib/Json.fs @@ -86,9 +86,13 @@ let private getCases (o: obj) : string array = nativeOnly [] let private raiseTypeError (o: obj) : obj = nativeOnly +[] +let private toList (o: obj) : obj = nativeOnly + /// Default function for JSON serialization of Fable types. /// Handles Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64 → int /// Handles Float32, Float64 → float +/// Handles typed arrays (Int32Array, Int64Array, Float64Array, etc.) → list /// Handles Union types (tag, fields, cases) → [caseName, ...fields] or just caseName /// Handles Record types (__slots__) → dict of slot names to values let fableDefault (o: obj) : obj = @@ -105,6 +109,20 @@ let fableDefault (o: obj) : obj = | "UInt64" -> toInt o | "Float32" | "Float64" -> toFloat o + // Generic arrays + | "FSharpArray" + | "GenericArray" + // Typed arrays + | "Int8Array" + | "Int16Array" + | "Int32Array" + | "Int64Array" + | "UInt8Array" + | "UInt16Array" + | "UInt32Array" + | "UInt64Array" + | "Float32Array" + | "Float64Array" -> toList o | _ -> if hasattr o "tag" && hasattr o "fields" then let cases = getCases o diff --git a/test/TestJson.fs b/test/TestJson.fs index 28a380a..b4e18af 100644 --- a/test/TestJson.fs +++ b/test/TestJson.fs @@ -94,3 +94,27 @@ let ``test Json.dumps with union case with multiple fields works`` () = let union = CaseC("hello", 123) let result = Json.dumps union result |> equal """["CaseC", "hello", 123]""" + +[] +let ``test Json.dumps with F# array works`` () = + let values = [| 1; 2; 3 |] + let result = Json.dumps values + result |> equal "[1, 2, 3]" + +[] +let ``test Json.dumps with F# array of strings works`` () = + let values = [| "a"; "b"; "c" |] + let result = Json.dumps values + result |> equal """["a", "b", "c"]""" + +[] +let ``test Json.dumps with nested F# array works`` () = + let obj = {| Items = [| 1; 2; 3 |] |} + let result = Json.dumps obj + result |> equal """{"Items": [1, 2, 3]}""" + +[] +let ``test Json.dumps with empty F# array works`` () = + let values: int array = [||] + let result = Json.dumps values + result |> equal "[]"