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

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace IPA.JsonConverters
{
internal class MultilineStringConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.StartArray)
{
var list = JsonSerializer.Deserialize<string[]>(ref reader, options);
return string.Join("\n", list);
}
return reader.GetString();
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
var list = value.Split('\n');
if (list.Length == 1)
writer.WriteStringValue(value);
else
JsonSerializer.Serialize(writer, list, options);
}
}
}