Need to serialize and deserialize C# dictionaries into a JSON array. I would like to also read the JSON from powershell using array index notation.
By default, JSON format is:
{
"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"
}
}
}
}
}
}
}
This works great using the default json reader in .Net Core, but it doesn't allow me to use array index notation in PowerShell.
Instead what I'm looking for is this:
{
"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"
}
]
}
}
]
}
}
]
}
}
I'm using the WriteJson part from Serializing Dictionary<string,string> to array of "name": "value"
This works well; however, of course since the ReadJson()
method isn't implemented, it doesn't read. Btw, to get the above desired json format, I modified the CustomDictionaryConverter in the link to:
writer.WritePropertyName(key.ToString());
//writer.WriteValue(key);
//writer.WritePropertyName("value");
serializer.Serialize(writer, valueEnumerator.Current);
The classes behind the implementation are:
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>();
}
}
I'm serializing the object as follows:
var json = JsonConvert.SerializeObject(ds, Formatting.Indented, new CustomDictionaryConverter());
As mentioned, serializing works, but I need help writing the ReadJson()
method in order to be able to deserialize.