json.net

Prevent serialization if value is null or whitespace in JSON.NET

落爺英雄遲暮 提交于 2019-12-24 00:08:42
问题 I have an object that needs to be serialized in such a way that both null and "whitespace" (empty or just spaces) values are not serialized. I don't control the object itself and therefore can't set attributes, but I know that all the properties are strings. Setting NullValueHandling to Ignore obviously only gets me part of the way to the solution. It "seems" (as best I understand) like what I need to do is create a custom DefaultContractResolver but I haven't come up with a solution that

Windows Azure Deployment - not load file or assembly 'Newtonsoft.Json'

妖精的绣舞 提交于 2019-12-23 23:51:06
问题 I created MVC 4 application with Web API. It works good in local environment. When I deployed it to Azure, it gives following error. Exception information: Exception type: HttpException Exception message: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) at System.Web

mvc return JsonArray as Json

放肆的年华 提交于 2019-12-23 23:37:37
问题 I'm trying to return a dynamic json array to the client side in mvc. So far I have var a = 1; var b = 10; var jsonArray = new JArray(); for (var i = 1; i < 5; i++) { var json = new JObject(); json.Add("field" + a, b); jsonArray.Add(json); a++; b++; } return Json(jsonArray); this returns to the client [[[[]]]] I have tried converting the JsonArray to a string first and setting it to have no formatter, but that doesn't return valid json according to fiddler. I'd expect the result to be

parsing nested json with json.net

我只是一个虾纸丫 提交于 2019-12-23 21:22:10
问题 I have problems with json deserialization , below is my json { "_id" : ObjectId("56bc28c436b252c406a67f17"), "empname": "dhiraj", "empcode": "123a", "level": { "levelID": 3, "levelDescription": "manager", "levelCode": "mg" }, "Address": [ { "Home": { "streetname": "Home", "city": "bbb", "state": "aaa" } }, { "Office": { "streetname": "ofc", "city": "ccc", "state": "ddd" } } ] } And for above json the object classes are like public class Employee { public ObjectId _id { get; private set; }

How can I implement custom serialisation of json appending the types to the property names?

纵然是瞬间 提交于 2019-12-23 19:11:25
问题 I am trying to serialize an object to JSON using newtonsoft.json. The only thing is that I cannot append the json type to the field name. Consider this example: var item = new { value = "value", data = new []{"str", "str"}, b = true }; I want to convert that to { "value.string" : "value", "data.array" : ["str", "str"], "b.bool" : true } or something similar. The idea is to append the json type (not the c# type) to the json field. The reason I don't want to append the C# type is because it

json.net: how to deserialize a type in a dynamically loaded assembly?

耗尽温柔 提交于 2019-12-23 18:44:51
问题 I'm dynamically loading an assembly using Assembly.LoadFrom() , then instantiating some of its types using .CreateInstance() . Next, I put these objects into an array and serialize it to a file using json.net (configured with TypeNameHandling.Auto ). In the file I can see that it is storing the correct type names, e.g.:- "Features": [{ "$type": "FeaturesAssembly.SomeFeature, FeaturesAssembly", // Other serialized properties }] The problem is that I can't deserialize the file. Json.net throws

How to make Newtonsoft.Json.Linq.JObject immutable?

爱⌒轻易说出口 提交于 2019-12-23 16:01:05
问题 I can create JObject var jobject = Newtonsoft.Json.Linq.JObject.Parse(jsonstring); I want to convert the jobject read only so that no new keys can be added or existing values modified. 回答1: It can't be done. There is an open issue for implementing it: https://github.com/JamesNK/Newtonsoft.Json/issues/468 But it is two years old and has drawn very little attention as far as I can tell. 回答2: An immutable object is one that can't be changed. If you don't want consumers of your JObject to change

Newtonsoft Json Deserlize as C# Datagridview

被刻印的时光 ゝ 提交于 2019-12-23 15:14:13
问题 I have some issues using the Newtonsoft Json Plugin. I want to fill a datagridview using Json but dont know how. In the Documentation of Newtonsoft Json i get an exmaple with datatable but if i try this sample i just get Errors. This is my Json: [ { "id": "17", "name": "Filename", "author": "unknown", "size": "3.1MB", "pfad": "ftp://path/Filename", "Filetoken": "6747rzuzur6urzut766754677" }, { "id": "20", "name": "Filename", "author": "unknown", "size": "3.1MB", "pfad": "ftp://path/Filename",

How to use a default value for JSON.net for properties with invalid values

一笑奈何 提交于 2019-12-23 13:00:03
问题 I am using the Newtonsoft JSON library to deserialize a response from a web service. The problem is that some of the fields contain values which are not valid. For example, one field on one record contained a "T" for a field that is supposed to be numeric. What I would like to do is to have values for fields that are invalid be null, or some other default value. All of my properties are defined as nullable, so it is fine if they default to null. Is there a way to do this? I have tried

How to “inline” a property in resulting json with json.net

浪子不回头ぞ 提交于 2019-12-23 12:45:06
问题 I have a property in one of my classes that I'm trying to serialize with json.net that I would like to "inline", meaning, I don't want to have the property be nested into an element with the property name, but its content be directly within its parent. Here's an example, let say I have the following class structure: public interface ISteeringWheelIdentifier {} public interface ISteeringWheel { ISteeringWheelIdentifier Identifier {get;} } public class ManufacturerIdentifier :