json.net

JSON.NET and Replacing @ Sign in XML to JSON converstion

可紊 提交于 2019-12-17 20:35:01
问题 The JSON.NET framework can convert XML to JSON, but it uses the @ sign in the JSON as the attribute. I would rather remove this before sending it to the view. What would be the best approach for this? I know I can do a straight up replace, but an @ character may be relevant somewhere and shouldn't be replaced. Is there a Regex for this? public ActionResult Layout() { var xml = new XmlDocument(); xml.XmlResolver = null; xml.Load(Server.MapPath("~/App_Data/Navigation.xml")); return Content

Using Json.net - partial custom serialization of a c# object

倾然丶 夕夏残阳落幕 提交于 2019-12-17 20:17:52
问题 I ma using Newtonsofts' Json.Net to serialize some and array of objects to json. The objects have a common set of properties but also have Meta property which is a dictionary During serialization I want the key value pairs to be added to my json object as if they where root level properties, like this... { id: 1, name:'jeff', food:'spinch', spoon: 'ýes' } Not like this: { id: 1, name:'jeff', meta:{ food:'spinch', spoon: 'ýes' } } I have dug through JsonSerializerSettings but cant seem to spot

Json.net serialization of custom collection implementing IEnumerable<T>

◇◆丶佛笑我妖孽 提交于 2019-12-17 20:17:17
问题 I have a collection class that implements IEnumerable and I am having trouble deserializing a serialized version of the same. I am using Json.net v 4.0.2.13623 Here is a simplier version of my collection class that illustrates my issue public class MyType { public int Number { get; private set; } public MyType(int number) { this.Number = number; } } public class MyTypes : IEnumerable<MyType> { private readonly Dictionary<int, MyType> c_index; public MyTypes(IEnumerable<MyType> seed) { this.c

Deserializing public property with non-public setter in json.net

耗尽温柔 提交于 2019-12-17 20:15:16
问题 Suppose I have the following class - public class A { public int P1 { get; internal set; } } Using json.net, I am able to serialize the type with P1 property. However, during deserialization, P1 is not set. Without modifying class A, is there an in build way to handle this? In my case, I am using a class from a different assembly and cannot modify it. 回答1: Yes, you can use a custom ContractResolver to make the internal property writable to Json.Net. Here is the code you would need: class

Json.NET JSONPath query not returning expected results

一曲冷凌霜 提交于 2019-12-17 20:14:33
问题 I'm using Newtonsoft's Json.Net to select nodes from the following json: { "projects":[ { "name":"Project 1", "client":{ "code":"ABC", "name":"Client 1" } }, { "name":"Project 2", "client":{ "code":"DEF", "name":"Client 2" } }, { "name":"Project 3", "client":{ "code":"GHI", "name":"Client 3" } } ] } The following c# snippet //json is a JObject representation of the json listed above var clients = json.SelectTokens("$.projects[*].client"); Yields: [ { "code":"ABC", "name":"Client 1" }, { "code

JsonSerializationException 'Unable to find a constructor' on Xamarin.Android

↘锁芯ラ 提交于 2019-12-17 20:05:58
问题 I have this very odd problem with my code, and it's a pretty new problem, considering I didn't have it half a year ago. Long story short, I've made an app in Xamarin, and released it about half a year ago, on all 3 stores (App Store, Google Play and Microsoft Store). Yesterday a user reported a problem with the Android app, and after I got that fixed and recompiled, I'm now encountering a new error with Json.NET The exception is Newtonsoft.Json.JsonSerializationException: Unable to find a

JsonProperty - Use different name for deserialization, but use original name for serialization?

拟墨画扇 提交于 2019-12-17 19:47:12
问题 I am retrieving JSON from an API. I am using newtonsoft (this is json.net right?) to deserialize this into a list of objects. It works. Unfortunately I also need to pass this along to someone else as JSON (they can't call the API directly only I have access to it). I say unfortunately because I need to OUTPUT my JSON that is different from what is being received (the property names need to be different). For example, I have a class called Person, with a property called Name. I want to get

Json.NET crashes when serializing unsigned integer (ulong) array

孤街醉人 提交于 2019-12-17 19:46:32
问题 Getting a parser error when trying to serialize a ulong array, looks like the Json.NET library isnt checking if the integer is signed or unsigned; any one know of a workaround for this? or any other .NET Json library that can handle unsigned int's? * EDIT: code below; * It serializes fine, but when its deserializing it throws an error; Looks like it doesnt cater for the unsigned int from looking at the stack trace; NewTonsoft.Json.JsonReaderException : {"JSON integer 18446744073709551615 is

Json.Net calls property getter during deserialization of list, resulting in duplicate items

霸气de小男生 提交于 2019-12-17 19:39:48
问题 I'm using json.net to implement the memento pattern for a winform application. I'm using the memento to rollback an object on a failed database transaction. The problem I'm getting is that when deserializing the memento, the getter is called rather than the setter. Let me demonstrate: class MyClass { public int ID { get; set; } public string field1 { get; set; } public string field2 { get; set; } private List<SomeObject> _someObjects; public List<SomeObject> SomeObjects { get { if(

Json.Net PopulateObject - update list elements based on ID

安稳与你 提交于 2019-12-17 19:25:20
问题 It is possible to define a custom "list-merge" startegy used for the JsonConvert.PopulateObject method? Example: I have two models: class Parent { public Guid Uuid { get; set; } public string Name { get; set; } public List<Child> Childs { get; set; } } class Child { public Guid Uuid { get; set; } public string Name { get; set; } public int Score { get; set; } } My initial JSON: { "Uuid":"cf82b1fd-1ca0-4125-9ea2-43d1d71c9bed", "Name":"John", "Childs":[ { "Uuid":"96b93f95-9ce9-441d-bfb0