[英].NET Core 3 System.Text.Json nested object serialization
[英]System.Text.Json problem with large list serialization
我想從我的項目中刪除 Newtonsoft 並開始使用默認的 System.Text.Json。 現在我有一個非常大的要序列化的對象列表,但這在 System.Text.Json 中不起作用,但它在 Newtonsoft 中起作用。
問題是 JSON 在 +- 58000 個字符后被“截斷”。 所以 JSON 不再有效。
這有效:
return Ok(JsonConvert.SerializeObject(result, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}));
我嘗試更改 web.config 中的最大設置,但這並沒有解決問題。 還有其他我可以實施的設置嗎?
提前致謝!
翻轉
我沒有重現問題,我自己編了一個model,里面有一個字符串變量,字符串的長度是9999999,就可以轉換成功。
然后我掃描JsonSerializerSettings
定義並找到下面的方法,該方法將拋出似乎與您的問題相關的ArgumentException
。 所以恐怕你可以試試 set MaxDepth
。
/// <summary>
/// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
/// A null value means there is no maximum.
/// The default value is <c>64</c>.
/// </summary>
public int? MaxDepth
{
get => _maxDepthSet ? _maxDepth : DefaultMaxDepth;
set
{
if (value <= 0)
{
throw new ArgumentException("Value must be positive.", nameof(value));
}
_maxDepth = value;
_maxDepthSet = true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.