Newtonsoft.Json與System.Text.Json區別 在 Newtonsoft.Json中可以使用例如 方式設置接收/序列化時間格式,但在.net core 3.1中System.Text.Json是沒有自帶方式進行轉換,這就需要自定義Converter實現時間轉換 "官方GitHu ...
Newtonsoft.Json與System.Text.Json區別
在 Newtonsoft.Json中可以使用例如
.AddJsonOptions(options =>
{
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
})
方式設置接收/序列化時間格式,但在.net core 3.1中System.Text.Json是沒有自帶方式進行轉換,這就需要自定義Converter實現時間轉換
官方GitHub地址
自定義DateTimeJsonConverter
public class DateTimeJsonConverter : JsonConverter<DateTime>
{
private readonly string _dateFormatString;
public DateTimeJsonConverter()
{
_dateFormatString = "yyyy-MM-dd HH:mm:ss";
}
public DateTimeJsonConverter(string dateFormatString)
{
_dateFormatString = dateFormatString;
}
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToUniversalTime().ToString(_dateFormatString));
}
}
JsonTokenType枚舉類型共有以下幾種
/// <summary>Defines the various JSON tokens that make up a JSON text.</summary>
public enum JsonTokenType : byte
{
/// <summary>There is no value (as distinct from <see cref="F:System.Text.Json.JsonTokenType.Null" />).</summary>
None,
/// <summary>The token type is the start of a JSON object.</summary>
StartObject,
/// <summary>The token type is the end of a JSON object.</summary>
EndObject,
/// <summary>The token type is the start of a JSON array.</summary>
StartArray,
/// <summary>The token type is the end of a JSON array.</summary>
EndArray,
/// <summary>The token type is a JSON property name.</summary>
PropertyName,
/// <summary>The token type is a comment string.</summary>
Comment,
/// <summary>The token type is a JSON string.</summary>
String,
/// <summary>The token type is a JSON number.</summary>
Number,
/// <summary>The token type is the JSON literal true.</summary>
True,
/// <summary>The token type is the JSON literal false.</summary>
False,
/// <summary>The token type is the JSON literal null.</summary>
Null,
}
services.AddMvc 中 Newtonsoft.Json與System.Text.Json 配置區別
.netcore 2.1
services
//全局配置Json序列化處理
.AddJsonOptions(options =>
{
//忽略迴圈引用
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
//不使用駝峰樣式的key
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
//設置時間格式
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
})
.netcore 3.1
services.AddControllers()
.AddJsonOptions(options =>
{
//設置時間格式
options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter("yyyy-MM-dd HH:mm:ss"));
//設置bool獲取格式
options.JsonSerializerOptions.Converters.Add(new BoolJsonConverter());
//不使用駝峰樣式的key
options.JsonSerializerOptions.PropertyNamingPolicy = null;
//不使用駝峰樣式的key
options.JsonSerializerOptions.DictionaryKeyPolicy = null;
//獲取或設置要在轉義字元串時使用的編碼器
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
});
bool型轉換問題
.netcore 3.1中接收的json中不能將 "true"/"false"
識別為boolean的True/False,這也需要自定義Converter實現bool轉換
namespace Kdniao.Core.Utility
{
public class BoolJsonConverter : JsonConverter<bool>
{
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.True || reader.TokenType == JsonTokenType.False)
return reader.GetBoolean();
return bool.Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
{
writer.WriteBooleanValue(value);
}
}
}