|
| 1 | +using System; |
| 2 | + |
| 3 | +using dosymep.SimpleServices; |
| 4 | + |
| 5 | +using pyRevitLabs.Json; |
| 6 | +using pyRevitLabs.Json.Linq; |
| 7 | + |
| 8 | +namespace RevitPackageDocumentation.Models.ConfigSerializer; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Конвертер для полиморфной десериализации компонентов листа |
| 12 | +/// </summary> |
| 13 | +public class SheetComponentConverter : JsonConverter { |
| 14 | + private readonly ILocalizationService _localizationService; |
| 15 | + |
| 16 | + private const string _componentTypeProperty = "ComponentType"; |
| 17 | + private const string _structuralPlanViewType = "PlanView"; |
| 18 | + private const string _structuralCalloutViewType = "CalloutView"; |
| 19 | + private const string _sectionViewType = "SectionView"; |
| 20 | + private const string _scheduleViewType = "ScheduleView"; |
| 21 | + private const string _textNoteType = "TextNote"; |
| 22 | + private const string _typicalAnnotationType = "TypicalAnnotation"; |
| 23 | + private const string _legendViewType = "LegendView"; |
| 24 | + |
| 25 | + public SheetComponentConverter(ILocalizationService localizationService) { |
| 26 | + _localizationService = localizationService; |
| 27 | + } |
| 28 | + |
| 29 | + public override bool CanConvert(Type objectType) { |
| 30 | + return objectType == typeof(SheetComponentData); |
| 31 | + } |
| 32 | + |
| 33 | + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { |
| 34 | + var jObject = JObject.Load(reader); |
| 35 | + var componentType = jObject[_componentTypeProperty]?.Value<string>(); |
| 36 | + |
| 37 | + if(string.IsNullOrEmpty(componentType)) |
| 38 | + throw new JsonSerializationException( |
| 39 | + $"{_localizationService.GetLocalizedString("MainViewModel.Property")} '{_componentTypeProperty}' " + |
| 40 | + $"{_localizationService.GetLocalizedString("MainViewModel.NotFoundInJSON")}"); |
| 41 | + |
| 42 | + try { |
| 43 | + return componentType switch { |
| 44 | + _structuralPlanViewType => jObject.ToObject<PlanViewData>(serializer), |
| 45 | + _structuralCalloutViewType => jObject.ToObject<CalloutViewData>(serializer), |
| 46 | + _sectionViewType => jObject.ToObject<SectionViewData>(serializer), |
| 47 | + _scheduleViewType => jObject.ToObject<ScheduleViewData>(serializer), |
| 48 | + _textNoteType => jObject.ToObject<TextNoteData>(serializer), |
| 49 | + _typicalAnnotationType => jObject.ToObject<TypicalAnnotationData>(serializer), |
| 50 | + _legendViewType => jObject.ToObject<LegendViewData>(serializer), |
| 51 | + _ => throw new NotSupportedException( |
| 52 | + $"{_localizationService.GetLocalizedString("MainViewModel.UnknownComponentType")}: {componentType}") |
| 53 | + }; |
| 54 | + } catch(Exception ex) { |
| 55 | + throw new JsonSerializationException( |
| 56 | + $"{_localizationService.GetLocalizedString("MainViewModel.ErrorDeserializingType")} '{componentType}'", ex); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { |
| 61 | + serializer.Serialize(writer, value); |
| 62 | + } |
| 63 | + |
| 64 | + public override bool CanWrite => true; |
| 65 | +} |
0 commit comments