json.net

Adding a property into specified location into json using Newtonsoft.Json

拜拜、爱过 提交于 2020-06-07 07:24:29
问题 I have a following JSON . I have added 2 x double quotes so that I could declare it as string variable in c# , in reality it contains one times double quotes for properties. The complete example code is in this fiddle. I am using Newtonsoft but can use System.Text.Json as well so a working solution for either will be appreciated. { ""display"": ""wizard"", ""settings"": {}, ""components"": [ { ""title"": ""Activity Information"", ""label"": ""Activity Information"", ""type"": ""Activity"", "

Json.NET: How to make DefaultValueHandling only apply to certain types?

旧时模样 提交于 2020-05-31 07:27:07
问题 I have a custom class that looks like this: public class PartnerLoginOptions { public string Username { get; set; } public string Password { get; set; } public string DeviceModel { get; set; } public string Version { get; set; } public bool? IncludeUrls { get; set; } public bool? ReturnDeviceType { get; set; } public bool? ReturnUpdatePromptVersions { get; set; } } I'd like to ignore any bool? members with default values when serializing, but keep strings with null values. For example, if I

.net Core 3 [JsonIgnore] not working when requesting single resource

独自空忆成欢 提交于 2020-05-31 05:22:51
问题 in my .net Core 3.0 Api the [JsonIgnore] Attribute is not working as excepted. Im using System.Text.Json instead of the old Newtonsoft.Json When i'm using my resource that returns a list of Objects, for example: /api/Object/ the objects are serialized like this: [ { "id": 1, "date": "2020-02-12T08:45:51.502", "userId": 1, "tags": [ { "name": "string" } ] } ] But when I request a single result /api/Object/{id} the full object gets serialized like this: { "user": { "hasAccess": false, "id": 1,

Newtonsoft.Json issue with deserialising relational model

≯℡__Kan透↙ 提交于 2020-05-29 10:21:45
问题 I've a simple relational model with a Parent and Child relation as follows: public class Parent { public Parent(int id) { Id = id; } public int Id { get; private set; } public IList<Child> Children { get; set; } = new List<Child>(); } public class Child { public Parent Parent { get; set; } } I create a small object graph consisting of a list of two children who share the same parent: var parent = new Parent(1); var child1 = new Child {Parent = parent}; var child2 = new Child {Parent = parent}

Newtonsoft.Json issue with deserialising relational model

痞子三分冷 提交于 2020-05-29 10:20:18
问题 I've a simple relational model with a Parent and Child relation as follows: public class Parent { public Parent(int id) { Id = id; } public int Id { get; private set; } public IList<Child> Children { get; set; } = new List<Child>(); } public class Child { public Parent Parent { get; set; } } I create a small object graph consisting of a list of two children who share the same parent: var parent = new Parent(1); var child1 = new Child {Parent = parent}; var child2 = new Child {Parent = parent}

Newtonsoft.Json issue with deserialising relational model

倖福魔咒の 提交于 2020-05-29 10:19:21
问题 I've a simple relational model with a Parent and Child relation as follows: public class Parent { public Parent(int id) { Id = id; } public int Id { get; private set; } public IList<Child> Children { get; set; } = new List<Child>(); } public class Child { public Parent Parent { get; set; } } I create a small object graph consisting of a list of two children who share the same parent: var parent = new Parent(1); var child1 = new Child {Parent = parent}; var child2 = new Child {Parent = parent}

What is the Correct way to define key-value pairs in json schema

天涯浪子 提交于 2020-05-29 03:21:32
问题 How do i define key value pairs object in json schema (the "correct" way) ? i want to define this: "id" : 99, _info : { "name" : "somename", "href" : "someUrl" } Are any of the following two accurate?: 1) { "type": "object", "name": "MyObj", "properties": { "id": { "type": "integer" }, "_info": { "type": "array", "items": { "type": "object" "properties": { "key": { "type": "string", "description": "key" }, "value": { "type": "string", "description": "the value" } } } } } } 2) { "type":

Deserializing JSON string fails when JSON dictionary is empty

一个人想着一个人 提交于 2020-05-28 04:57:26
问题 Currently I'm facing the following issue when I try to deserialize a json string that I receive from a 3rd party (--> I cannot change the received json string by myself) with Newtonsoft.Json : The json contains a dictionary (and some other entries that I don't list here): "food": { "Menu 1": "abc", "Menu 2": "def" } I've created a class that contains the property (plus the properties that I didn't list here): Dictionary<string, string> Food {get; set;} In this case the desiralization of the

Change default type of numeric deserialization

醉酒当歌 提交于 2020-05-23 10:08:31
问题 While I was deserializing some JSON data to DataSet, the resultant dataset may lose its column schema. that means, When I deserialize some JSON, it populates the Dataset with Int64 objects rather than Int32. I would like it to choose Int32. I know, Json.NET by default reads integer values as Int64 because there is no way to know whether the value should be Int32 or Int64. JsonSerializerSettings settings = new JsonSerializerSettings() { Converters = { new PrimitiveJsonConverter() }, }; DataSet

How to sort properties alphabetically when serializing JSON using Netwonsoft library?

房东的猫 提交于 2020-05-16 04:36:33
问题 When serializing objects using the Netwonsoft.JSON library, it's possible to specify the output order using JsonPropertyAttribute property Order . However, I would like to also sort alphabetically the properties by default on top of that. 回答1: You can actually control the order by implementing IContractResolver or overriding the DefaultContractResolver's CreateProperties method. Here's an example of my simple implementation of IContractResolver which orders the properties alphabetically: