14 Star 54 Fork 31

NtripShare/NtripShareBase

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
SerializationHelper.cs 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
NtripShare 提交于 4年前 . 初始版本
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace WatsonTcp
{
internal static class SerializationHelper
{
private static readonly JsonSerializerSettings HardenedSerializerSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.None // Prevents CS2328 style attacks if a project is allowing automatic type resolution elsewhere.
};
internal static T DeserializeJson<T>(string json)
{
if (String.IsNullOrEmpty(json)) throw new ArgumentNullException(nameof(json));
return JsonConvert.DeserializeObject<T>(json, HardenedSerializerSettings);
}
private static readonly JsonSerializerSettings SerializerDefaults = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DateTimeZoneHandling = DateTimeZoneHandling.Local,
};
internal static string SerializeJson(object obj, bool pretty)
{
if (obj == null) return null;
string json;
if (pretty)
{
json = JsonConvert.SerializeObject(
obj,
Newtonsoft.Json.Formatting.Indented,
SerializerDefaults
);
}
else
{
json = JsonConvert.SerializeObject(
obj,
SerializerDefaults
);
}
return json;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/ntripshare/ntrip-share-base.git
git@gitee.com:ntripshare/ntrip-share-base.git
ntripshare
ntrip-share-base
NtripShareBase
master

搜索帮助