Skip to content

Commit

Permalink
Merge pull request #1485 from bonjune/json-save-options
Browse files Browse the repository at this point in the history
  • Loading branch information
cartermp authored Apr 30, 2023
2 parents ef2eda4 + 39da6c2 commit 13afb37
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/FSharp.Data.Json.Core/JsonValue.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -96,7 +108,7 @@ type JsonValue =
w.Write "["

for i = 0 to elements.Length - 1 do
if i > 0 then w.Write ","
if i > 0 then comma ()
newLine indentation 2
serialize (indentation + 2) elements.[i]

Expand Down

0 comments on commit 13afb37

Please sign in to comment.