需要将 C# 字典序列化和反序列化为 JSON 数组。我还想使用数组索引表示法从 powershell 读取 JSON。
默认情况下,JSON 格式为:
{
"defaultSettings": {
"applications": {
"Apollo": {
"environments": {
"DEV": {
"dbKeyTypes": {
"DmkPassword": "AEikOooIuGxXC9UBJQ3ckDj7Q126tB06",
"SymmetricKeySource": "bTU7XOAYA2FFifmiBUggu99yHxX3Ftds"
}
},
"TST": {
"dbKeyTypes": {
"DmkPassword": "AEikOooIuGxXC9UBJQ3ckDj7Q126tB06",
"SymmetricKeySource": "bTU7XOAYA2FFifmiBUggu99yHxX3Ftds"
}
}
}
},
"Gemini": {
"environments": {
"DEV": {
"dbKeyTypes": {
"DmkPassword": "AEikOooIuGxXC9UBJQ3ckDj7Q126tB06",
"SymmetricKeySource": "bTU7XOAYA2FFifmiBUggu99yHxX3Ftds"
}
},
"TST": {
"dbKeyTypes": {
"DmkPassword": "AEikOooIuGxXC9UBJQ3ckDj7Q126tB06",
"SymmetricKeySource": "bTU7XOAYA2FFifmiBUggu99yHxX3Ftds"
}
}
}
}
}
}
}
使用 .Net Core 中的默认 json 阅读器效果很好,但它不允许我在 PowerShell 中使用数组索引表示法。
相反,我正在寻找的是:
{
"defaultSettings": {
"applications": [
{
"Apollo": {
"environments": [
{
"DEV": {
"dbKeyTypes": [
{
"DmkPassword": "AEikOooIuGxXC9UBJQ3ckDj7Q126tB06"
},
{
"SymmetricKeySource": "bTU7XOAYA2FFifmiBUggu99yHxX3Ftds"
}
]
}
},
{
"TST": {
"dbKeyTypes": [
{
"DmkPassword": "AEikOooIuGxXC9UBJQ3ckDj7Q126tB06"
},
{
"SymmetricKeySource": "bTU7XOAYA2FFifmiBUggu99yHxX3Ftds"
}
]
}
}
]
}
},
{
"Gemini": {
"environments": [
{
"DEV": {
"dbKeyTypes": [
{
"DmkPassword": "AEikOooIuGxXC9UBJQ3ckDj7Q126tB06"
},
{
"SymmetricKeySource": "bTU7XOAYA2FFifmiBUggu99yHxX3Ftds"
}
]
}
},
{
"TST": {
"dbKeyTypes": [
{
"DmkPassword": "AEikOooIuGxXC9UBJQ3ckDj7Q126tB06"
},
{
"SymmetricKeySource": "bTU7XOAYA2FFifmiBUggu99yHxX3Ftds"
}
]
}
}
]
}
}
]
}
}
我正在使用序列化 Dictionary<string,string> 中的 WriteJson 部分到“name”数组:“value”
这很好用;但是,当然,由于该ReadJson()
方法未实现,因此它不会读取。顺便说一句,为了获得上述所需的 json 格式,我将链接中的 CustomDictionaryConverter 修改为:
writer.WritePropertyName(key.ToString());
//writer.WriteValue(key);
//writer.WritePropertyName("value");
serializer.Serialize(writer, valueEnumerator.Current);
实现背后的类是:
public enum DeploymentEnvironment { DEV = 1, TST = 2 }
public enum TargetApplication { Apollo = 1, Gemini = 2 }
public enum DbKeyType { DmkPassword = 1, SymmetricKeySource = 2 }
public class DeploymentSettings
{
[JsonProperty("defaultSettings")]
public DefaultSettings DefaultSettings { get; set; }
public DeploymentSettings()
{
DefaultSettings = new DefaultSettings();
}
}
public partial class DefaultSettings
{
[JsonProperty("applications")]
public Dictionary<TargetApplication, ApplicationContainer> Applications { get; set; }
public DefaultSettings()
{
Applications = new Dictionary<TargetApplication, ApplicationContainer>();
}
}
public partial class ApplicationContainer
{
[JsonProperty("environments")]
public Dictionary<DeploymentEnvironment, EnvironmentContainer> Environments { get; set; }
public ApplicationContainer()
{
Environments = new Dictionary<DeploymentEnvironment, EnvironmentContainer>();
}
}
public partial class EnvironmentContainer
{
[JsonProperty("dbKeyTypes")]
public Dictionary<DbKeyType, string> DbKeyTypes { get; set; }
public EnvironmentContainer()
{
DbKeyTypes = new Dictionary<DbKeyType, string>();
}
}
我将对象序列化如下:
var json = JsonConvert.SerializeObject(ds, Formatting.Indented, new CustomDictionaryConverter());
如前所述,序列化有效,但我需要帮助编写ReadJson()
方法才能反序列化。