using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
public string Street { get; set; }
public string StreetNumber { get; set; }
public class NameRemappingJsonWriter : JsonTextWriter
readonly Action<string> jsonWriterWritePropertyName;
readonly Func<string, int, string, string> map;
public NameRemappingJsonWriter(TextWriter textWriter, Func<string, int, string, string> map) : base(textWriter)
this.map = map ?? throw new ArgumentNullException(nameof(map));
var ptr = typeof(JsonWriter).GetMethod("WritePropertyName", new[] { typeof(string) }).MethodHandle.GetFunctionPointer();
jsonWriterWritePropertyName = (Action<string>)Activator.CreateInstance(typeof(Action<string>), this, ptr);
public override void WritePropertyName(string name) => WritePropertyName(name, true);
public override void WritePropertyName(string name, bool escape)
jsonWriterWritePropertyName(name);
WriteRaw(JsonConvert.ToString(map(name, Top, Path)));
public static void Test()
Street = "Evergreen Terrace",
var settings = new JsonSerializerSettings
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Func<string, int, string, string> map = (name, depth, parentPath) => "person." + parentPath;
using var textWriter = new StringWriter();
using (var jsonWriter = new NameRemappingJsonWriter(textWriter, map) { Formatting = Formatting.Indented })
JsonSerializer.CreateDefault(settings).Serialize(jsonWriter, person);
var json = textWriter.ToString();
Assert.IsTrue(JToken.DeepEquals(JToken.Parse(GetRequiredJson()), JToken.Parse(json)));
static string GetRequiredJson() => @"{
""person.firstName"":""Homer"",
""person.lastName"":""Simpson"",
""person.address.street"":""Evergreen Terrace"",
""person.address.streetNumber"":""742""
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, 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.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];
public class PropertyPathNameJsonTextWriter : JsonTextWriter
Action<string> jsonWriterWritePropertyName;
public PropertyPathNameJsonTextWriter(TextWriter textWriter)
var ptr = typeof(JsonWriter).GetMethod("WritePropertyName", new[] { typeof(string) }).MethodHandle.GetFunctionPointer();
jsonWriterWritePropertyName = (Action<string>)Activator.CreateInstance(typeof(Action<string>), this, ptr);
public override void WritePropertyName(string name)
WritePropertyName(name, true);
public override void WritePropertyName(string name, bool escape)
jsonWriterWritePropertyName(name);
WriteRaw(escape ? JsonConvert.ToString(Path) : Path);