json.net

New line character in JSON

本小妞迷上赌 提交于 2019-12-22 18:27:39
问题 We are using a WCM tool for content entry and content editors will enter content having text and html in the tool. To convert the content into JSON we are using newton JSON as below where we are passing simple dictionary of keys and values: string output = JsonConvert.SerializeObject(nonJsonResult); Whenever editor gives content with new lines then we are getting the serailized JSON output with \r\n . Example input text: first line second line third line Output: { "test": "first line\r

How to reference external files with JSON.net?

ⅰ亾dé卋堺 提交于 2019-12-22 13:57:26
问题 Here is the error I am getting when JSON.net is trying to read my JSON schema ( return JsonSchema.Read(new JsonTextReader(reader)); ): 2014-07-15 15:33:42.7011 [Fatal] Newtonsoft.Json.JsonException: Could not resolve schema reference 'data-result.json'. at Newtonsoft.Json.Schema.JsonSchemaBuilder.ResolveReferences(JsonSchema schema) in c:\Development\Releases\Json\Work ing\Newtonsoft.Json\Src\Newtonsoft.Json\Schema\JsonSchemaBuilder.cs:line 139 at Newtonsoft.Json.Schema.JsonSchemaBuilder

POST JSON with Flurl

送分小仙女□ 提交于 2019-12-22 13:49:12
问题 I start with Flurl and I would like to create a POST but I think I have a problem with the format of my JSON parameters. You can see the JSON parameters: { "aaaUser" : { "attributes" : { "name" : "device:domain\\login", "pwd" : "123456" } } } These settings work with Postman and now I would like to use Flurl to continue my little POST :) But my JSON format is not correct. using System.Threading.Tasks; using Flurl.Http; namespace Script { class Program { static async Task Main(string[] args) {

Is there an alternative for ShouldSerialize[PropertyName] in C#?

橙三吉。 提交于 2019-12-22 13:32:36
问题 I have been writing a lot of code lately which involves serialization using Json.NET and due to the nature of the data that I serialize, sometimes not all of their properties need to be serialized so, I do as follows... public int Foo { get; set; } public bool ShouldSerializeFoo() => Foo > -1; This's good and works but involves a lot of work if you have many properties ( in my case I have over 100 ). So, I wanted to know if there's an alternative to writing those methods. 回答1: One alternative

Is there an alternative for ShouldSerialize[PropertyName] in C#?

这一生的挚爱 提交于 2019-12-22 13:31:02
问题 I have been writing a lot of code lately which involves serialization using Json.NET and due to the nature of the data that I serialize, sometimes not all of their properties need to be serialized so, I do as follows... public int Foo { get; set; } public bool ShouldSerializeFoo() => Foo > -1; This's good and works but involves a lot of work if you have many properties ( in my case I have over 100 ). So, I wanted to know if there's an alternative to writing those methods. 回答1: One alternative

Custom inheritance JsonConverter fails when JsonConverterAttribute is used

戏子无情 提交于 2019-12-22 11:04:12
问题 I am trying to deserialize derived type, and I want to use a custom property Type to distinguish between derived types. [ { "Type": "a", "Height": 100 }, { "Type": "b", "Name": "Joe" } ] The solution I came to was to create a custom JsonConverter . On ReadJson I read the Type property and instantiate that type through the ToObject<T> function. Everything works fine until I use a JsonConverterAttribute . The ReadJson method loops infinitely because the attribute is applied on subtypes too. How

How to create a json string where a property contains a dot (period)?

半城伤御伤魂 提交于 2019-12-22 10:53:06
问题 I'm trying to send an HttpRequest that takes a JSON object like this: { "some.setting.withperiods":"myvalue" } I've been creating anonymous objects for my other requests, but I can't do that with this one since the name contains a dot. I know I can create a class and specify the [DataMember(Name="some.setting.withperiods")] attribute, but there must be a more lightweight solution. 回答1: There is no "easy" way to achieve this because the . in C# is reserved. However, you could achieve something

JSON.net, C#, unable to set default values in data model

做~自己de王妃 提交于 2019-12-22 10:14:50
问题 I am using JsonConvert.DeserializeObject(json.ToString()); to deserialise a JSON and populate my fields in a related model. This works well until there is data missing in the JSON. Basically instead of having "key : value" I will just have "key : null". My understanding about the JsonConverter was that it would create an object, in which I should be able to populate fields with a default value either through the constructor (not being called in my case for unknown reasons), or through the

How can I deserialize a child instance as a parent object without losing its specific property?

ぃ、小莉子 提交于 2019-12-22 09:57:32
问题 Below are several classes. How can I serialize a Json string of an instance of A in which PropertyB contains either SpecPropB1 or SpecPropB2 so that those properties remains in the C# object? public class A { public B PropertyB {get;set;} } public class B { public string GenProp {get;set;} } public class B1:B { public string SpecPropB1 {get;set;} } public class B2:B { public string SpecPropB2 {get;set;} } I see multiple solutions to this problem but they lack elegance: Put the type of

Azure Mobile App customizing json serialization

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 09:51:22
问题 I can't seem to be able to customize JSON serialization in an Azure Mobile App. To avoid the complexity of my own code, I setup a new project from scratch. Visual Studio Community 2015 Update 2, Azure App Service Tools v2.9 (if that matters). New Project, Visual C#, Cloud, Azure Mobile App. In App_Start\Startup.MobileApp.cs this is what's in the template: public static void ConfigureMobileApp(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); new MobileAppConfiguration()