我在 .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) 我有一个包含通用集合的大师班。集合中的元素有不同的类型,每个元素都实现了一个接口。
硕士课:
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) 所以我遇到的情况是我的 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) 我有以下课程
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)