Created
June 16, 2017 16:51
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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