Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. // Use like this:
  2. // var hostBuilder = new HostBuilder()
  3. // .ConfigureHostConfiguration(cfg => {
  4. // cfg.Add(new JObjectConfigurationSource(new JObject()));
  5. // })
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Linq;
  11. using Microsoft.Extensions.Configuration;
  12. using Newtonsoft.Json.Linq;
  13.  
  14. namespace MyNamespace
  15. {
  16. public class JObjectConfigurationSource : ConfigurationProvider, IConfigurationSource
  17. {
  18. private readonly JObject _jObject;
  19. private readonly IDictionary<string, string> _data = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  20. private readonly Stack<string> _context = new Stack<string>();
  21. private string _currentPath;
  22.  
  23. public JObjectConfigurationSource(JObject jObject) => _jObject = jObject;
  24.  
  25. public IConfigurationProvider Build(IConfigurationBuilder builder) => this;
  26.  
  27. public override void Load()
  28. {
  29. Data = ParseStream(_jObject);
  30. }
  31.  
  32. private IDictionary<string, string> ParseStream(JObject jObject)
  33. {
  34. _data.Clear();
  35. VisitJObject(jObject);
  36. return _data;
  37. }
  38.  
  39. public void VisitJObject(JObject jObject)
  40. {
  41. foreach (var property in jObject.Properties())
  42. {
  43. EnterContext(property.Name);
  44. VisitProperty(property);
  45. ExitContext();
  46. }
  47. }
  48.  
  49. private void VisitProperty(JProperty property)
  50. {
  51. VisitToken(property.Value);
  52. }
  53.  
  54. private void VisitToken(JToken token)
  55. {
  56. switch (token.Type)
  57. {
  58. case JTokenType.Object:
  59. VisitJObject(token.Value<JObject>());
  60. break;
  61. case JTokenType.Array:
  62. VisitArray(token.Value<JArray>());
  63. break;
  64. case JTokenType.Integer:
  65. case JTokenType.Float:
  66. case JTokenType.String:
  67. case JTokenType.Boolean:
  68. case JTokenType.Null:
  69. case JTokenType.Raw:
  70. case JTokenType.Bytes:
  71. VisitPrimitive(token.Value<JValue>());
  72. break;
  73. default:
  74. throw new FormatException();
  75. }
  76. }
  77.  
  78. private void VisitArray(JArray array)
  79. {
  80. for (int index = 0; index < array.Count; ++index)
  81. {
  82. EnterContext(index.ToString());
  83. VisitToken(array[index]);
  84. ExitContext();
  85. }
  86. }
  87.  
  88. private void VisitPrimitive(JValue data)
  89. {
  90. string currentPath = _currentPath;
  91. if (_data.ContainsKey(currentPath))
  92. {
  93. throw new FormatException();
  94. }
  95.  
  96. _data[currentPath] = data.ToString(CultureInfo.InvariantCulture);
  97. }
  98.  
  99. private void EnterContext(string context)
  100. {
  101. _context.Push(context);
  102. _currentPath = ConfigurationPath.Combine(_context.Reverse());
  103. }
  104.  
  105. private void ExitContext()
  106. {
  107. _context.Pop();
  108. _currentPath = ConfigurationPath.Combine(_context.Reverse());
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement