[转]Newtonsoft JSON how to dynamically change the date format?
I'm using Newtonsoft JSON Serializer and it's great and super fast but I'd like to do a bit more with it. I'm not sure it's possible as all the search I've done comes up to nothing. What I would like is to be able to truncate empty time, so when it's display 2014-01-01 00:00:00.000
I just want 2014-01-01
at the end, so basically cut the entire time when they're all zeros. For now I use this piece of code:
DataTable dt = loadData();
// encode the string with Newton JSON.Net
string output = JsonConvert.SerializeObject(dt,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Newtonsoft.Json.Formatting.None,
DateFormatString = "yyyy-MM-dd HH:mm:ss"
});
Is there a way to format these dates without the time (only when they are all zeros) without affecting the performance?
Best How To :
You can do this with a custom JsonConverter:
class CustomDateConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(DateTime));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
DateTime date = (DateTime)value;
string format = "yyyy-MM-dd HH:mm:ss";
if (date.Hour == 0 &&
date.Minute == 0 &&
date.Second == 0 &&
date.Millisecond == 0)
{
format = "yyyy-MM-dd";
}
writer.WriteValue(date.ToString(format));
}
public override bool CanRead
{
get { return false; }
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Demo:
class Program
{
static void Main(string[] args)
{
List<DateTime> dates = new List<DateTime>
{
DateTime.Now,
DateTime.Today
};
JsonSerializerSettings settings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Newtonsoft.Json.Formatting.None,
Converters = new List<JsonConverter> { new CustomDateConverter() }
};
string json = JsonConvert.SerializeObject(dates, settings);
Console.WriteLine(json);
}
}
Output:
["2014-06-11 11:56:28","2014-06-11"]
[转]Newtonsoft JSON how to dynamically change the date format?的更多相关文章
- 调用newtonsoft.json反序列出错
调用newtonsoft.json反序列出错: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current J ...
- 找不到方法 Void Newtonsoft.Json.JsonConvert.set_DefaultSettings
找不到方法 Void Newtonsoft.Json.JsonConvert.set_DefaultSettings 因为 Newtonsoft.Json.dll 的版本号问题: C:\Program ...
- Newtonsoft.Json 序列化
当我们对一个json数组进行反序列化用Newtonsoft.Json.JsonConvert.DeserializeObject<T>() 通常会报此错误 Newtonsoft.Json. ...
- .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程
JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...
- 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)
在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...
- Newtonsoft.Json 自定义 解析协议
在开发web api的时候 遇到一个要把string未赋值默认为null的情况改成默认为空字符串的需求 这种情况就需要自定义json序列话的 解析协议了 Newtonsoft.Json默认的解析协议是 ...
- Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6
未能加载文件或程序集“Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6”或它的某一个 ...
- Newtonsoft.Json 序列化和反序列化 时间格式【转】
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- Newtonsoft.Json 版本冲突解决
在做asp.net MVC 开发时,因为引用的dll 中使用了更高版本的 Newtonsoft.Json ,导致运行时发生错误, 查资料说是因为webApi使用了Newtonsoft.Json 导致了 ...
随机推荐
- 微软 eshop 数据存储之sqlserver
微软的eshop项目写的很牛,学起来也比较吃力,最近公司刚好有一本书,说的就是.NET微服务,记下来. 因为微软对性能的要求,docker里面要有内存要求 安装dokcer,拉镜象 : docker ...
- Windows上编译Boost
Boost做得很好,有自己的build系统,可以几乎一键式编译,这才是尼玛世界一流质量的良心开源库啊. 将Boost 1.49.0解压到boost/boost_1_49_0里面,然后在boost目录底 ...
- struts中如何查看配置文件中是否存在某个返回值
ActionConfig config = ActionContext.getContext() .getActionInvocation().getProxy().getConfig(); Resu ...
- selenium自动加载Flash
当我们在定位的时候,有时候会碰到Flash问题导致无法定位到元素 通过下面的代码就能解决问题 参考:https://blog.csdn.net/qq_37913997/article/details/ ...
- [jvm]运行时数据区域详解
了解虚拟机是怎么使用内存的,有助于我们解决和排查内存泄漏和溢出方面的问题.详解java虚拟机内存的各个区域,分析这些区域的作用服务对象以及可能发生的问题. 一.运行时数据区域 java虚拟机在执行ja ...
- 程序设计中的dry原则
DRY:dont repeat yourself 假设一个逻辑(代码块)会重复两次或者以上,应该写成函数被调用 为什么呢,实际上,我们处处可见重复性的代码.这除了增加工作量之外,还会增加维护难度. d ...
- Saiku2.6 配置数据源
把连接字符串和Schema文件放这里.
- 从零开始完整搭建 Spring-Boot 项目开发框架的教程
前言 难度:简单 类型:step-by-step 适用:初学者,完全没有接触过 Spring-Boot 开发环境:jdk 1.8 关键词:java, sring-boot, spring-mvc, r ...
- 2019.4.3 HTML&CSS 理论部分
HTML 块标签 1.独占一行,不和其他标签待在一行; 2.能设置宽高 常见的块标签:h1-h6,p,div,table,hr,br,ul,ol, 行标签 1.可以和其他行标签待在一行 2.不能设置宽 ...
- vue-cli项目启动遇到的坑
利用 npm init webpack projectname 之后 切换到项目所在文件夹下,执行命令 npm install ,一直非常慢,卡在那里基本不动. 最后是利用cnpm 安装成功的. 转载 ...