Skip to content

Commit 3eb77ef

Browse files
dbrattliclaude
andcommitted
feat: add typed array support to Json serialization
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 <[email protected]>
1 parent 027207a commit 3eb77ef

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

release-please-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"packages": {
44
".": {
55
"release-type": "simple",
6-
"release-as": "5.0.0-alpha.21.1",
6+
"release-as": "5.0.0-alpha.21.2",
77
"include-component-in-tag": false,
88
"include-v-in-tag": true,
99
"changelog-path": "CHANGELOG.md"

src/stdlib/Json.fs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ let private getCases (o: obj) : string array = nativeOnly
8686
[<Emit("(_ for _ in ()).throw(TypeError(f'Object of type {type($0).__name__} is not JSON serializable'))")>]
8787
let private raiseTypeError (o: obj) : obj = nativeOnly
8888

89+
[<Emit("list($0)")>]
90+
let private toList (o: obj) : obj = nativeOnly
91+
8992
/// Default function for JSON serialization of Fable types.
9093
/// Handles Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64 → int
9194
/// Handles Float32, Float64 → float
95+
/// Handles typed arrays (Int32Array, Int64Array, Float64Array, etc.) → list
9296
/// Handles Union types (tag, fields, cases) → [caseName, ...fields] or just caseName
9397
/// Handles Record types (__slots__) → dict of slot names to values
9498
let fableDefault (o: obj) : obj =
@@ -105,6 +109,20 @@ let fableDefault (o: obj) : obj =
105109
| "UInt64" -> toInt o
106110
| "Float32"
107111
| "Float64" -> toFloat o
112+
// Generic arrays
113+
| "FSharpArray"
114+
| "GenericArray"
115+
// Typed arrays
116+
| "Int8Array"
117+
| "Int16Array"
118+
| "Int32Array"
119+
| "Int64Array"
120+
| "UInt8Array"
121+
| "UInt16Array"
122+
| "UInt32Array"
123+
| "UInt64Array"
124+
| "Float32Array"
125+
| "Float64Array" -> toList o
108126
| _ ->
109127
if hasattr o "tag" && hasattr o "fields" then
110128
let cases = getCases o

test/TestJson.fs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,27 @@ let ``test Json.dumps with union case with multiple fields works`` () =
9494
let union = CaseC("hello", 123)
9595
let result = Json.dumps union
9696
result |> equal """["CaseC", "hello", 123]"""
97+
98+
[<Fact>]
99+
let ``test Json.dumps with F# array works`` () =
100+
let values = [| 1; 2; 3 |]
101+
let result = Json.dumps values
102+
result |> equal "[1, 2, 3]"
103+
104+
[<Fact>]
105+
let ``test Json.dumps with F# array of strings works`` () =
106+
let values = [| "a"; "b"; "c" |]
107+
let result = Json.dumps values
108+
result |> equal """["a", "b", "c"]"""
109+
110+
[<Fact>]
111+
let ``test Json.dumps with nested F# array works`` () =
112+
let obj = {| Items = [| 1; 2; 3 |] |}
113+
let result = Json.dumps obj
114+
result |> equal """{"Items": [1, 2, 3]}"""
115+
116+
[<Fact>]
117+
let ``test Json.dumps with empty F# array works`` () =
118+
let values: int array = [||]
119+
let result = Json.dumps values
120+
result |> equal "[]"

0 commit comments

Comments
 (0)