ArangoDB 如何使用Newtonsoft Json将整型值转换为字符串

vhmi4jdf  于 2022-12-09  发布在  Go
关注(0)|答案(1)|浏览(141)

我刚刚开始检查如何使用json对需要使用ArangoDB的项目进行序列化和反序列化。
此刻,我有一个测试类AnoherTestPerson

public class AnotherTestPerson
    {
        public AnotherTestPerson(int id, string fullname, int age)
        {
            this.Id = id;
            this.Fullname = fullname;
            this.Age = age;
        }

        public int Id { get; set; }
        public string Fullname { get; set; }
        public int Age { get; set; }
    }

现在,我需要将Id值转换为字符串,因为当您将数值作为_key传递时,ArangoDB不起作用,所以我猜我必须从Arango驱动程序使用的序列化器中执行此操作,因为在我将要处理的项目中,我们将无法访问要存储在数据库中的实体的类。
任何帮助都将不胜感激,因为我仍在学习序列化如何与Json和C#一起工作。
下面是代码的其余部分:

public static async Task Main(string[] args)
    {

        string connectionString = "private";

        var arango = new ArangoContext(cs:connectionString, settings:
            new ArangoConfiguration
            {
                Serializer = new ArangoNewtonsoftSerializer(CustomDataContractResolver.Instance)
                //Using custom contract resolver for automatically changing the Id name
                //from the object class to _key in the Json file
            }
        );
        await arango.Document.CreateAsync("TestDB", typeof(AnotherTestPerson).Name, testPerson);
    }

这是自定义协定解析程序。我尝试在此处更改属性的类型,但没有成功。

public class CustomDataContractResolver : DefaultContractResolver
{
    public static readonly CustomDataContractResolver Instance = new CustomDataContractResolver();

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        if (property.PropertyName.Equals("Id", StringComparison.OrdinalIgnoreCase))
        {
            property.PropertyName = "_key";
            if(property.PropertyType == Type.GetType("System.Int32"))
            {
                property.PropertyType = Type.GetType("System.String");
            }
        }
        return property;
    }
}

编辑

因此,查看SBFrancies的注解,我实现了一个基本的JsonConverter:

public class ToStringJsonConverted : Newtonsoft.Json.JsonConverter
{
    public static readonly ToStringJsonConverted Instance = new ToStringJsonConverted();

    public override bool CanConvert(Type objectType)
    {
        return true;
    }

    public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString());
    }
}

并将其链接到自定义ContractResolver:

public class CustomDataContractResolver : DefaultContractResolver
{
    public static readonly CustomDataContractResolver Instance = new CustomDataContractResolver();

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        if (property.PropertyName.Equals("Id", StringComparison.OrdinalIgnoreCase))
        {
            property.PropertyName = "_key";
            if(property.PropertyType == Type.GetType("System.Int32"))
            {
                property.Converter = ToStringJsonConverted.Instance;
            }
        }
        return property;
    }
}

它得到了我想要的序列化,但是反序列化现在不起作用。我将检查如何读取Json文件并解析它们。

xn1cxnb4

xn1cxnb41#

在注解中的@SBFrancies引用的帮助下,我实现了序列化和反序列化。
契约解析程式:

public class CustomDataContractResolver : DefaultContractResolver
{
    public static readonly CustomDataContractResolver Instance = new CustomDataContractResolver();

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        if (property.PropertyName.Equals("Id", StringComparison.OrdinalIgnoreCase))
        {
            property.PropertyName = "_key";
            if(property.PropertyType == Type.GetType("System.Int32"))
            {
                property.Converter = ToStringJsonConverted.Instance;
            }
        }
        return property;
    }
}

JSON转换器:

public class ToStringJsonConverted : Newtonsoft.Json.JsonConverter
{
    public static readonly ToStringJsonConverted Instance = new ToStringJsonConverted();

    public override bool CanConvert(Type objectType)
    {
        return true;
    }

    public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
    {
        return Int32.Parse((string)reader.Value);
    }

    public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString());
    }
}

相关问题