json.net

Filtering Out Properties in an ASP.NET Core API

守給你的承諾、 提交于 2019-12-30 21:37:30
问题 I want to serve up the following JSON in my API: { "id": 1 "name": "Muhammad Rehan Saeed", "phone": "123456789", "address": { "address": "Main Street", "postCode": "AB1 2CD" } } I want to give the client the ability to filter out properties they are not interested in. So that the following URL returns a subset of the JSON: `/api/contact/1?include=name,address.postcode { "name": "Muhammad Rehan Saeed", "address": { "postCode": "AB1 2CD" } } What is the best way to implement this feature in ASP

REST API wrapper design: returning dynamic json as JSON.NET JObject / JArray

拥有回忆 提交于 2019-12-30 21:01:33
问题 I am writing a C# wrapper for a RESTful JSON API, and using Json.NET to deserialize the incoming json to strongly typed object. but a few properties in the incoming json are highly dynamic, it will be some json object with different number and type of properties. My current solution is, I mapped the dynamic json property to Dictionary<string, object> (Nested dictionary) in my C# class,and write Custom JsonConverter to convert dynamic json to Nested dictionary. My class look like this: public

REST API wrapper design: returning dynamic json as JSON.NET JObject / JArray

拈花ヽ惹草 提交于 2019-12-30 21:01:31
问题 I am writing a C# wrapper for a RESTful JSON API, and using Json.NET to deserialize the incoming json to strongly typed object. but a few properties in the incoming json are highly dynamic, it will be some json object with different number and type of properties. My current solution is, I mapped the dynamic json property to Dictionary<string, object> (Nested dictionary) in my C# class,and write Custom JsonConverter to convert dynamic json to Nested dictionary. My class look like this: public

Json.NET serializes enum as string even with default settings

社会主义新天地 提交于 2019-12-30 19:57:26
问题 I am using Json.NET 7.0.1. The documentation says that Enum [is serialized as] Integer (can be the enum value name with StringEnumConverter) In my Global.asax.cs, I specify the default settings as follows: JsonConvert.DefaultSettings = (() => { var settings = new JsonSerializerSettings(); settings.Converters.Add(new StringEnumConverter()); return settings; }); However, in certain situations, I want Enums to be serialized as integers, for instance when I build a JSON which is to be stored in

Json.NET serializes enum as string even with default settings

你说的曾经没有我的故事 提交于 2019-12-30 19:55:54
问题 I am using Json.NET 7.0.1. The documentation says that Enum [is serialized as] Integer (can be the enum value name with StringEnumConverter) In my Global.asax.cs, I specify the default settings as follows: JsonConvert.DefaultSettings = (() => { var settings = new JsonSerializerSettings(); settings.Converters.Add(new StringEnumConverter()); return settings; }); However, in certain situations, I want Enums to be serialized as integers, for instance when I build a JSON which is to be stored in

Can I make a strict deserialization with Newtonsoft.Json?

一曲冷凌霜 提交于 2019-12-30 17:57:25
问题 I am using Newtonsoft.Json to serialize/deserialize objects. As far as I know a deserialization can not be successful if the class does not have parameterless constructor. Example, public class Dog { public string Name; public Dog(string n) { Name = n; } } For this class below code generates the object correctly. Dog dog1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Dog>("{\"Name\":\"Dog1\"}"); For me, surprisingly it generates the object correctly with below codes also. Dog dog2 =

Can I make a strict deserialization with Newtonsoft.Json?

孤街浪徒 提交于 2019-12-30 17:57:04
问题 I am using Newtonsoft.Json to serialize/deserialize objects. As far as I know a deserialization can not be successful if the class does not have parameterless constructor. Example, public class Dog { public string Name; public Dog(string n) { Name = n; } } For this class below code generates the object correctly. Dog dog1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Dog>("{\"Name\":\"Dog1\"}"); For me, surprisingly it generates the object correctly with below codes also. Dog dog2 =

Bson array (de)serialization with Json.NET

萝らか妹 提交于 2019-12-30 17:39:11
问题 I am simply trying to serialize and deserialize a string array in Bson format using Json.NET, but the following code fails: var jsonSerializer = new JsonSerializer(); var array = new string [] { "A", "B" }; // Serialization byte[] bytes; using (var ms = new MemoryStream()) using (var bson = new BsonWriter(ms)) { jsonSerializer.Serialize(bson, array, typeof(string[])); bytes = ms.ToArray(); } // Deserialization using (var ms = new MemoryStream(bytes)) using (var bson = new BsonReader(ms)) { //

Could not determine JSON object type for type “Class”

孤者浪人 提交于 2019-12-30 16:26:15
问题 I got the following error while trying to add an object of type class to the JArray . Could not determine JSON object type for type "Class" Here is the code that I am using: private dynamic _JArray = null private JArray NArray(Repository repository) { _JArray = new JArray(); string[] amounts = repository.Amounts.Split('|'); for (int i = 0; i <= amounts.Length; i++) { _JArray.Add( new AmountModel { Amounts = amounts[i], }); } return _JArray; } public class AmountModel { public string Amounts;

How to do Conditional Serialization using C# - NewtonSoft.Json

时光总嘲笑我的痴心妄想 提交于 2019-12-30 13:42:28
问题 I am doing json serialization using NewtonSoft.Json public class CommonBase { [JsonProperty(PropertyName = "u_customer_id")] public long CustomerId { get; set; } } I want to do a conditional serialization so that if CustomerId value is 0, I want to set a blank value for CustomerId during json serialization. Since CommonBase is a base class and I am not able to change data type from long to string . How can I achieve this? 回答1: You almost have the answer in your question title. What you are