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 EntityProperty
public string Name { get; set; }
public IDictionary<string, JToken> OtherProperties { get; set; }
public string Type { get; set; }
public List<EntityProperty> Properties { get; set; }
public class PropertyNameMappingJsonReader : JsonTextReader
readonly Func<string, string> nameMapper;
public PropertyNameMappingJsonReader(TextReader textReader, Func<string, string> nameMapper)
throw new ArgumentNullException();
this.nameMapper = nameMapper;
public override object Value
if (TokenType == JsonToken.PropertyName)
return nameMapper((string)base.Value);
public static class JsonExtensions
public static T DeserializeObject<T>(string json, Func<string, string> nameMapper, JsonSerializerSettings settings = null)
using (var textReader = new StringReader(json))
using (var jsonReader = new PropertyNameMappingJsonReader(textReader, nameMapper))
return JsonSerializer.CreateDefault(settings).Deserialize<T>(jsonReader);
public static void Test()
var root = JsonExtensions.DeserializeObject<RootObject>(json, (s) => s.Replace(".", ""));
var outputJson = JsonConvert.SerializeObject(root, Formatting.Indented);
Console.WriteLine(outputJson);
Assert.IsTrue(!((JContainer)JToken.Parse(outputJson)).DescendantsAndSelf().OfType<JProperty>().Any(p => p.Name.Contains(".")));
Console.WriteLine("Done.");
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
public static void IsTrue(bool value, string message)
throw new AssertionFailedException(message ?? "failed");