fix actor serialization

This commit is contained in:
Nicolas Constant
2020-06-10 20:40:25 -04:00
parent 4875e71340
commit cf2c8a38d4
3 changed files with 52 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Text.Json.Serialization;
using System.Collections.Generic;
using Newtonsoft.Json;
using JsonConverter = Newtonsoft.Json.JsonConverter;
namespace BirdsiteLive.ActivityPub
{
@@ -8,7 +9,8 @@ namespace BirdsiteLive.ActivityPub
{
//[JsonPropertyName("@context")]
[JsonProperty("@context")]
public object[] context { get; set; } = new[] {"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1" };
[JsonConverter(typeof(ContextArrayConverter))]
public string[] context { get; set; } = new[] { "https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1" };
public string id { get; set; }
public string type { get; set; }
public string preferredUsername { get; set; }
@@ -20,4 +22,37 @@ namespace BirdsiteLive.ActivityPub
public Image icon { get; set; }
public Image image { get; set; }
}
public class ContextArrayConverter : JsonConverter
{
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var result = new List<string>();
var list = serializer.Deserialize<List<object>>(reader);
foreach (var l in list)
{
if (l is string s)
result.Add(s);
else
{
var str = JsonConvert.SerializeObject(l);
result.Add(str);
}
}
return result.ToArray();
}
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
}
}