我试图为具有嵌套对象的对象创建自定义json转换器,以作为正文发送到Firestore REST API:
public class Channel
{
public string Name { get; private set; }
public long OwnerId { get; private set; }
public IEnumerable<Message> Messages { get; set; }
public IEnumerable<User> Users { get; set; }
}
public class User
{
public string UserId{ get; private set; }
public string Pseudonym { get; private set; }
}
public class Message
{
public long SenderUserId { get; private set; }
public string text { get; private set; }
public DateTime SentDate { get; private set; }
}
序列化时,json结构应如下所示:
{
"OwnerId": {
"stringValue": "id-1"
},
"Name": {
"stringValue": "New Channel"
},
"Messages": {
"arrayValue": {
"values": [
{
"mapValue": {
"fields": {
"ConversationMessage": {
"stringValue": "Hello kev"
},
"SenderUserId": {
"stringValue": "id-1"
}
}
}
}
]
}
},
"Users": {
"arrayValue": {
"values": [
{
"mapValue": {
"fields": {
"UserId": {
"stringValue": "id-1"
},
"Pseudonym": {
"stringValue": "raven"
}
}
}
},
{
"mapValue": {
"fields": {
"UserId": {
"stringValue": "id-2"
},
"Pseudonym": {
"stringValue": "kev"
}
}
}
}
]
}
}
}
Firestore强制执行此结构,属性变为对象,类型为键,值为值(字符串变为stringValue,IEnumerable变为带有值的arrayValue,对象变为带字段的mapValue ...)
我应该为每个对象创建一个转换器,还是只为Channel对象创建一个转换器?任何答案或帮助将非常感谢,谢谢!