json.net

Ignore a property during xml serialization but not during deserialization

我与影子孤独终老i 提交于 2019-12-17 08:56:09
问题 In C#, how can I make XmlSerializer ignore a property during serialization but not during deserialization? (Or how do I do the same with Json.net?) To prevent a property from being serialized, you can add the XmlIgnore attribute: [XmlIgnore] public int FooBar {get;set;} This will cause the <FooBar> tag to be omitted during serialization. However, this also means that the <FooBar> tag will be ignored during deserialization. In my case, I accept an array of items from user in the request, and

How to deserialize JSON with duplicate property names in the same object

拟墨画扇 提交于 2019-12-17 07:50:21
问题 I have a JSON string that I expect to contain duplicate keys that I am unable to make JSON.NET happy with. I was wondering if anybody knows the best way (maybe using JsonConverter ? ) to get JSON.NET to change a JObject 's child JObjects into to JArrays when it sees duplicate key names ? // For example: This gives me a JObject with a single "JProperty\JObject" child. var obj = JsonConvert.DeserializeObject<object>("{ \"HiThere\":1}"); // This throws: // System.ArgumentException : Can not add

How to deserialize JSON with duplicate property names in the same object

冷暖自知 提交于 2019-12-17 07:49:35
问题 I have a JSON string that I expect to contain duplicate keys that I am unable to make JSON.NET happy with. I was wondering if anybody knows the best way (maybe using JsonConverter ? ) to get JSON.NET to change a JObject 's child JObjects into to JArrays when it sees duplicate key names ? // For example: This gives me a JObject with a single "JProperty\JObject" child. var obj = JsonConvert.DeserializeObject<object>("{ \"HiThere\":1}"); // This throws: // System.ArgumentException : Can not add

Merge two Json.NET arrays by concatenating contained elements

倾然丶 夕夏残阳落幕 提交于 2019-12-17 07:42:05
问题 I have two JToken 's that represent JSON arrays of objects and I would like to merge them. JToken has a method Concat but it produces null as result when I try to use it. Action<JToken> Ok = (x) => { Debug.WriteLine(x); /* outputs [ { "id": 1, }, { "id": 2, } ] */ x = (x).Concat<JToken>(x) as JToken; Debug.WriteLine(x); // null }; How can I make it work? 回答1: Use JContainer.Merge() with MergeArrayHandling.Concat . This is available starting with Json.NET 6 Release 4. So if your arrays are in

Merge two Json.NET arrays by concatenating contained elements

被刻印的时光 ゝ 提交于 2019-12-17 07:42:05
问题 I have two JToken 's that represent JSON arrays of objects and I would like to merge them. JToken has a method Concat but it produces null as result when I try to use it. Action<JToken> Ok = (x) => { Debug.WriteLine(x); /* outputs [ { "id": 1, }, { "id": 2, } ] */ x = (x).Concat<JToken>(x) as JToken; Debug.WriteLine(x); // null }; How can I make it work? 回答1: Use JContainer.Merge() with MergeArrayHandling.Concat . This is available starting with Json.NET 6 Release 4. So if your arrays are in

JToken: Get raw/original JSON value

坚强是说给别人听的谎言 提交于 2019-12-17 07:38:33
问题 Is there a way to get the raw/original JSON value from a JToken ? The problem: var data = JObject.Parse(@"{ ""SimpleDate"":""2012-05-18T00:00:00Z"", ""PatternDate"":""2012-11-07T00:00:00Z"" }"); var value = data["SimpleDate"].Value<string>(); The value is now 05/18/2012 00:00:00 but I need the original string 2012-05-18T00:00:00Z . Is there a way to get this original value? Also, I cannot change the way how the JObject is created (e.g. change settings), because it is passed as parameter into

Newtonsoft JSON dynamic property name

那年仲夏 提交于 2019-12-17 07:33:34
问题 Is there a way to change name of Data property during serialization, so I can reuse this class in my WEB Api. For an example, if i am returning paged list of users, Data property should be serialized as "users", if i'm returning list of items, should be called "items", etc. Is something like this possible: public class PagedData { [JsonProperty(PropertyName = "Set from constructor")]?? public IEnumerable<T> Data { get; private set; } public int Count { get; private set; } public int

How to turn on indentation when writing JSON using Json.net?

你离开我真会死。 提交于 2019-12-17 07:30:32
问题 I am using Json.Net to serialize XML into Json . When i write the serialized string to a file it all comes in a single line . How do i get it to actually look like Json with the usual tabs and indentation? 回答1: Set the JSON writer Formatting property to Formatting.Indented : jsonWriter.Formatting = Formatting.Indented; The JsonConvert.Serialize* methods also have overloads that take a Formatting enum (thanks John Flatness). Documentation: Serialize an Object 来源: https://stackoverflow.com

NewtonSoft add JSONIGNORE at runTime

懵懂的女人 提交于 2019-12-17 07:27:54
问题 Am looking to Serialize a list using NewtonSoft JSON and i need to ignore one of the property while Serializing and i got the below code public class Car { // included in JSON public string Model { get; set; } // ignored [JsonIgnore] public DateTime LastModified { get; set; } } But am using this Specific class Car in many places in my application and i want to Exclude the option only in one place. Can i dynamically add [JsonIgnore] in the Specific Place where i need ? How do i do that ? 回答1:

How can I get a list of keys from Json.NET?

风流意气都作罢 提交于 2019-12-17 07:15:42
问题 I'm using C# and Json.NET. If I have a JObject, I want a list of the keys within the object, similar to how object.Keys() returns the keys within the object. This seems like it'd be obvious, but I'm having a rough time finding a way to do this. Edit: I'm traversing through the object, and I want to spit out all the keys in the object as I go through. I realize that this example will result in seeing the same key multiple times, and that's OK for my needs. public void SomeMethod(JObject parent