Skip to content

Commit

Permalink
Merge pull request #1507 from njlr/feature/json-control-indentation-s…
Browse files Browse the repository at this point in the history
…paces

feature/json-control-indentation-spaces
  • Loading branch information
cartermp authored Apr 21, 2024
2 parents 674cacf + 663b8b7 commit c5b026b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
18 changes: 11 additions & 7 deletions src/FSharp.Data.Json.Core/JsonValue.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ type JsonValue =
str

/// Serializes the JsonValue to the specified System.IO.TextWriter.
member x.WriteTo(w: TextWriter, saveOptions) =
member x.WriteTo(w: TextWriter, saveOptions, ?indentationSpaces: int) =
let indentationSpaces = defaultArg indentationSpaces 2

let newLine =
if saveOptions = JsonSaveOptions.None then
Expand Down Expand Up @@ -96,11 +97,11 @@ type JsonValue =
for i = 0 to properties.Length - 1 do
let k, v = properties.[i]
if i > 0 then comma ()
newLine indentation 2
newLine indentation indentationSpaces
w.Write "\""
JsonValue.JsonStringEncodeTo w k
w.Write propSep
serialize (indentation + 2) v
serialize (indentation + indentationSpaces) v

newLine indentation 0
w.Write "}"
Expand All @@ -109,8 +110,8 @@ type JsonValue =

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

if elements.Length > 0 then newLine indentation 0
w.Write "]"
Expand Down Expand Up @@ -140,11 +141,14 @@ type JsonValue =
| '\\' -> w.Write "\\\\"
| _ -> w.Write c

member x.ToString saveOptions =
member x.ToString(saveOptions, ?indentationSpaces: int) =
let w = new StringWriter(CultureInfo.InvariantCulture)
x.WriteTo(w, saveOptions)
x.WriteTo(w, saveOptions, ?indentationSpaces = indentationSpaces)
w.GetStringBuilder().ToString()

member x.ToString(?indentationSpaces: int) =
x.ToString(JsonSaveOptions.None, ?indentationSpaces = indentationSpaces)

override x.ToString() = x.ToString(JsonSaveOptions.None)

/// <exclude />
Expand Down
32 changes: 26 additions & 6 deletions tests/FSharp.Data.Core.Tests/JsonValue.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ let ``Can parse document with iso date``() =
j?anniversary.AsDateTime() |> should equal (new DateTime(2009, 05, 19, 14, 39, 22, 500, DateTimeKind.Local))
j?anniversary.AsDateTime().Kind |> should equal DateTimeKind.Local

let withCulture (cultureName: string) =
let withCulture (cultureName: string) =
let originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture <- CultureInfo cultureName
{ new IDisposable with
member _.Dispose() =
{ new IDisposable with
member _.Dispose() =
CultureInfo.CurrentCulture <- originalCulture }

[<Test>]
Expand Down Expand Up @@ -139,7 +139,7 @@ let ``Can parse time span in different culture``() =
j?duration.AsTimeSpan CultureInfo.CurrentCulture |> should equal (TimeSpan(1, 3, 16, 50, 500))

[<Test>]
let ``Can parse UTF-32 unicode characters`` () =
let ``Can parse UTF-32 unicode characters`` () =
let j = JsonValue.Parse """{ "value": "\U00010343\U00010330\U0001033F\U00010339\U0001033B" }"""
j?value.AsString() |> should equal "\U00010343\U00010330\U0001033F\U00010339\U0001033B"

Expand Down Expand Up @@ -317,6 +317,26 @@ let ``Pretty printing works``() =
"z": []
}""")

[<Test>]
let ``Pretty printing with specified indentation works``() =
let text = """{"items":[{"id":"Open"},null,{"id":25}],"x":{"y":2},"z":[]}"""
let json = JsonValue.Parse text
json.ToString(indentationSpaces = 3) |> normalize |> should equal (normalize """{
"items": [
{
"id": "Open"
},
null,
{
"id": 25
}
],
"x": {
"y": 2
},
"z": []
}""")

[<Test>]
let ``Can parse various JSON documents``() =
let IsFloatNear (l : float) (r : float) =
Expand Down Expand Up @@ -467,15 +487,15 @@ let ``Can parse various JSON documents``() =
Assert.Fail <| failures.ToString ()

[<Test>]
let ``Basic special characters encoded correctly`` () =
let ``Basic special characters encoded correctly`` () =
let input = " \"quoted\" and \'quoted\' and \r\n and \uABCD "
let w = new IO.StringWriter()
JsonValue.JsonStringEncodeTo w input
let expected = " \\\"quoted\\\" and 'quoted' and \\r\\n and \uABCD "
(w.GetStringBuilder().ToString()) |> should equal expected

[<Test>]
let ``Encoding of simple string is valid JSON`` () =
let ``Encoding of simple string is valid JSON`` () =
let input = "sample \"json\" with \t\r\n \' quotes etc."
let w = new IO.StringWriter()
JsonValue.JsonStringEncodeTo w input
Expand Down

0 comments on commit c5b026b

Please sign in to comment.