json.net

Details about the json:Array feature of Newtonsoft.JSON XML to JSON converter

一世执手 提交于 2019-12-12 03:43:25
问题 Referencing this example of using "json:Array": Converting between JSON and XML I have two questions: Does the namespace have to be "json"? I.e. if ns2 matched back to "xmlns:ns2='http://james.newtonking.com/projects/json'" would that work? Can the namespace be omitted? Can I just put "Array='true'"? I'm about to try to test by trial and error, but thought maybe somebody would know the answer, or someone in the future would like to know. Not that it matters a whole lot, but my XML is being

How to add types information to JSON on serialization?

时光怂恿深爱的人放手 提交于 2019-12-12 03:43:23
问题 Angular requires Date objects in many places whereas JSON contains string representation of the date. I want to add an array of properties which contain date values: class Foo { public int IntProp {get;set;} public DateTime? Prop1 {get;set;} public DateTime Prop2 {get;set;} public Bar Bar {set;set;} } class Bar { public DateTime Prop {get;set;} public IEnumerable<DateTime?> Dates {get;set;} } Foo should then be serialized like this: { "IntProp": 1, "Prop1": "...", "Prop2": "...", "Bar": {

Deserializing large files using Json.NET

二次信任 提交于 2019-12-12 03:39:16
问题 I am trying to process a very large amount of data (~1000 seperate files, each of them ~30 MB) in order to use as input to the training phase of a machine learning algorithm. Raw data files formatted with JSON and I deserialize them using JsonSerializer class of Json.NET. Towards the end of the program, Newtonsoft.Json.dll throwing 'OutOfMemoryException' error. Is there a way to reduce the data in memory, or do I have to change all of my approach (such as switching to a big data framework

JSON.NET serializer improperly serializes string as boolean

∥☆過路亽.° 提交于 2019-12-12 03:34:29
问题 I have a class: [JsonObject(MemberSerialization.OptIn)] public class UserPreferenceDTO { [JsonProperty] public string Name { get; set; } [JsonProperty] public string Value { get; set; } public static explicit operator UserPreferenceDTO(UserPreference pref) { if (pref == null) return null; return new UserPreferenceDTO { Name = pref.Name, Value = pref.Value }; } public static explicit operator UserPreference(UserPreferenceDTO pref) { if (pref == null) return null; return new UserPreference {

Deserialize JSON that could be an array or a single item

[亡魂溺海] 提交于 2019-12-12 03:25:52
问题 I am using an API from a 3rd-party that returns different JSON results from the same endpoint depending on how many results there are. If there is a single result the response is: { "data": { ... }, "metadata": { ... } } However if the result has more than one the response is: { "items": [{ "data": {...}, "metadata": {...} }, { "data": {...}, "metadata": {...} }], "metadata": {...} } I'm using C# and Json.Net and can't work out how to dynamically handle this mixed response. Is there a way to

How to have JSON.NET render forward references during serialization

笑着哭i 提交于 2019-12-12 03:22:13
问题 JSON.NET (using the setting PreserveReferencesHandling = PreserveReferencesHandling.Objects ) serializes a reoccurring object inline on first occurrence and serialzes it as JSON reference on subsequent occurrences. I'm guessing this is done to avoid forward references. For example: class Person { public string Name { get; set; } public Person Mother { get; set; } } var joni = new Person { Name = "Joni" }; var james = new Person { Name = "James", Mother = joni }; var people = new List<Person>

Simplify looking up nested Json values with Json.NET

♀尐吖头ヾ 提交于 2019-12-12 03:07:09
问题 I use the Facebook's graph Api to get posts from a Facebook page I administer. To get a url to the full size picture of a post I included the "attachments" field. the JSon obtained is as follows: { "data": [ { "message": "Using Facebook's Graph Api to get Testdrive's news from the Facebook page on to the website. So this post will be visible in a minute at the website as well. Cool!", "link": "https://www.facebook.com/TestdriveDressage/photos/a.493612667417831.1073741827.493607594085005

JsonConverter where the property is composed of fields on its parent

寵の児 提交于 2019-12-12 02:42:31
问题 I have a situation where an API has multiple array-like objects as individual properties on a object. For example: "parent": { id: 4, /*... other fields ...*/ "prop_1": "A", "prop_2": "B", /*... other "props" ...*/ "prop_24": "W" } I want the resulting model in C# to not repeat that same structure and have prop_X deserialized as a List and serialized back to that mess. class Parent { [JsonProperty("id")] public int ParentId { get; set; } /*... other properties ...*/ public List<string> Props

Json .Net serialize flat object into a complex object (change objects structure on serialization/deserialization)

帅比萌擦擦* 提交于 2019-12-12 02:26:09
问题 I have a flat DTO like this public class User { [JsonProperty("email")] public string Email { get; set; } [JsonProperty("fname")] public string FirstName { get; set; } [JsonProperty("lname")] public string LastName { get; set; } [JsonProperty("phone")] public string Phone { get; set; } [JsonProperty("city")] public string City { get; set; } [JsonProperty("country")] public string CountryCode { get; set; } [JsonProperty("state")] public string State { get; set; } [JsonProperty("zip")] public

Deserializing a collection of interfaces

江枫思渺然 提交于 2019-12-12 02:18:22
问题 I have the following class which is perfectly serializable: public class Form { public IList<IControl> Controls { get; set; } } public class ControlA : IControl {} public class ControlB : IControl {} It serializes without $type information but I have a custom JsonConverter which is able to deserialize all implementations of IControl : internal class MyJsonConverter : CustomCreationConverter<IControl> {} It works fine for a scenario like this: [JsonConverter(typeof(MyJsonConverter ))] public