Pascal case dynamic properties with Json.NET

前端 未结 4 811
一整个雨季
一整个雨季 2020-12-06 15:57

This is what I have:

using Newtonsoft.Json;

var json = \"{\\\"someProperty\\\":\\\"some value\\\"}\";
dynamic deserialized = JsonConvert.DeserializeObject(j         


        
4条回答
  •  有刺的猬
    2020-12-06 16:42

    I can't help but feel that this isn't a good idea. It seems like you're trying to preserve a coding convention, but at the expense of maintaining fidelity between the wire format (the JSON structs) and your logic classes. This can cause confusion for developers who expect the JSON classes to be preserved, and might cause issues if you also need, or will need in the future, to re-serialize this data into the same JSON format.

    That said, this probably can be achieved by creating the .NET classes ahead of time, then using the DeserializeObject(string value, JsonSerializerSettings settings) overload, passing it a JsonSerializerSettings with the Binder property set. You will also need to write a custom SerializationBinder in which you manually define the relationship between your JSON class and your predefined .NET class.

    It may be possible to generate Pascal-cased classes in runtime without predefining them, but I haven't delved deep enough into the JSON.NET implementation for that. Perhaps one of the other JsonSerializerSettings settings, like passing a CustomCreationConverter, but I'm not sure of the details.

提交回复
热议问题