From 05156509a313d866d83ff1979e3e385abf2ca0fd Mon Sep 17 00:00:00 2001 From: Bongjun Jang Date: Sat, 29 Apr 2023 10:20:31 +0900 Subject: [PATCH 1/2] Add JsonSaveOptions case (#1482) --- src/FSharp.Data.Json.Core/JsonValue.fs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/FSharp.Data.Json.Core/JsonValue.fs b/src/FSharp.Data.Json.Core/JsonValue.fs index 708ab1fb1..49c60b7c3 100644 --- a/src/FSharp.Data.Json.Core/JsonValue.fs +++ b/src/FSharp.Data.Json.Core/JsonValue.fs @@ -26,6 +26,11 @@ type JsonSaveOptions = /// Print the JsonValue in one line in a compact way | DisableFormatting = 1 + /// Print the JsonValue in one line in a compact way, + /// but place a single space after every comma + /// https://github.com/fsprojects/FSharp.Data/issues/1482 + | CompactSpaceAfterComma = 2 + /// Represents a JSON value. Large numbers that do not fit in the /// Decimal type are represented using the Float case, while /// smaller numbers are represented as decimals to avoid precision loss. @@ -67,6 +72,13 @@ type JsonValue = let propSep = if saveOptions = JsonSaveOptions.None then "\": " else "\":" + let comma () = + match saveOptions with + | JsonSaveOptions.None -> w.Write "," + | JsonSaveOptions.DisableFormatting -> w.Write "," + | JsonSaveOptions.CompactSpaceAfterComma -> w.Write ", " + | _ -> failwith "Invalid JsonSaveOptions" + let rec serialize indentation = function | Null -> w.Write "null" @@ -83,7 +95,7 @@ type JsonValue = for i = 0 to properties.Length - 1 do let k, v = properties.[i] - if i > 0 then w.Write "," + if i > 0 then comma () newLine indentation 2 w.Write "\"" JsonValue.JsonStringEncodeTo w k @@ -96,7 +108,7 @@ type JsonValue = w.Write "[" for i = 0 to elements.Length - 1 do - if i > 0 then w.Write "," + comma () newLine indentation 2 serialize (indentation + 2) elements.[i] From 39da6c2d6a841baee019cd03439ad9240b5b91c7 Mon Sep 17 00:00:00 2001 From: Bongjun Jang Date: Sat, 29 Apr 2023 10:24:00 +0900 Subject: [PATCH 2/2] fix --- src/FSharp.Data.Json.Core/JsonValue.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FSharp.Data.Json.Core/JsonValue.fs b/src/FSharp.Data.Json.Core/JsonValue.fs index 49c60b7c3..25378592a 100644 --- a/src/FSharp.Data.Json.Core/JsonValue.fs +++ b/src/FSharp.Data.Json.Core/JsonValue.fs @@ -108,7 +108,7 @@ type JsonValue = w.Write "[" for i = 0 to elements.Length - 1 do - comma () + if i > 0 then comma () newLine indentation 2 serialize (indentation + 2) elements.[i]