json.net

Why does System.Text Json Serialiser not serialise this generic property but Json.NET does?

天大地大妈咪最大 提交于 2021-02-16 18:17:05
问题 I have the following situation. I have simplified the problem into the following example, although my real situation is more complicated. System.Text.Json does not serialise the object fully but Newtonsoft Json.NET does. Suppose I have the following class structure. public class A { public string AProperty { get; set; } = "A"; } public class A<T> : A where T : class, new() { public T TObject { get; set; } = new T(); } public class B { public string BProperty { get; set; } = "B"; } public

how to get Json.net serialize DateTime similar to JavaScriptSerializer?

社会主义新天地 提交于 2021-02-16 14:36:10
问题 My code base had been using JavaScriptSerializer for long time, now I am planning to migrate to Json.Net . I saw that DateTime Serializing on both work differently. Console.WriteLine(new JavaScriptSerializer().Serialize(DateTimeOffset.UtcNow)); Output:"\/Date(1403629024695)\/" Console.WriteLine(JsonConvert.SerializeObject(DateTimeOffset.UtcNow)); Output: "2014-06-24T16:57:04.6954145+00:00" Is there a way to get Json.net to serialize similar to JavaScriptSerializer? 回答1: You can use

Serializing Timespan in Dictionary<string, object> with Json.NET

主宰稳场 提交于 2021-02-16 14:21:46
问题 I have a property that is actually a Dictionary. And I keep many types in this dictionary like TimeSpans, DateTimes, etc. But serializing and deserializing TimeSpans are wrong and it deserializes as string. var dict = new Dictionary<string, object>(); dict.Add("int", 15); dict.Add("string", "foo"); dict.Add("timeSpan", new TimeSpan(1,1,1)); dict.Add("dateTime", DateTime.Now); var settings = new JsonSerializerSettings{ TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat =

C#笔记——7.序列化与反序列化

让人想犯罪 __ 提交于 2021-02-12 07:59:34
序列化与反序列化简介 : 序列化指将对象转换为字节流的过程,与之相反的便是反序列化,即将字节流转换为对象的过程。 .NET中进行对象序列化的几种方式 : 二进制序列化 :对象序列化之后是二进制形式的,通过System.Runtime.Serialization.Formatters.Binary命名空间下的BinaryFormatter类来实现序列化操作。 SOAP序列化 :对象序列化之后的结果符合SOAP协议,即可以使用SOAP协议传输,通过System.Runtime.Serialization.Formatters.Soap命名空间下的SoapFormatter类来实现序列化操作。 XML序列化 :将对象序列化为XML形式,可以通过位于System.Xml.Serialization命名空间下XmlSerializer类来实现,要读取XML中的数据可以直接使用XmlTextReader、XmlDocument、XPath, 也可以使用LINQ TO XML或者反序列化的方法从XML中读取数据。 Json序列化 :通过序列化将.net对象转换为JSON字符串,在.NET中操作JSON,可以使用 LitJson 或者 Json.NET ,也可以使用.NET内建的System.Runtime.Serialization.Json,相比较而言Json.NET功能更加强大一些,JSON

Create a nested json object from c# model

依然范特西╮ 提交于 2021-02-11 12:31:28
问题 This is my json: [ { "ChangeFlowsFromParent":"false", "ChangeFlowsToParent":"true", "StreamType":"mainine", "streamName":"ArgOS_2_0", "Parent":"none", "Compliance":"Released", "children":[ { "ChangeFlowsFromParent":"true", "ChangeFlowsToParent":"true", "StreamType":"Release", "streamName":"ArgOS_2_0_DHAL1", "Parent":"ArgOS_2_0", "Compliance":"Released", "children":[ { "ChangeFlowsFromParent":"false", "ChangeFlowsToParent":"true", "StreamType":"Release", "streamName":"ArgOS_child_DHAL2",

Json.NET: Different JSON schemas for a single property

泪湿孤枕 提交于 2021-02-10 18:01:01
问题 I'm working with a third-party API that plays fast and loose with its returned JSON objects. In C#, I'm trying to set up Json.NET to deserialize these JSON objects to classes, but I've run into a hitch: sometimes the same property name will be used with several different schemas, depending on context. Here's an example of the JSON data structure: { "examples": [{ "data": "String data", "type": "foo" }, { "data": { "name": "Complex data", "19": { "owner": "Paarthurnax" } }, "type": "complex" }

Json.NET: Different JSON schemas for a single property

*爱你&永不变心* 提交于 2021-02-10 17:59:45
问题 I'm working with a third-party API that plays fast and loose with its returned JSON objects. In C#, I'm trying to set up Json.NET to deserialize these JSON objects to classes, but I've run into a hitch: sometimes the same property name will be used with several different schemas, depending on context. Here's an example of the JSON data structure: { "examples": [{ "data": "String data", "type": "foo" }, { "data": { "name": "Complex data", "19": { "owner": "Paarthurnax" } }, "type": "complex" }

JsonConverters and object properties

偶尔善良 提交于 2021-02-10 12:12:31
问题 I'm trying to find an answer to a problem that has stumped me for a few days, I'm converting some legacy WCF code over to SignalR, it's an internal API and the client is Silverlight for the moment. I have a .NET class (Content1) on the server side that has a matching JSON converter and a message class (Message1) that has a property of type Content1. This all seems to work fine, the problem is when that property is changed to type object (which the current code is) public class Message1 {

JsonConverters and object properties

廉价感情. 提交于 2021-02-10 12:07:40
问题 I'm trying to find an answer to a problem that has stumped me for a few days, I'm converting some legacy WCF code over to SignalR, it's an internal API and the client is Silverlight for the moment. I have a .NET class (Content1) on the server side that has a matching JSON converter and a message class (Message1) that has a property of type Content1. This all seems to work fine, the problem is when that property is changed to type object (which the current code is) public class Message1 {

Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'System.Collections.Generic.IEnumerable<Employee>'

為{幸葍}努か 提交于 2021-02-10 11:44:06
问题 I have the following code: public EmployeeDepartment InsertEmployeeAndDepartment(params dynamic[] employee) { filteredEmployees.EmployeeRecords = employee[0]; } Where EmployeeRecords is of type IEnumerable<Employee> I am getting the following exception: Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) How can I resolve this? 回答1: The message suggests that employee[0] is being