Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/json-control-indentation-spaces #1507

Merged
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
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
Loading