void TryFixJson(string inputFile, string outputFile) { using (var jsonTextReader = new StreamReader(inputFile)) using (var jsonReader = new Newtonsoft.Json.JsonTextReader(jsonTextReader)) using (var jsonTextWriter = new StreamWriter(outputFile)) using (var jsonWriter = new Newtonsoft.Json.JsonTextWriter(jsonTextWriter) { AutoCompleteOnClose = true, Formatting = Newtonsoft.Json.Formatting.Indented }) { try { jsonWriter.WriteToken(jsonReader, writeChildren: true); } catch (Newtonsoft.Json.JsonReaderException) { } } }
public void Pretty() { var baseModel = Factory.GetAllTypesModel(); var json = JsonSerializer.Serialize(baseModel); string jsonPretty; using (var stringReader = new StringReader(json)) using (var stringWriter = new StringWriter()) { var jsonReader = new Newtonsoft.Json.JsonTextReader(stringReader); var jsonWriter = new Newtonsoft.Json.JsonTextWriter(stringWriter) { Formatting = Newtonsoft.Json.Formatting.Indented, Indentation = 4 }; jsonWriter.WriteToken(jsonReader); jsonPretty = stringWriter.ToString(); } var model = JsonSerializer.Deserialize <AllTypesModel>(jsonPretty); Factory.AssertAreEqual(baseModel, model); }