相关疑难解决方法(0)

使用 JsonConverter 在 C# 中自定义 JSON 反序列化

我在 .Net Core 中有两个课程

班上Ownership

namespace CustomStoreDatabase.Models
{
    public class Ownership
    {
        public string OwnershipId { get; set; }
        public List<string> TextOutput { get; set; }
        public DateTime DateTime { get; set; }
        public TimeSpan MeanInterval { get; set; }// Like long ticks, TimeSpan.FromTicks(Int64), TimeSpan.Ticks
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要使用方法和来显示MeanInterval长刻度。TimeSpan.FromTicks(Int64)TimeSpan.Ticks

我的自定义 JsonConverter

using CustomStoreDatabase.Models;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace CustomStoreDatabase.Util
{
    public class OwnershipJSonConverter : JsonConverter<Ownership>
    {
        public override bool CanConvert(Type typeToConvert)
        {
            return true; …
Run Code Online (Sandbox Code Playgroud)

c# deserialization jsonconverter system.text.json

4
推荐指数
1
解决办法
5866
查看次数

使用 System.Text.Json 序列化实现接口的对象

我有一个包含通用集合的大师班。集合中的元素有不同的类型,每个元素都实现了一个接口。

硕士课:

public class MasterClass
{
    public ICollection<IElement> ElementCollection { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

元素的契约:

public interface IElement
{
    string Key { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

元素的两个样本:

public class ElementA : IElement
{
    public string Key { get; set; }

    public string AValue { get; set; }
}

public class ElementB : IElement
{
    public string Key { get; set; }

    public string BValue { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我需要MasterClass使用System.Text.JsonJson 中的新库序列化对象实例。使用以下代码,

public string Serialize(MasterClass masterClass)
{ …
Run Code Online (Sandbox Code Playgroud)

json .net-core-3.0 system.text.json

3
推荐指数
3
解决办法
2664
查看次数

System.Text.Json转换器获取整个字符串对象

所以我遇到的情况是我的 NewtonJson 自定义转换器无法与 body api 调用一起使用。(默认使用 System.Text.Json 进行转换)。

所以目前我有一个临时解决方案来编写一些包装器,这些包装器最终将调用 Newtonjson,直到编写并测试 text.json 转换器。

我想做的是将整个对象作为字符串读取并将其传递给牛顿转换器

我的 StartUp.cs

 services.AddControllers(options =>
        options.Filters.Add<ApiExceptionFilterAttribute>())
            .AddFluentValidation(x => x.AutomaticValidationEnabled = false)//ValidationBehaviour handle fluent validations.
            .AddJsonOptions(x =>
            {
                x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
                x.JsonSerializerOptions.Converters.Add(new SystemModelConverter());
                x.JsonSerializerOptions.IgnoreNullValues = true;
            });
Run Code Online (Sandbox Code Playgroud)

我的转换器类

 public class SystemModelConverter : JsonConverter<ISystemModel>
{
    public override bool CanConvert(Type typeToConvert)
    {
        return (typeToConvert.GetInterface(typeof(ISystemModel).Name) != null);
    }

    public override ISystemModel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        var value = new StringBuilder();
        while(reader.Read())
        {
            var str = reader.GetString();
            value.Append(str);
        }
        //passing …
Run Code Online (Sandbox Code Playgroud)

.net c# json system.text.json

2
推荐指数
1
解决办法
2025
查看次数

如何序列化 System.Text.Json 中的抽象类

我有以下课程

public abstract class Settings
    {
        private string _filename;
        
        protected virtual void defaults()
        {
            
        }

        public static T Load<T>(string filename) where T : Settings, new()
        {
            T theSetting;
            if (File.Exists(filename))
            {
                var reader = new StreamReader(filename);
                var configJson = reader.ReadToEnd();
                reader.Close();
                theSetting = System.Text.Json.JsonSerializer.Deserialize<T>(configJson);
            }
            else
            {
                theSetting = new T();
                theSetting.defaults();
            }

            theSetting._filename = filename;
            theSetting.Save();

            return theSetting;
        }

        public void Save()
        {
            var writer = new StreamWriter(_filename);
            writer.Write(JsonSerializer.Serialize(this));
            writer.Close();
        }

        public void SaveAs(string filename)
        {
            _filename = filename; …
Run Code Online (Sandbox Code Playgroud)

c# json .net-core system.text.json

-1
推荐指数
1
解决办法
1476
查看次数