json.net

External json vulnerable because of Json.Net TypeNameHandling auto?

大憨熊 提交于 2019-12-17 06:48:21
问题 I'm operating a small website where users can upload custom "objects" defined in JSON. Recently I've learned about possible threats using JSON with automatic type deserialization: JSON problem. I think I understand the problematics but I have to ask to be sure. If I only deserialize the incoming JSON with a given specific type (here MyObject ) JsonConvert.DeserializeObject<MyObject>(json, settings); and no type inside MyObject and no subtype of any member of MyObject has the type System

Convert Newtonsoft.Json.Linq.JArray to a list of specific object type

风流意气都作罢 提交于 2019-12-17 06:22:24
问题 I have the following variable of type {Newtonsoft.Json.Linq.JArray} . properties["Value"] {[ { "Name": "Username", "Selected": true }, { "Name": "Password", "Selected": true } ]} What I want to accomplish is to convert this to List<SelectableEnumItem> where SelectableEnumItem is the following type: public class SelectableEnumItem { public string Name { get; set; } public bool Selected { get; set; } } I am rather new to programming and I am not sure whether this is possible. Any help with

How to call JsonConvert.DeserializeObject and disable a JsonConverter applied to a base type via [JsonConverter]?

↘锁芯ラ 提交于 2019-12-17 06:18:22
问题 EDIT: Clarify question: I have overridden the JsonConverter for a base type (by applying [JsonConverter(typeof(TConverter))] to the superclass), but when deserializing the sub-type directly I want to use STANDARD serialization (i.e. no custom converter) for deserializing my derived object. How do I specify STANDARD serialization for use in the deserialize method, as if I had NOT overridden the JsonConverter? I am using elastic search and can't call JsonConvert.DeserializeObject with my custom

Json.NET serialize by depth and attribute

限于喜欢 提交于 2019-12-17 06:18:10
问题 For example we have two classes class FooA { [SomeSpecialAttribute] public int SomeValueA { get; set; } public int SomeValueB { get; set; } public int SomeValueC { get; set; } } class FooB { public FooA FooA { get; set; } } I use Json.NET, max depth is 1. While serializing FooA it should output all properties as usual, but while serializing FooB it should output only one FooA's property which has special attribute. So only while resolving nested reference properties (Depth > 0) we should get

Ensuring json keys are lowercase in .NET

自作多情 提交于 2019-12-17 05:38:10
问题 Is there simple way using JSON in .NET to ensure that the keys are sent as lower case? At the moment I'm using the newtonsoft's Json.NET library and simply using string loginRequest = JsonConvert.SerializeObject(auth); In this case auth is just the following object public class Authority { public string Username { get; set; } public string ApiToken { get; set; } } This results in {"Username":"Mark","ApiToken":"xyzABC1234"} Is there a way to ensure that the username and apitoken keys come

Can I serialize nested properties to my class in one operation with Json.net?

最后都变了- 提交于 2019-12-17 05:13:14
问题 Lets say I have a model like: public class MyModel { public string Name { get; set; } public string[] Size { get; set; } public string Weight { get; set; } } And Json like this: { "name" : "widget", "details" : { "size" : [ "XL","M","S", ] "weight" : "heavy" } } I have been trying to work out a way to serialize my object without making one model for the "name" and one model for the "details" as this doesn't map nicely to my database so involves a little juggling to get class populated. I can

Using JSON.NET to return ActionResult [duplicate]

狂风中的少年 提交于 2019-12-17 04:26:31
问题 This question already has answers here : Json.Net And ActionResult (2 answers) Closed 2 years ago . I'm trying to write a C# method that will serialize a model and return a JSON result. Here's my code: public ActionResult Read([DataSourceRequest] DataSourceRequest request) { var items = db.Words.Take(1).ToList(); JsonSerializerSettings jsSettings = new JsonSerializerSettings(); jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; var converted = JsonConvert.SerializeObject(items,

How can I return camelCase JSON serialized by JSON.NET from ASP.NET MVC controller methods?

血红的双手。 提交于 2019-12-17 03:52:25
问题 My problem is that I wish to return camelCased (as opposed to the standard PascalCase) JSON data via ActionResults from ASP.NET MVC controller methods, serialized by JSON.NET. As an example consider the following C# class: public class Person { public string FirstName { get; set; } public string LastName { get; set; } } By default, when returning an instance of this class from an MVC controller as JSON, it'll be serialized in the following fashion: { "FirstName": "Joe", "LastName": "Public" }

Json.NET serialize object with root name

我们两清 提交于 2019-12-17 03:08:12
问题 In my web app I'm using Newtonsoft.Json and I have following object [Newtonsoft.Json.JsonObject(Title = "MyCar")] public class Car { [Newtonsoft.Json.JsonProperty(PropertyName = "name")] public string Name{get;set;} [Newtonsoft.Json.JsonProperty(PropertyName = "owner")] public string Owner{get;set;} } and I want serialize them with root name (class name). This is desired format using {'MyCar': { 'name': 'Ford', 'owner': 'John Smith' } } I know that I can do that with anonymous object, but is

How to set Json.Net as the default serializer for WCF REST service

余生颓废 提交于 2019-12-17 02:13:04
问题 Is it possible to override the default WCF DataContractSerializer behaviour when Serialize/DeSerialize entities and use JSON.NET instead? I have the following service contract for handling the City entity. For design reasons the City entity has IsReference=true, and therefore the default DataContractSerializer raise errors. For the "GET" methods I can handle the situation with JsonConvert.DeserializeObject, but with "PUT,POST,DELETE" methods DataContractSerializer takes precedence and fails