Reputation: 1457
I have simple code to serialise my object into JSON:
Dim json As String = JsonConvert.SerializeObject(MyObject, Formatting.Indented)
How can I change indent char to "-" in resulting JSON? I know there is JsonTextWriter.IndentChar
but I have no clue how to implement it together with JsonConvert.SerializeObject
Upvotes: 2
Views: 2160
Reputation: 6230
There's an answer in the linked question below from Yuval Itzchakov that creates a JsonTextWriter
, sets an IndentChar
and then serializes with the modified settings. Here's that code rewritten in VB and using the requested '-' char.
Sub Main
Dim anon = New With { .Name = "Yuval", .Age = 1 }
Dim result = SerializeObject(anon)
Console.WriteLine(result)
End Sub
Public Function SerializeObject(Of t)(ByVal arg As t) As String
Dim sw = New StringWriter()
Using jsonWriter = New JsonTextWriter(sw)
jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented
jsonWriter.IndentChar = "-"C
jsonWriter.Indentation = 4
Dim jsonSerializer = Newtonsoft.Json.JsonSerializer.CreateDefault()
jsonSerializer.Serialize(jsonWriter, arg)
End Using
Return sw.ToString()
End Function
Results
{
----"Name": "Yuval",
----"Age": 1
}
Source: Customize indentation parameter in JsonConvert.SerializeObject
Upvotes: 3