json.net

Json.net failing to load certain properties belonging to a class object?

二次信任 提交于 2019-12-31 07:24:46
问题 NOTE: I'm adding this community wiki entry to save someone the loss of time I just went through debugging this problem. I have a class object with multiple public properties. I can serialize it fine using JSON.net. But when I load the JSON text back and deserialize it using JsonConvert.DeserializeObject<> , some of the fields are set to NULL when they definitely had valid values at the time of serialization. I inspected the serialized JSON string manually and I definitely see values for the

How do I deserialize a property containing an escaped JSON string? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-31 05:57:28
问题 This question already has answers here : How do I convert an escaped JSON string within a JSON object? (4 answers) Closed last year . I have an application/json response from an API that has a property, itself, containing an escaped JSON string. { "id": 0, "aggregation_id": "533741f4-49da-4db9-9660-4ca7bafb30e1", "task_id": "217", "event_type": "discovery", "event_name": "device_discovery_complete", "method": "ssh", "message_details": "{\"aggregation_id\":\"533741f4-49da-4db9-9660

JSON: How to parse the JSON string which contains “object”:“page”

别来无恙 提交于 2019-12-31 05:28:12
问题 We receive JSON data from Facebook Real Time subscription. The JSON itself contains property like "object":"page" and we need to access this property. { "entry":[ { "changes":[ ], "id":"1037501376337008", "time":1465883784 } ],"object":"page" } We use dynamic object to parse the JSON but when we try to access the result.object, it is not allowed as object is the keyword in C#. dynamic result = JsonConvert.DeserializeObject<dynamic>(jsonRealTimeNotification); string objectType = result.object

How to exclude properties from JsonConvert.PopulateObject that don't exist in some base type or interface?

吃可爱长大的小学妹 提交于 2019-12-31 04:44:05
问题 Is it possible to define options to JsonConvert.PopulateObject to exclude fields given in a json, that does not exists in the target object's interface implementation? public interface IElementWriter { string Name { get; set; } } public interface IElementUpdateWriter : IElementWriter { string FirstName { get; set; } } public interface IElementInsertWriter : IElementWriter { DateTime? CreationDate { get; set; } } public class Element:IElementWriter, IElementInsertWriter, IElementUpdateWriter {

Newtonsoft.Json.JsonSerializationException “Self referencing loop detected” in library code

不羁岁月 提交于 2019-12-31 03:26:28
问题 I have self referencing data: each Item contains a list of Scrapbooks that it is a member of and each Scrapbook contains a list of Items it contains. Clearly that's circular, and so when a Scrapbook is serialised I get a Newtonsoft.Json.JsonSerializationException "Self referencing loop detected" error. We get around this on our Azure Mobile Services server by adding the line GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json

How to apply JsonExtensionData (Dictionary<string, JToken>) to another object with JSON.Net

一个人想着一个人 提交于 2019-12-31 02:43:10
问题 I came across this (seemingly usual) scenario but I could not find a satisfying solution. Maybe someone knows: For some reason I parse JSON and I allow the user to provide more key-value pairs than my class has properties. I store the arbitrary ones away like so: class MusterNode { // some definite property public string TypeName { get; set; } // takes the rest // see https://www.newtonsoft.com/json/help/html/DeserializeExtensionData.htm [JsonExtensionData] private Dictionary<string, JToken>

JsonPropertyAttribute ignored on private property in derived class

喜你入骨 提交于 2019-12-31 02:13:09
问题 I have a problem with Json.Net when serializing derived objects, which have private properties. Sth like public class Base { [JsonProperty] private string Type { get { return "Base"; } } } public class Inherited : Base { [JsonProperty] private string Type { get { return "Inherited"; } } } When I serialize instances of Inherited , the Type property is always set to "Base". The only way I've found that work is that the property is protected or public and overriden in subclass. Why does it work

Intercept list population to assign values in deserialization

心已入冬 提交于 2019-12-31 01:57:08
问题 I have a recursive class (tree hierarchy), that derives from a list, that has the children, and itself, populated from deserialization in JSON.NET. The TLDR version is that I want to populate a variable in the children, from the parent, on each level of this class where it exists, without using $ref variables from JSON.NET (saves space when storing to a cookie). For those that followed my question from yesterday, this may look like a duplicate, but it is not. It is the same code, but the old

Handling JSON circular reference exception in ASP.NET 5

梦想与她 提交于 2019-12-31 01:13:45
问题 So I'm playing around with Web API in ASP.NET 5. At some point my app stopped working, only showing "Bad Gateway" IIS error page (I run it in IIS Express, by F5). It took me a while to figure out what the problem was - I introduced a circular reference into a class one of my Web API methods returns, like this: public class CircularParent { public CircularChild Data; public CircularParent() { Data = new CircularChild(this); } } public class CircularChild { public CircularParent Owner { get;

How do you serialize an enum array to a Json array of strings? [duplicate]

社会主义新天地 提交于 2019-12-31 01:11:09
问题 This question already has answers here : Serialize a container of enums as strings using JSON.net (2 answers) Closed last year . Based on Diego's unanswered comment under the top-voted answer in this question: JSON serialization of enum as string So for an enum: public enum ContactType { Phone = 0, Email = 1, Mobile = 2 } And for eg. a property: //could contain ContactType.Phone, ContactType.Email, ContactType.Mobile IEnumerable<ContactType> AvailableContactTypes {get;set;} To something like