如何使用JSON.NET在C#中序列化PSObject?
我正在编写一个Cmdlet,需要将对象结构传递给可能包含PSObjects的API客户端.目前,这些序列化作为包含CLI
XML的
JSON字符串.相反,我需要将它视为一个对象(包括PSObject.Properties中的NoteProperties作为属性,并递归地序列化它们的值).
我尝试编写自己的JsonConverter但由于某种原因它只调用顶级对象,而不是嵌套的PSObjects: public class PSObjectJsonConverter : JsonConverter { public override void WriteJson(JsonWriter writer,object value,JsonSerializer serializer) { if (value is PSObject) { JObject obj = new JObject(); foreach (var prop in ((PSObject)value).Properties) { obj.Add(new JProperty(prop.Name,value)); } obj.WriteTo(writer); } else { JToken token = JToken.FromObject(value); token.WriteTo(writer); } } public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer) { throw new NotImplementedException(); } public override bool CanRead { get { return false; } } public override bool CanConvert(Type objectType) { return true; } } 此外,我正在使用CamelCasePropertyNamesContractResolver序列化到驼峰案例.有没有办法让转换器尊重? 解决方法
以下转换器应正确序列化类型为
PSObject 的递归嵌套对象:
public class PSObjectJsonConverter : JsonConverter { public override bool CanConvert(Type objectType) { return typeof(PSObject).IsAssignableFrom(objectType); } public override void WriteJson(JsonWriter writer,JsonSerializer serializer) { var psObj = (PSObject)value; writer.WriteStartObject(); foreach (var prop in psObj.Properties) { //Probably we shouldn't try to serialize a property that can't be read. //https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.pspropertyinfo.isgettable?view=powershellsdk-1.1.0#System_Management_Automation_PSPropertyInfo_IsGettable if (!prop.IsGettable) continue; writer.WritePropertyName(prop.Name); serializer.Serialize(writer,prop.Value); } writer.WriteEndObject(); } public override object ReadJson(JsonReader reader,JsonSerializer serializer) { throw new NotImplementedException(); } public override bool CanRead { get { return false; } } } 笔记: >在 更新:此外,我正在使用CamelCasePropertyNamesContractResolver序列化到驼峰案例.有没有办法让转换器尊重? 为类型引入custom public override void WriteJson(JsonWriter writer,JsonSerializer serializer) { var psObj = (PSObject)value; writer.WriteStartObject(); var resolver = serializer.ContractResolver as DefaultContractResolver; var strategy = (resolver == null ? null : resolver.NamingStrategy) ?? new DefaultNamingStrategy(); foreach (var prop in psObj.Properties) { //Probably we shouldn't try to serialize a property that can't be read. //https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.pspropertyinfo.isgettable?view=powershellsdk-1.1.0#System_Management_Automation_PSPropertyInfo_IsGettable if (!prop.IsGettable) continue; writer.WritePropertyName(strategy.GetPropertyName(prop.Name,false)); serializer.Serialize(writer,prop.Value); } writer.WriteEndObject(); } 请注意,命名策略是在Json.NET 9.0.1中引入的,因此如果您使用的是早期版本,则需要创建自己的驼峰案例名称映射器,如this answer中所示. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- vb.net – 使用关键字:.Close或.Dispose
- 文件 – 为什么使用mmap而不是fread?
- C#中调用Windows API的技术要点说明
- flash – ActionScript编译器
- 正则表达式 – BeautifulSoup.find的返回值是多少?
- 正则表达式 – 什么时候最好使用正则表达式超过基本字符串分
- React Native(四):布局(使用Flexbox)
- ruby – 为什么ActiveRecord :: Base.connected?在建立_连
- React Native Android:screenPhysicalPixels.width未定义
- actionscript-3 – AS3最大文本字段的宽度