Marten JasperFx - 如何在生成文档时忽略 class 的 属性

Marten JasperFx - How to ignore property of class when document is generated

Marten 将文档存储在数据库中时,可以忽略 class 属性 吗?

例如:

    public class Test
    {
        public int Id { get; set; }
        public string Name { get; set; }
        [Ignore this when create a document on DB]
        public Date DateOfBirth { get; set; }
    }

已解决

public class Test
{
   public int Id { get; set; }
   public string Name { get; set; }
   [IgnoreDataMember]
   public Date DateOfBirth { get; set; }
}

TL:DR

Marten 只是在后台使用 Newtonsoft.Json,因此要忽略 属性,请使用 Newtonsoft JsonIgnoreAttribute:

public class Account
{
    public string FullName { get; set; }
    public string EmailAddress { get; set; }

    [JsonIgnore]
    public string PasswordHash { get; set; }
}

说明

这似乎是在 google 搜索之上可见的问题,所以我想加上我的 50 美分。

因为 MartenDB 在内部使用 Newtonsoft.Json 那么这个库中的所有属性应该都可以正常工作。我不知道 Igor ,无法在图书馆的任何地方确认这一点,所以它现在似乎已经过时了。

来自docs:

An absolutely essential ingredient in Marten's persistence strategy is JSON serialization of the document objects. Marten aims to make the JSON serialization extensible and configurable through the native mechanisms in each JSON serialization library. For the purposes of having a smooth "getting started" story, Marten comes out of the box with support for a very basic usage of Newtonsoft.Json as the main JSON serializer.

关于 Newtonsoft.json MartenDB

MartenDB 与 Newtonsoft.json 无关,您始终可以为其他库编写自己的适配器。

你所要做的就是实现ISerializer接口:

public interface ISerializer
{
    void ToJson(object document, TextWriter writer);

    string ToJson(object document);

    T FromJson<T>(TextReader reader);

    object FromJson(Type type, TextReader reader);

    string ToCleanJson(object document);

    EnumStorage EnumStorage { get; }

    Casing Casing { get; }

    CollectionStorage CollectionStorage { get; }

    NonPublicMembersStorage NonPublicMembersStorage { get; }
}

更详细的示例:docs