json.net

Should I try to take BadRequest(ModelState) returned from my API, and deserialize to *what* with JSON.NET?

假如想象 提交于 2019-12-24 20:25:20
问题 TL;DR; "I like how my generated AutoRest client deserializes my main entities when dealing with the 200 scenarios.. but, MUST I manually parse the 400 scenarios?", said the lazy programmer DETAILS: So, I have an API, (Web API 2), and I do all the standard stuff.. using POCO's that implement IValidatable in addition to property-level validation using System.Data.DataAnnotations my Web API returns 400 errors like this (just an example): if (!ModelState.IsValid) { return BadRequest(ModelState);

Serialize an object's properties to separate JSON objects in an array

穿精又带淫゛_ 提交于 2019-12-24 17:53:24
问题 I want to serialize properties but with each property enclosed in a separate JSON object in an array. Example below: public class Metadata { public string Name { get; set; } public Guid Id { get; set; } } public void SerializeCars() { var data = new Metadata { Name = "MyName", Id = Guid.NewGuid() }; var json = JsonConvert.SerializeObject(data, Formatting.Indented); } Current result will be: { "Name": "MyName", "Id": "f9c4bc06-0b99-47ff-b22b-ea094fc188ee" } I want it to be (missing "td" class

Trying to hide base class member during serialization with Json.NET

╄→гoц情女王★ 提交于 2019-12-24 17:43:56
问题 I have two classes, where the concrete class Model<T> hides the base class' Items property. class Model { List<ListItem> Items {get;set;} } class Model<T> : Model { new List<ListItem<T>> Items {get;set;} } Upon serializing an instance of Model<T> with Json.NET I get the error: Newtonsoft.Json.JsonSerializationException : A member with the name ' Items ' already exists on ' Model<T> '. Use the JsonPropertyAttribute to specify another name. I understand why I'm receiving this error, however, I

Modify the behaviour of JSON.NET to serialize a collection of objects to an array of IDs

谁说我不能喝 提交于 2019-12-24 16:31:44
问题 I want to modify JSON.NET so that when I am serializing a Model from my API it sends only an array of IDs for a composite Collection object. For example: class Employee { public ICollection<Address> Addresses { get; set; } } class Address { public int id; public string location; public string postcode; } Then when I send that back through WebApi Request.Createresponse(HttpStatusCode.OK, new Employee()); Instead of this: { "Addresses" : [ {"id" : 1, "location" : "XX", "postcode" : "XX" }, {"id

Nested nodes are not formatted correctly after implementing JsonConverter

老子叫甜甜 提交于 2019-12-24 16:29:50
问题 I have a list of objects that I want to serialize as JSON like below, since there is a complex type in this list. I want to change this complex type to Key/Value pairs where each Key is the name of a property in the type, and each Value is the corresponding value of that property. I've tried multiple solutions but none of them worked for me. Here is the object structure public class Metadata { public string FirstName { get; set; } public string LastName { get; set; } } public class Data { //

Why is a complex string (stringified json data) not accepted as a POST parameter to a Web API method, but a simple string is?

核能气质少年 提交于 2019-12-24 14:53:16
问题 I want to send a bunch of data, converted to json, as a string, to a Web API POST method. I can send a simple string just fine, but when I try to send stringified json data, the method is not even reached - apparently the complex string is not viewed as a valid string value or something. This works, when passing "randomString" from the client: Web API [Route("{unit}/{begindate}/{enddate}/{stringifiedjsondata}")] [HttpPost] public void Post(string unit, string begindate, string enddate, string

JSON.net null property

二次信任 提交于 2019-12-24 13:30:26
问题 I recently solved this error: Type is an interface or abstract class and cannot be instantiated , by using the solution suggested here: Using Json.NET converters to deserialize properties. Unfortunately I am getting new issues with this. In the code below, Depth gets called as part of the deserialisation, which in turn calls the Prof property which fails because 'this.Profile' is null. ('this.Profile' is a property inherited from the Section class.) public class TimberSection : Section,

Force root xml element to be array on json conversion

北战南征 提交于 2019-12-24 11:30:52
问题 I am using below (http://james.newtonking.com/projects/json) to force XML nodes to be an array when converted to JSON: <person xmlns:json='http://james.newtonking.com/projects/json' id='1'> <name>Alan</name> <url>http://www.google.com</url> <role json:Array='true'>Admin</role> </person> and what I get is { "person": { "@id": "1", "name": "Alan", "url": "http://www.google.com", "role": [ "Admin" ] } } What I wanted is { "person": [ { "@id": "1", "name": "Alan", "url": "http://www.google.com",

Allow multiple values for the same key when creating JSON using ExpandoObject and IDictionary

梦想的初衷 提交于 2019-12-24 11:21:40
问题 I am trying to create dynamic JSON using ExpandoObject and IDictionary. During the dynamic creation of JSON there could be instances when the Name or Value would repeat. However when adding the repeated Name or Value to the ExpandoObject, it gives an error: An item with the same key has already been added. Below is my code snippet : DataTable dt_MappedColumns = (DataTable)ViewState["MappedColumns"]; dynamic ManCols = new ExpandoObject(); var dictionary1 = (IDictionary<string, object>)ManCols;

Preserve remove trailing zeros in Newtonsoft.Json

这一生的挚爱 提交于 2019-12-24 11:12:00
问题 I have problem with Newtonsoft.JsonConverter in my C# app. I have decimal with trailing zeros. After convert to json - there zeros are missing. Example: input (decimal): 1.99000 output (json): 1.99 I have my own converter, that handles decimal value. Also, I configured FloatParseHandling as decimal. What can I do to preserve this process? 回答1: This was a bug introduced in Json.NET 10.0.1 and reported here. It has since been fixed in 11.0.1. 来源: https://stackoverflow.com/questions/46684557