Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions src/stdlib/Json.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ let private getCases (o: obj) : string array = nativeOnly
[<Emit("(_ for _ in ()).throw(TypeError(f'Object of type {type($0).__name__} is not JSON serializable'))")>]
let private raiseTypeError (o: obj) : obj = nativeOnly

[<Emit("list($0)")>]
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 =
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions test/TestJson.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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]"""

[<Fact>]
let ``test Json.dumps with F# array works`` () =
let values = [| 1; 2; 3 |]
let result = Json.dumps values
result |> equal "[1, 2, 3]"

[<Fact>]
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"]"""

[<Fact>]
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]}"""

[<Fact>]
let ``test Json.dumps with empty F# array works`` () =
let values: int array = [||]
let result = Json.dumps values
result |> equal "[]"