json.net

Can I safely remove the fields and properties with the suffix Specified in my C# Model classes if I am only using JSON.Net

被刻印的时光 ゝ 提交于 2019-12-11 03:41:40
问题 I have a C# Application. I have a class that is generated from an xsd. The class looks as below public class Transaction { public bool amountSpecified {get; set;} public double amount {get; set;} } If you notice in the class above, along with the property amount, the generator has also generated a property called amountSpecified. I know that the properties with suffix "Specified" are required for all non-nullable field/property, because this is the requirement of XML Serializer as mentioned

JSON.net not including null properties with EmitDefaultValue false

戏子无情 提交于 2019-12-11 03:37:08
问题 I have a system with many data contracts where the members are decorated with: [DataMember(EmitDefaultValue = false)] I have a case where I need the members to be included when null when serialized to JSON. No matter what serializer settings I choose, I can not get this to work. [TestMethod] public void SerializationTest() { var contract = Activator.CreateInstance(typeof(TestContract)); var formatter = new JsonMediaTypeFormatter { SerializerSettings = new JsonSerializerSettings {

How to search JSON string for fields?

耗尽温柔 提交于 2019-12-11 03:36:58
问题 I have a JSON string: [{"number":"123-456-789","array":["1", "2"]}] . I want check to see if this JSON contains a "number" field. What I am trying: string jsonString = [{"number":"123-456-789","array":["1", "2"]}]; Newtonsoft.Json.Linq.JArray jsonObject = JArray.Parse(jsonString); How do I then "search" this jsonObject for a specified field? 回答1: If you would like to test if the "number" property exists, then you can use: bool exists = jsonObject[0].Children<JProperty>().Any(p => p.Name ==

Custom JSON Derivative Format

↘锁芯ラ 提交于 2019-12-11 03:27:30
问题 I would like to have a serialization format that is nearly identical to JSON, except that key-values are represented as <key>="<value>" instead of "<key>":"<value>" . With Newtonsoft I made a custom JsonConverter called TsonConverter that works fairly well, except that it can't "see" an embedded dictionary. Given the following type: public class TraceyData { [Safe] public string Application { get; set; } [Safe] public string SessionID { get; set; } [Safe] public string TraceID { get; set; }

Implementing Custom JsonWriter (JSON.net)

送分小仙女□ 提交于 2019-12-11 03:26:10
问题 I have been working on implementing this a custom JSON writer by following this sample I'm overriding the WriteValue methods so that instead of printing the value of a json, I print the Type.ToString() and conceal the real value of the item. I'm having trouble with this method: private void WriteValueElement(string elementName, JTokenType type) { _writer.WriteStartElement(elementName); _writer.WriteAttributeString("type", type.ToString()); } And particularly with this statement _writer

How to format properties that are only string in an object while converting to json?

风流意气都作罢 提交于 2019-12-11 03:14:18
问题 The intance type is not clear. I am using Foo as example. I have a format method and a class like below, public string FormatMethod(string s){ //for example pattern ++ return "++" + s + "++"; } public class Foo{ public int FooId {get;set;} public string Name {get;set;} public string Desciption {get;set;} } var foo = new Foo{ FooId = 1, Name = "FooName", Description = "Bla bla bla" }; // or var list = new List<Foo>(); list.Add(foo); var json = JsonConvert.SerializeObject(list); //or var

Append to a json file in windows application

醉酒当歌 提交于 2019-12-11 03:13:33
问题 I have created a Json format file in windows application using newtonsoft.the code is working and the json is creating fine.But I want to append the new file to the old json file,now it is replacing.here is my code List<DeviceData> tempDate = new List<DeviceData>(); DeviceData D = new DeviceData(); D.deviceId = St_Id.ToString(); D.ansId = AnswerStr; tempDate.Add(D); string ans = JsonConvert.SerializeObject(tempDate, Formatting.Indented); System.IO.File.WriteAllText(@"E:\" + " device.json",

JSON.Net ignoring class with only null properties

╄→尐↘猪︶ㄣ 提交于 2019-12-11 02:49:52
问题 Following up on my last question (http://stackoverflow.com/questions/8027748/render-c-sharp-class-as-javascript/8027824#8027824), I'm now using JSON.net to convert some classes to JSON. Works great, but I have some class which contain other classes as members. For instance: public class Parent { public Child Child { get; set; } public string Var { get; set; } } When I render this using json.net, I can set the serializer to ignore nulls. This means the Var member isn't printed when it hasn't

C# Json deserializing properties with changing names

浪尽此生 提交于 2019-12-11 02:49:05
问题 So I want to deserialize a Json reply that looks like this: { "Meta Data": { "1. Information": "Intraday (1min) prices and volumes", "2. Symbol": "OMXS30", "3. Last Refreshed": "2018-07-11 10:03:00", "4. Interval": "1min", "5. Output Size": "Compact", "6. Time Zone": "US/Eastern" }, "Time Series (1min)": { "2018-07-11 10:03:00": { "1. open": "1526.9352", "2. high": "1526.9522", "3. low": "1526.6548", "4. close": "1526.7195", "5. volume": "0" }, "2018-07-11 10:02:00": { "1. open": "1526.3879",

Why is JSON.NET adding all these backslashes

♀尐吖头ヾ 提交于 2019-12-11 02:45:18
问题 Please see: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System.IO; namespace TestJson2 { class Program { private static List<string> myCollections; static void Main(string[] args) { myCollections = new List<string>(); myCollections.Add("frog"); myCollections.Add("dog"); myCollections.Add("cat"); StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using