json.net

JSON.NET Serialize DateTime.MinValue as null

落花浮王杯 提交于 2019-12-05 01:34:28
I'd like DateTime fields that are set to DateTime.MinValue returned by my Web API to be serialized to NULL instead of "0001-01-01T00:00:00" . I understand there's a way to get JSON.NET to omit fields that are set to default values, but I would prefer JSON.NET to specifically serialize DateTime MinValue / "0001-01-01T00:00:00" as null . Is there a way to do this? Create a custom converter which serializes DateTime.MinValue into null , and (if required) deserializes null into DateTime.MinValue : public class MinDateTimeConverter : DateTimeConverterBase { public override object ReadJson

JSON.NET Serialization on an object with a member of type Stream?

混江龙づ霸主 提交于 2019-12-05 01:14:45
Hopefully this is an easy fix that I have overlooked. I have an object passed into an event handler that I want to serialize that object using JSON.NET, like so: public void OnEvent(IEventObject foo) { // Serialize foo to string/disk here? var data = JsonConvert.SerializeObject(foo, Formatting.Indented); } It appears that one or more of foo's members are streams. I already recognize that Streams are not serializable since they are an abstraction over data and not the data itself. This makes sense. I do not know how to serialize this object anyway by either: a) Converting the streams into data

Copy JsonSerializerSettings from JsonSerializer to new JsonSerializer

谁都会走 提交于 2019-12-05 00:51:48
Is there any way to take the settings out of a JsonSerializer class and reimplement them in a new JsonSerializer ? There doesn't seem to be any methods to do anything like that. The best I found was a private method to be called through reflection, ApplySerializerSettings . I'm trying to take the serializer from the WriteJson method and copy it into a new serializer, tweaking it a bit. Specifically I want to replace the ContractResolver . public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) Zachary Dow Seems like the best way is to just copy all the

Can I distribute JSON.NET with my commercial application? [closed]

我怕爱的太早我们不能终老 提交于 2019-12-05 00:44:37
I am looking for a .NET JSON library that I can use to access JSON objects in a xpath like fashion. For example, given a JSON object in a string, can access its attributes by specifying a path in another string. Well, I found JSON.NET will do the job, however I am not sure if I can use and distribute it with my commercial application without any legal consequences or restrictions. Also, I am not planning to distribute the code of my app. I checked their readme, and the license states the following. I just want to make sure there is no hidden information, or useful things to know before picking

Json Datetime issue [duplicate]

醉酒当歌 提交于 2019-12-05 00:32:03
This question already has answers here : Closed 7 years ago . Possible Duplicate: how to force netwtonsoft json serializer to serialize datetime property to string? I am using Newtonsoft.Json to convert my object in to JSON file. But I am having issue with the DateTime , in my object datetime field is set as "7/30/2012 8:29:12 PM" but in JSON file. I am getting DateTime field in this format: "\/Date(1343660352227+0530)\/" . I need to get the DateTime in the same format as that of the object. Is it possible? What should I do in order to get the same format? Amol Kolekar I got the solution as

Json.net deserializing list gives duplicate items

旧巷老猫 提交于 2019-12-05 00:30:45
I have just started using Newtonsoft.Json (Json.net). In my first simple test, I ran into a problem when deserializing generic lists. In my code sample below I serialize an object, containing three types of simple integer lists (property, member var and array). The resulting json looks fine (the lists are converted into json-arrays). However, when I deserialize the json back to a new object of the same type, all list items are duplicated, expect for the array. I've illustrated that by serializing it a second time. From searching around, I've read that there may be a "private" backing field to

How to filter JSON array in C#

匆匆过客 提交于 2019-12-05 00:06:25
问题 I have spent a lot of time to find a solution for my problem. In this example, I have 2 records in SetNavRecords array. The first one is "Artikelnummer" : "21700" and the second one is "Artikelnummer" : "21701" Each record have an array "OfflineVerkaufspreis". Important is for me the field "Location_Code" in "OfflineVerkaufspreis" I only need the hole informations for one filtered Location Code. How can I select the data for one Location Code, for example "MH"? I am using C# and the

Newtonsoft.Json causing serialization to happen twice causing duplicate definition in the Reference.cs

为君一笑 提交于 2019-12-04 22:59:55
I have a project Common that has a service reference. After adding a reference to Newtonsoft.json(Version 6.0.2 to the same project(Common) which has service reference, and a Serializable class ChatLine [Serializable] public class ChatLine { [JsonProperty("L")] public string LineId { get; set; } [JsonProperty("CT")] public DateTime ConversationTimeInUtc { get; set; } [JsonProperty("S")] public string SenderId { get; set; } [JsonProperty("R")] public IEnumerable<string> Recipients { get; set; } [JsonProperty("CM")] public string ConversationMessage { get; set; } } I updated the service

How to pass parameter to constructor deserializing json

倖福魔咒の 提交于 2019-12-04 22:58:52
I have a small problem with passing some parent instance to a constructor when deserializing an object with Newtonsoft.Json . Let's assume i have the following classes public class A { public string Str1 { get; set; } public IList<B> Bs { get; set; } } public class B { public B(A a) { // a should not be null! Console.WriteLine(a.Str) } } And now i serailze and than deserialize the object a like this: A a = new A() a.Bs = new List<B>() a.Bs.Add(new B(a)); a.Bs.Add(new B(a)); a.Bs.Add(new B(a)); var json = JsonConvert.SerializeObject(a); // Here i need to call the constructor of B when creating

How can Json.NET perform dependency injection during deserialization?

烂漫一生 提交于 2019-12-04 22:51:58
When I have a class with no default constructor, i.e. using dependency injection to pass its dependencies, can Newtonsoft.Json create such an object? For example: public class SomeFoo { private readonly IFooDependency _dependency; public SomeFoo(IFooDependency dependency){ if(dependency == null) throw new ArgumentNullException("dependency"); _dependency = dependency; } public string Data { get; set; } public int MoreData { get; set; } public void DoFoo(){ Data = _dependency.GetFooData(); MoreData = _dependency.GetMoreFooDate(); } } During serialization, I only care of storing Data and MoreData