| | 1 | | using Newtonsoft.Json.Linq; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | 3 | | using ValidateLib.Metadata.Descriptors; |
| | 4 | | using ValidateLib.Metadata.Descriptors.Interfaces; |
| | 5 | |
|
| | 6 | | namespace ValidateLib.Metadata.Parsers |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Class that is defaul parser for descriptors. |
| | 10 | | /// </summary> |
| | 11 | | /// <typeparam name="T"> Derived descriptor we want to parse. </typeparam> |
| | 12 | | public abstract class DescriptorParserBase<T> : IParser<T> where T : DescriptorBase, Descriptors.Interfaces.IParsabl |
| | 13 | | { |
| 1 | 14 | | public T Descriptor { get; set; } = new T(); |
| 1 | 15 | | public List<Warning> Warnings { get; set; } |
| | 16 | |
|
| 1 | 17 | | public DescriptorParserBase(List<Warning> warnings, T? descriptor = null) |
| | 18 | | { |
| 1 | 19 | | if (descriptor != null) Descriptor = descriptor; |
| 1 | 20 | | Warnings = warnings; |
| 1 | 21 | | } |
| | 22 | | /// <summary> |
| | 23 | | /// Parser json value object into a derived descriptor. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="jToken"> json value containing the object</param> |
| | 26 | | /// <param name="propertyName"> name of the property containing this object, needed for error messages.</param> |
| | 27 | | /// <returns> Descriptor parsed. </returns> |
| | 28 | | public virtual T ParseJToken(JToken jToken, string propertyName) |
| | 29 | | { |
| 1 | 30 | | if (jToken.Type != JTokenType.Object) |
| | 31 | | { |
| 1 | 32 | | if (jToken.Type == JTokenType.String) |
| 0 | 33 | | Warnings.Add(WarningFactory.GetObjectStringNormalizationProblemWarning(jToken.ToString(), propertyNa |
| | 34 | | else |
| 1 | 35 | | Warnings.Add(WarningFactory.GetObjectPropertyWrongValueWarning(jToken, propertyName)); |
| 1 | 36 | | Descriptor.IsInvalid = true; |
| 1 | 37 | | return Descriptor; |
| | 38 | | } |
| | 39 | | else |
| | 40 | | { |
| 1 | 41 | | foreach (JProperty property in jToken) |
| | 42 | | { |
| 1 | 43 | | var propertyParser = Descriptor.GetPropertyParser(property, Warnings); |
| 1 | 44 | | if (propertyParser == null) |
| | 45 | | { |
| 1 | 46 | | Warnings.Add(WarningFactory.GetUnknownPropertyWarning(property)); |
| | 47 | | } |
| | 48 | | else |
| | 49 | | { |
| 1 | 50 | | propertyParser.ParseProperty(property); |
| | 51 | | } |
| | 52 | | } |
| 1 | 53 | | if (Descriptor is IRequiredPropertyValidatable) ((IRequiredPropertyValidatable)Descriptor).CheckRequired |
| 1 | 54 | | return Descriptor; |
| | 55 | | } |
| | 56 | | } |
| | 57 | | } |
| | 58 | | } |