json.net

how do I map this using Automapper

自闭症网瘾萝莉.ら 提交于 2019-12-12 02:15:02
问题 I am in need to map the below scenario. public class Customer { public string CustomerJson { get; set; } } public class CustomerTO { public object CustomerJson { get; set; } } From DAL I get CustomerJson value as below. Customer.CustomerJson = { "name": "Ram", "city": "India" } I am in need to Deserialize this string. so I tried the below stuff while mapping. var config = new MapperConfiguration(cfg => { cfg.CreateMap<Customer, CustomerTO>() .ForMember(dest => dest.CustName, opt => opt

JsonConvert.DeserializeXmlNode and Fortify Warns of JSON Injection

一世执手 提交于 2019-12-12 02:13:02
问题 Fortify is warning me that "JsonConvert.DeserializeXmlNode(JsonString);" could allow an attacker to inject arbitrary elements or attributes into the JSON entity. Json coming into my app is unfortunately dynamic, is there anything I can do to mitigate this? Is there a better method to convert my Json to XML? It says I can "ensure all serialization to JSON is performed using a safe serialization function that delimits untrusted data within single or double quotes and escapes any special

Serializing a DbGeography type in MVC

雨燕双飞 提交于 2019-12-12 02:05:00
问题 I've got a problem with serializing a DbGeography type. My controller action always gives an error instead of an object. For purpose of example, here is my class: public class Centre { public int CentreId { get; set; } [Required] public string Name { get; set; } [Required] [JsonConverter(typeof(DbGeographyConverter))] public DbGeography Location { get; set; } } My DbGeographyConverter class is defined as per many examples around: public class DbGeographyConverter : JsonConverter { private

Serialize and deserialize char(s)

核能气质少年 提交于 2019-12-12 01:48:57
问题 i have a list of chars on my class. Serialization and deserialization are works as expected. If my list contains which char is need to describe byte order mark. Example char code is 56256. So, created simple test to as this question is below. [Test] public void Utf8CharSerializeAndDeserializeShouldEqual() { UInt16 charCode = 56256; char utfChar = (char)charCode; using (MemoryStream ms = new MemoryStream()) { using (StreamWriter writer = new StreamWriter(ms, Encoding.UTF8, 1024, true)) { var

Serializing anonymous types with json.net seems broken

让人想犯罪 __ 提交于 2019-12-12 01:26:20
问题 Latest edits after realizing that the issue seems to be in RavenDb library instead of json.net library So far I have always used this piece of code to serialize anonymous types: using Raven.Imports.Newtonsoft.Json; var anon = new { errors = new string[] { "Login error" } }; var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; var str = JsonConvert.SerializeObject(anon, Formatting.None, settings); It was with json.net embedded inside

How to parse not fully corresponding json

試著忘記壹切 提交于 2019-12-12 01:16:50
问题 I am using third-party API, so I cannot change the response structure. In response I am getting something like this: { "Code": "SomeCode", "Name": "Some Name", "IsActive": true, "Prop13": { "LongId": "12", "ShortId": "45" }, "Prop26": { "LongId": "12", "ShortId": "45" }, "Prop756": { "LongId": "12", "ShortId": "45" } } I need to parse it to next type of class: public class Class1 { public string Code { get; set; } public string Name { get; set; } public bool IsActive { get; set; } public

Why are DriveInfo's properties missing when serializing to json string using Json.NET?

試著忘記壹切 提交于 2019-12-12 00:40:13
问题 Attempting to serialize DrivInfo to a Json string with this code only return the "name" property: DriveInfo dr = new DriveInfo("C"); string json = Newtonsoft.Json.JsonConvert.SerializeObject(dr); The string result is only: {"_name":"C:\"} DrivInfo is sealed so I cannot change anything. Is there a way to do it excluding wrapping? 回答1: The class contains its own custom serialization which specifies that only the _name field should be included. The other properties aren't stored in the class.

Undefined behaviour in Json

主宰稳场 提交于 2019-12-12 00:30:36
问题 What should happen when the value of a property is set to undefined in a json string. i.e: {"Name":undefined} The following example is using the json.net library. An exception is thrown when de-serializing the object. JsonConvert.DeserializeObject<SimpleObject>("{\"Name\":undefined}"); public class SimpleObject { public string Name { get; set; } } Newtonsoft.Json.JsonReaderException was unhandled Message=Error reading string. Unexpected token: Undefined. Path 'Value', line 1, position 18.

How to select JToken based on multiple name candidates in JSONPath?

社会主义新天地 提交于 2019-12-11 23:16:26
问题 I have a JObject like this: { name1: { value [ ... ] } } It may also be in the form of: { name2: { value [ ... ] } } So I'm trying to use a single JSONPath to select the JArray value out. Is there a way to do something like this? $['name1' or 'name2']['value'] 回答1: Based on the answers to these two SO questions: OR operator in JSONPath? and JsonPath AND Operator on Array, it seems AND and OR operators are not fully supported in JSONPath yet. However, we can use the union operator to

Customize Json.NET serialization to consider empty strings as null

主宰稳场 提交于 2019-12-11 20:19:55
问题 I have a stupid problem here. I'm developing a web application with ASP .NET MVC 4, EF5, and Ext JS (Sencha). The thing is, by design (I don't know why), Ext JS when doing ajax requests is converting js objects with null values to empty strings. I'll over simplify the example just for the sake of it. So if I have a variable in JavaScript icon_id : null When I get the JObject server-side it becomes icon_id : "" when I pass icon_id as the parameter for the request. So when I write icon_id