[转]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 导致了 ...
随机推荐
- python学习之路 二 :基本数据类型
本节重点 理解什么是变量? 掌握各种数据类型 理解可变类型和不可变类型 一.变量和常量 变量: 作用:存贮程序的中间结果在内存里,以备后边的程序调用 定义规范: 变量名只能是 字母.数字活下划线的任意 ...
- 2018-12-20 第一章初识JAVA-上机
JAVA初体验之课后练习 一.请写出Java 程序执行过程与编译原理 答:①执行过程:创建.java文件——编译器(JDK环境里的javac.exe文件编译)——输出.class供计算机接受(只有cl ...
- NOI2014起床困难综合症
Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争.通过研究相关文献,他找 ...
- 小记一次shellscript的麻烦
小记一次shellscript的麻烦 一.起因: 之前写过篇文章 文本分析实例 ,大致的内容就是对 "nginx的web服务器进行日志分析,删除不被访问的截图". 点我打开哔哩哔哩 ...
- Windows下磁盘无损重新分配
打开百度(www.baidu.com),找到我们的分区助手.exe 单击“普通下载”,会下载一个安装包. 这时候,我们进行安装.安装完成如下. 对着那个盘容量较大的盘右键,分配空闲空间. 服务器多次重 ...
- NASA的10条代码编写原则
NASA的10条代码编写原则 作者: Gerard J. Holzmann 来源: InfoQ 原文链接 英文原文:NASA's 10 Coding Rules for Writing Safety ...
- 【Leetcode】Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 1016 部分A+B (15 分)
// 题目不难,感觉变量定义的有点儿多了#include <iostream> #include <cmath> using namespace std; int main() ...
- [Alpha]Scrum Meeting#2
github 本次会议项目由PM召开,时间为4月2日晚上10点30分 时长25分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写内置问卷(issue#3) 撰写团队贡献分配计划(issue#39) ...
- C# 进程通信SendMessage和有关消息参数
SendMessage是啥? 函数原型: LRESULT SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam)SendMessage( ...