json.net

how come BinaryFormatter can serialize an Action<> but Json.net cannot

試著忘記壹切 提交于 2019-12-08 13:37:14
问题 Trying to serialize/deserialize an Action<>. Try #1 naive by me JsonConvert.SerializeObject(myAction); ... JsonConvert.Deserialize<Action>(json); Deserialize fails saying it cannot serialize Action. Try #2 JsonConvert.DeserializeObject<Action>(ctx.SerializedJob, new JsonSerializerSettings {ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor }); Same(ish) failure. Try # 3 Then I found http://mikehadlow.blogspot.com/2011/04/serializing-continuations.html This uses

JSON.NET Line serialization winrt

守給你的承諾、 提交于 2019-12-08 13:04:27
问题 I'm having some trouble with serializing a ObservableCollection of Lines (Shape). I'm developing for Windows RT and I'm using JSON.NET v5.02. I'm getting the following exception for the code below: ObservableCollection<Line> lines; //some code string linesString = JsonConvert.SerializeObjectAsync(lines); // problem An exception of type Newtonsoft.Json.JsonSerializationException occurred in mscorlib.dll but was not handled in user code Additional information: Error getting value from 'X1' on

Resolving interfaces in WebAPI

蓝咒 提交于 2019-12-08 13:02:03
问题 I have a controller method with the following signature [HttpGet] [Route("results")] public List<IResult> GetResults() { return repo.GetResults(); } Not surprisingly, I get a JSON .NET exception saying JSON .NET can't resolve IResult to a concrete type. Is there a way to provide JSON .NET the concrete class (Result) so I don't have to change the singature of the method? 回答1: It seems like what you want to do isn't really dependency injection but instead you want to control how the data is

Convert JSON response to DataTable

♀尐吖头ヾ 提交于 2019-12-08 12:41:39
问题 I have the following example response provided in JSON. I'd like to be able to convert this to a C# DataTable so I can loop through the elements and program on from there. { "totalItems" : 10, "pageSize" : 20, "page" : 1, "items" : [ { "type" : "call", "uri" : "http://link.com", "created" : "2014-07-28T10:02:48.000+0000", "callType" : "external", "from" : "01234567891", "to" : "01234567892", "callId" : "ast01-1406541749.604847", "links" : { "recordings" : "http://link.com" } }, { "type" :

how to declare two arrays in json?

给你一囗甜甜゛ 提交于 2019-12-08 12:27:03
问题 I want to create a json string for my web application. Actually i am new to this json format.In my json string i have to create two arrays in my json structure.But i have some syntax problem in creating two arrays. my json string is given below for your reference. { MarkUpdate:[ { 'FinalMarks':[ { 'studentId':'S1', 'Ques_Mark':[ { 'qId' :'Q1', 'mark':'14', }, { 'qId':'Q2', 'mark':'10', } ] }, { 'studentId':'S2', 'Ques_Mark':[ { 'qId' :'Q1', 'mark':'12', }, { 'qId':'Q2', 'mark':'13', } ] } ] }

How to perform serialization and deserialization of datatables to and from JSON

≯℡__Kan透↙ 提交于 2019-12-08 12:21:16
问题 I am trying to find a way to both serialize and deserialize a dataset with one datatable to JSON and from JSON back to a dataset / datatable using JSON.NET, however all the samples and examples I have found are only serializing from a dataset or datatable to JSON and never two way. We have a system that deals with XML serialized datasets and datatables that we need to still retain in that format but allow certain UI views to render the data as JSON. Data can have null values and that's valid.

Applying [JsonIgnore] to property causes DataAnnotation attributes to be “lost”

自作多情 提交于 2019-12-08 11:52:18
问题 Given the following code: [JsonIgnore] [Display(Name = "Loan Cap")] public virtual string SomeProperty { get; protected set; } When the DataAnnotationsModelMetadataProvider.CreateMetadata is called by ASP.NET MVC to fulfil the following call: @Html.DisplayNameFor(model => model.SomeAttribute) The list of attributes handed to CreateMetadata does not include the [Display] attribute (or the [JsonIgnore] for that matter). If I remove the [JsonIgnore] then the [Display] gets found and passed to

Deserialize JSON, sometimes value is an array, sometimes “” (blank string)

為{幸葍}努か 提交于 2019-12-08 11:17:15
问题 I am trying to deserialize a field: "presenters":[{...},{...}] but some of the rows come back with only: "presenters":"" When the serializer gets to the row with that empty string I get: Error converting value "" to type 'System.Collections.Generic.List`1[DataPrototype.Model.Presenter]'. Am I right in thinking that I need a JsonConverter that will change the empty string into an empty List? 回答1: Yes. Inside the JsonConverter test the token type from the JsonReader. If it is a string then

Deserialization of JSON.Net returns 'null'

泄露秘密 提交于 2019-12-08 10:13:55
问题 I am using JSON.Net to Deserialize a JSON string. The JSON string is string testJson = @"{ ""Fruits"": { ""Apple"": { ""color"": ""red"", ""size"": ""round"" }, ""Orange"": { ""Properties"": { ""color"": ""red"", ""size"": ""round"" } } } }"; and my code is public class Fruits { public Apple apple { get; set; } public Orange orange { get; set; } } public class Apple { public string color { get; set; } public string size { get; set; } } public class Orange { public Properties properties { get;

How to cause a JSON property to be converted to XML as an attribute of an xml-element

感情迁移 提交于 2019-12-08 08:52:13
问题 Using Newtonsoft's .Net Library to convert JSON to XML, is there a way to convert a specific JSON element to an XML attribute? For example, taking the following JSON: { "array": { "item": [ 1, 2, 3 ], "length": 3 } } and converting it to: <array length="3"> <item>1</item> <item>2</item> <item>3</item> </array> Thanks. 回答1: Can you prefix the attributes with @ and put them at the top of the object? It says in the docs: Attributes are prefixed with an @ and should be at the start of the object.