using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public class ReplacingStringWritingConverter : JsonConverter
readonly string oldValue;
readonly string newValue;
public ReplacingStringWritingConverter(string oldValue, string newValue)
if (string.IsNullOrEmpty(oldValue))
throw new ArgumentException("string.IsNullOrEmpty(oldValue)");
throw new ArgumentNullException("newValue");
this.oldValue = oldValue;
this.newValue = newValue;
public override bool CanConvert(Type objectType)
return objectType == typeof(string);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
throw new NotImplementedException();
public override bool CanRead { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
var s = ((string)value).Replace(oldValue, newValue);
public static void Test()
public static void Test<RootObject>()
var root = JsonConvert.DeserializeObject<RootObject>(GetJson());
var settings = new JsonSerializerSettings { Converters = { new ReplacingStringWritingConverter("\n", "") } };
var newJson = JsonConvert.SerializeObject(root, Formatting.None, settings);
Console.WriteLine(newJson);
Assert.IsTrue(!newJson.Contains("\\n"));
var newJsonWithoutReplacing = JsonConvert.SerializeObject(root, Formatting.None);
Assert.IsTrue(newJsonWithoutReplacing.Contains("\\n"));
""ActionDate"": ""20190912"",
""EmployeeCode"": ""EPL2"",
""LocalEntityCode"": ""K"",
""PolicyHolderCode"": ""1YAAAAC"",
""Name1"": ""monoliability PDL\n"",
""FMEPaymentFrequency1"": ""00"",
""Reference"": ""C00-AAC""
""PolicyHolderTrailerInfo"": {
""CountryCode"": ""KSA"",
""SourceIndicator"": ""EPLC"",
""SourceSystemDate"": ""2019-09-17T17:48:31.3543314+08:00""
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];