json.net

JSON.NET class name as root

让人想犯罪 __ 提交于 2019-12-24 10:56:23
问题 I'm working with ASP.NET MVC4 WebApi and Knockout.js. My model consists of Pages that contain 1-N Controls . When I need to load a new Page , I will fetch the HTML View and the JSON serialized object and put all together with KO. As my model is very dynamic, I'm going to use the KO mapping plugin so I don't have to define all the observables. Here is my (very simplified) model: public class Page { public string Name { get; set; } public List<Control> Controls { get; set; } } public abstract

How to selectively exclude a property from serialization based on the parent class type

北城余情 提交于 2019-12-24 10:38:42
问题 I'm using the Newtonsoft.JSON library to serialize several objects. In some cases I don't want to serialize a property so I've used the ShouldSerialize prefix which has been largely successful in most cases. In one case I only want to serialize a property if it belongs to a specific class. I've tried using the stack trace but it only tells me that the JSON object is calling the ShouldSerialize method. I don't need to know what calls ShouldSerialize, I need to know what parent class

Pass constructor arguments when deserializing into a List(Of T)

佐手、 提交于 2019-12-24 09:06:48
问题 I need to serialize and deserialize a List(of T) via JSON.Net, where T is an object which contains a reference which cannot be serialized. Here is a simplified version: Class MyObject Private ReadOnly _Parent As Word.Document Property Foo As String Property Bar As String Sub New(Parent As Word.Document, Foo As String, Bar As String) Me.New(Parent) Me.Foo = Foo Me.Bar = Bar End Sub Sub New(Parent As Word.Document) _Parent = Parent End Sub <JsonConstructor> Private Sub New() End Sub Function

Parse JSON where node has numeric value

自作多情 提交于 2019-12-24 08:57:44
问题 How do I parse the following JSON to get the value of TestCycleName . The challenge is in identifying the root node since it starts with a number. My implementation will be in C# using JSON.net { "URL": "rest/zapi/latest/cycle?projectId=##projectId##&versionId=##versionId##", "Method": "GET", "Parameters": { "1": { "VersionName": "Custom Pipes Development", "TestCycleName": "SetMaxFutureDateFromCustomerField_Mobile" }, "2": { "VersionName": "Recurring payments 1.5", "TestCycleName": "Internet

Google speech to text API using C#

我是研究僧i 提交于 2019-12-24 08:34:14
问题 I'm using google speech recognition for speech to text of a audio file. Response i get as output shows only {"result":[]}. I don't see any output result. i have picked my code from How to use google speech recognition api in c#? and Google speech to text API in C# . i have tried almost every answer in above links still i am getting the error. My code is: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using

What am I doing wrong with JSON.NET's JsonConvert

有些话、适合烂在心里 提交于 2019-12-24 08:06:13
问题 json string { "success": true, "challenge_ts": "2016-11-03T17:30:00Z", "hostname": "mydomain.com" } class internal class reCaptchaResponse { internal bool success { get; set; } internal DateTime challenge_ts { get; set; } // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ) internal string hostname { get; set; } // the hostname of the site where the reCAPTCHA was solved internal string[] error_codes { get; set; } // optional } attempt to serialize reCaptchaResponse

Deserializing nodes presenting as different types

假如想象 提交于 2019-12-24 08:05:27
问题 I am looping over a folder of JSON files, and I am trying to pull out some information from them; however, I am finding it greatly difficult to do so. I have started to build my objects to deserialize. I have a particular node I would normally deserialize as an object, but when it's empty, it presents as an empty array. Please see the definition field in the example JSON below: { "name": "Example", "description": "Example JSON", "properties": { "foo": "bar", "foo1": "bar2", "foo3": "bar4" },

In JSON.NET how to get a reference to every deserialized object?

不问归期 提交于 2019-12-24 08:02:52
问题 I'm attempting to implement IDeserializationCallback using JSON.NET. I'm deserializing an object, and I would like to generate a list of all the objects which were deserialized which implement IDeserializationCallback, what would be the best way to do this? Does JSON.NET have any appropriate extension point to facilitate this? I have a (seemingly) working solution below, however it is quite ugly, so I'm convinced there must be a better way to do this. Any help is appreciated, thanks! private

C# Serialize with JSON.NET inherited private fields

こ雲淡風輕ζ 提交于 2019-12-24 07:59:28
问题 I have an object structure (in external dll) like this: public class Demo2 { private int count; public Demo2() { count = 2; } } public class MyDemo : Demo2 { private int count; public MyDemo() { count = 3; } } public class Perform { static void Main(string[] args) { MyDemo d = new MyDemo(); String json = JsonSerializer.SerializeOnce<MyDemo>(d); Console.WriteLine(json); /* print: {count: 3} */ } } I need something this: "{count: 3, base: {count:2}}". And Deserialize later 回答1: Assuming your

StackOverflow exception when using custom JsonConverter and custom ContractResolver

偶尔善良 提交于 2019-12-24 07:49:32
问题 My requirement is use JsonProperty during de-serializing and ignore JsonProperty during serialization. My model, [JsonConverter(typeof(JsonPathConverter))] public class FacebookFeed { public FacebookFeed() { Posts = new List<FacebookFeedPost>(); } [JsonProperty("name")] public string Name { get; set; } [JsonProperty("fan_count")] public int Likes { get; set; } [JsonProperty("feed.data")] public List<FacebookFeedPost> Posts { get; set; } } public class FacebookFeedPost { [JsonProperty("id")]