Skip to content

Instantly share code, notes, and snippets.

@ryansalt
Created June 16, 2017 16:51
Show Gist options
  • Select an option

  • Save ryansalt/d2c0ac1e2f700c57810fb3eaf11cfa0d to your computer and use it in GitHub Desktop.

Select an option

Save ryansalt/d2c0ac1e2f700c57810fb3eaf11cfa0d to your computer and use it in GitHub Desktop.
Custom JsonConverter to convert a list of connectionstrings in format <name, connectionstring> and format for output in appsettings.json.
public class ConnectionStringsJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true; //TODO: more logic check the type before the conversion..
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var rows = (List<KeyValuePair<string, string>>)value;
writer.WriteStartObject();
writer.WritePropertyName("Data");
writer.WriteStartObject();
foreach (var row in rows)
{
//writer.WriteStartObject();
writer.WritePropertyName(row.Key);
writer.WriteStartObject();
writer.WritePropertyName("ConnectionString");
writer.WriteValue(row.Value);
writer.WriteEndObject();
}
writer.WriteEndObject();
writer.WriteEndObject();
writer.Flush();
}
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment