LocalizableStringToStringJsonConverter.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Reflection;
  3. using Newtonsoft.Json;
  4. namespace Abp.Localization
  5. {
  6. /// <summary>
  7. /// This class can be used to serialize <see cref="ILocalizableString"/> to <see cref="string"/> during serialization.
  8. /// It does not work for deserialization.
  9. /// </summary>
  10. public class LocalizableStringToStringJsonConverter : JsonConverter
  11. {
  12. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  13. {
  14. if (value == null)
  15. {
  16. writer.WriteNull();
  17. return;
  18. }
  19. var localizableString = (ILocalizableString) value;
  20. writer.WriteValue(localizableString.Localize(new LocalizationContext(LocalizationHelper.Manager)));
  21. }
  22. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  23. {
  24. throw new NotImplementedException();
  25. }
  26. public override bool CanConvert(Type objectType)
  27. {
  28. return typeof (ILocalizableString).GetTypeInfo().IsAssignableFrom(objectType);
  29. }
  30. }
  31. }