fix actor serialization

This commit is contained in:
Nicolas Constant 2020-06-10 20:40:25 -04:00
parent 4875e71340
commit cf2c8a38d4
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
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();
}
}
}

View File

@ -73,7 +73,7 @@ namespace BirdsiteLive.Controllers
private Dictionary<string, string> RequestHeaders(IHeaderDictionary header)
{
return header.ToDictionary<KeyValuePair<string, StringValues>, string, string>(h => h.Key, h => h.Value);
return header.ToDictionary<KeyValuePair<string, StringValues>, string, string>(h => h.Key.ToLowerInvariant(), h => h.Value);
}
}
}

View File

@ -16,6 +16,20 @@ namespace BirdsiteLive.ActivityPub.Tests
var actor = JsonConvert.DeserializeObject<Actor>(json);
}
[TestMethod]
public void Serialize()
{
var obj = new Actor
{
type = "Person",
id = "id"
};
var json = JsonConvert.SerializeObject(obj);
}
}
}