You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
921 B

  1. using System;
  2. using System.Text.Json;
  3. using System.Text.Json.Serialization;
  4. namespace IPA.JsonConverters
  5. {
  6. internal class MultilineStringConverter : JsonConverter<string>
  7. {
  8. public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  9. {
  10. if (reader.TokenType == JsonTokenType.StartArray)
  11. {
  12. var list = JsonSerializer.Deserialize<string[]>(ref reader, options);
  13. return string.Join("\n", list);
  14. }
  15. return reader.GetString();
  16. }
  17. public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
  18. {
  19. var list = value.Split('\n');
  20. if (list.Length == 1)
  21. writer.WriteStringValue(value);
  22. else
  23. JsonSerializer.Serialize(writer, list, options);
  24. }
  25. }
  26. }