json.net

DateTime与DateTimeOffset

冷暖自知 提交于 2019-12-28 20:06:45
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 目前,我们有一种以TimeZone感知方式处理.net DateTimes的标准方法:每当我们生成一个 DateTime 我们用UTC(例如使用 DateTime.UtcNow )来做,每当我们显示一个时,我们就会从UTC转换回用户的当地时间。 这工作正常,但我一直在阅读有关 DateTimeOffset 以及它如何捕获对象本身的本地和UTC时间。 所以问题是,使用 DateTimeOffset 与我们已经做的事情有什么好处? #1楼 来自微软: DateTimeOffset值的这些用法比DateTime值的用法更常见。 因此,应将DateTimeOffset视为应用程序开发的默认日期和时间类型。 来源: “在DateTime,DateTimeOffset,TimeSpan和TimeZoneInfo之间选择” , MSDN 我们使用 DateTimeOffset 几乎所有内容,因为我们的应用程序处理特定时间点(例如,创建/更新记录时)。 另外,我们也在SQL Server 2008中使用 DATETIMEOFFSET 。 当你想要只处理日期,只处理时间或者在一般意义上处理时,我认为 DateTime 是有用的。 例如,如果你有,你要每天都去送行上午7时报警,你可以存储在一个 DateTime 利用

Setting IgnoreSerializableAttribute Globally in Json.net

人盡茶涼 提交于 2019-12-28 14:41:08
问题 I'm working on a ASP.NET WebApi (Release Candidate) project where I'm consuming several DTOs that are marked with the [Serializable] attribute. These DTOs are outside of my control so I'm not able to modify them in any way. When I return any of these from a get method the resulting JSON contains a bunch of k__BackingFields like this: <Name>k__BackingField=Bobby <DateCreated>k__BackingField=2012-06-19T12:35:18.6762652-05:00 Based on the searching I've done this seems like a problem with JSON

Json.Net And ActionResult

冷暖自知 提交于 2019-12-28 12:14:33
问题 Im building a JObject myself and want to return it as ActionResult. I dont want to create and then serialize a data object For example public ActionResult Test(string id) { var res = new JObject(); JArray array = new JArray(); array.Add("Manual text"); array.Add(new DateTime(2000, 5, 23)); res["id"] = 1; res["result"] = array; return Json(res); //??????? } 回答1: You should just be able to do this in your action method: return Content( res.ToString(), "application/json" ); 回答2: In case, if you

Serialize one to many relationships in Json.net

你离开我真会死。 提交于 2019-12-28 11:45:11
问题 I am using the Entity Framework code first for data access and I have a Company class which has a collection of Employees. The Employee class also has a Company property. I would like to be able to serialize a Company and include the list of employees in the serialization. Here is Company: public class Company { public long Id { get; set; } public string Name { get; set; } public DateTime? Established { get; set; } public virtual IList<Employee> Employees { get; set; } public DateTime?

Converting a JToken (or string) to a given Type

吃可爱长大的小学妹 提交于 2019-12-28 11:42:39
问题 TL;DR Version I have a object of type JToken (but can also be a string ) and I need to convert it into a Type contained in the type variable: Type type = typeof(DateTime); /* can be any other Type like string, ulong etc */ var obj = jsonObject["date_joined"]; /* contains 2012-08-13T06:01:23Z+05:00 */ var result = Some_Way_To_Convert(type, obj); The above result should be a DateTime object with the value given in date_joined . Full Story I'm using both RestSharp and Json.NET in a Windows Phone

Converting a JToken (or string) to a given Type

南楼画角 提交于 2019-12-28 11:42:09
问题 TL;DR Version I have a object of type JToken (but can also be a string ) and I need to convert it into a Type contained in the type variable: Type type = typeof(DateTime); /* can be any other Type like string, ulong etc */ var obj = jsonObject["date_joined"]; /* contains 2012-08-13T06:01:23Z+05:00 */ var result = Some_Way_To_Convert(type, obj); The above result should be a DateTime object with the value given in date_joined . Full Story I'm using both RestSharp and Json.NET in a Windows Phone

Self referencing loop from Newtonsoft JsonSerializer using Entity Framework Core

大城市里の小女人 提交于 2019-12-28 07:07:11
问题 I've encountered the error: JsonSerializationException: Self referencing loop detected for property 'Subject' with type 'Project.Models.Subject'. Path 'data[0].Totals'. It occurs when I load a View with a dataGrid populated by an IEnumerable<Subject> model. The Grid is a DevExtreme DataGrid bound to the View's model like this: @(Html.DevExtreme().DataGrid() .DataSource(Model) .Paging(paging => { paging.Enabled(true); paging.PageIndex(0); paging.PageSize(20); }) .Columns(columns => { columns

Conditional member serialization based on query parameter?

浪子不回头ぞ 提交于 2019-12-28 04:33:12
问题 I would like to control which properties from my model are serialized to my WebAPI2 JSON response, based on matching a query parameter to an attribute. I mainly want to do this to reduce bandwidth on GETs without causing a proliferation of ViewModel classes. For example: GET /books/1?format=summary public class Book { [SerializeFormat("summary")] public int Id { get; set; } [SerializeFormat("summary")] public string Title { get; set; } public string Contents { get; set; } } or

Newtonsoft inline formatting for subelement while serializing

血红的双手。 提交于 2019-12-28 04:33:07
问题 Is it possible to create an attribute to serialize some subelements inline (Formatting.None) with newtonsoft json.net? I have a very huge set of data and I want to keep it readeable. Some subelements are not very important and can be writen inline. { "name": "xxx", "desc": "xxx", "subelem": [ {"val1": 1, "val2": 2, ...}, //inline, {"val1": 1, "val2": 2, ...}, ... ] "subelem2": { "val1": 1, "val2": 2, ... } } I want to force the inline serialization for some sub objects of my models. In this

Web API: Configure JSON serializer settings on action or controller level

ぃ、小莉子 提交于 2019-12-28 03:45:09
问题 Overriding the default JSON serializer settings for web API on application level has been covered in a lot of SO threads. But how can I configure its settings on action level? For example, I might want to serialize using camelcase properties in one of my actions, but not in the others. 回答1: Option 1 (quickest) At action level you may always use a custom JsonSerializerSettings instance while using Json method: public class MyController : ApiController { public IHttpActionResult Get() { var