ParameterConverter.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* ==============================================================================
  2. * 功能描述:ParameterConverter
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/6/26 10:14:23
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using JBIM.Component;
  12. using Newtonsoft.Json;
  13. using Newtonsoft.Json.Linq;
  14. namespace RevitToJBim.JsonConverter
  15. {
  16. /// <summary>
  17. /// ParameterDefinationConverter
  18. /// </summary>
  19. class ParameterConverter : Newtonsoft.Json.JsonConverter
  20. {
  21. public override bool CanConvert(Type objectType)
  22. {
  23. return objectType == typeof(Parameter);
  24. }
  25. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  26. {
  27. throw new NotImplementedException();
  28. }
  29. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  30. {
  31. if (value == null)
  32. {
  33. writer.WriteNull();
  34. return;
  35. }
  36. JObject jObject = new JObject();
  37. Parameter parameter = null;
  38. try
  39. {
  40. parameter = value as Parameter;
  41. if (parameter == null) return;
  42. jObject.Add("Name", parameter?.Definition?.Name);
  43. jObject.Add("Value", (parameter?.Value)??"");
  44. writer.WriteToken(jObject.CreateReader());
  45. }
  46. catch (Exception e)
  47. {
  48. Console.WriteLine(e);
  49. }
  50. }
  51. }
  52. }