json.net

Serialize instance of a class deriving from DynamicObject class

拈花ヽ惹草 提交于 2019-12-10 19:05:00
问题 I have a class that derives from DynamicObject class. On calling JsonConvert.SertializeObject, none of the dynamic properties are serialized. Class is defined as, public class Component : DynamicObject { // The inner dictionary. public Dictionary<string, object> dictionary = new Dictionary<string, object>(); [JsonProperty(PropertyName = "id")] public string Id { get; set; } // If you try to get a value of a property // not defined in the class, this method is called. public override bool

When using a JsonWriter, what's the purpose of WriteStartConstructor?

£可爱£侵袭症+ 提交于 2019-12-10 18:26:20
问题 Title says it all. I see it (with its corresponding end) spits out the following... new Foo( ) ...but I don't understand what the new actually does when deserializing. The docs just say it writes a Json constructor, but not what a Json constructor is ! 回答1: This method was introduced as part of an enhancement to allow Json.NET to parse date literals when represented with JavaScript constructors like so: new Date(1234656000000). For details, see Serializing Dates in JSON. Even though this

Set the comparer for Newtonsoft.Json.JsonConvert to use for HashSet/Dictionary

為{幸葍}努か 提交于 2019-12-10 18:23:30
问题 I have a HashSet<string> that JsonConvert.SerializeObject serialises to an array. When I deserialise using JsonConvert.DeserializeObject<HashSet<string>> I get a new HashSet<string> with the same values. However, the Comparer has been reset. // JSON settings var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; // Create a case insensitive hashset var h = new HashSet<string>(new string[] {"A", "b"}, StringComparer.OrdinalIgnoreCase); h

Deserializing JSON Without Unique Property Name

痴心易碎 提交于 2019-12-10 18:13:33
问题 I have a json string that looks like the following { "1ST": { "name": "One", "symbol": "1st" }, "2ND": { "name": "Two", "symbol": "2nd" } } Im trying to Serialize it down to a C# object. It looks like it is creating a Dictionary, so i have created the following structure public class Response { public Dictionary<string, Item> Objects { get; set; } } public class Item { public string name { get; set; } public string symbol { get; set; } } And during serialization running the following response

Json.net no longer throws in case of duplicate

送分小仙女□ 提交于 2019-12-10 18:09:51
问题 I'm trying to upgrade my C# application from Newtonsoft.JSON 6 to the latest version (9.0.1). I noticed a change of behavior when deserializing object containing duplicate elements, such as : { "name": "test", "data": { "myElem": 1, "myElem": 2 } } When deserializing such object, Json.net was previously throwing an ArgumentException. Now the deserialization succeed and it seems that it uses the value of the last duplicated key (hence "2" in above example). From what I read, there is some

C# format JSON with backslash '\' in value

佐手、 提交于 2019-12-10 18:09:07
问题 I have some JSON from a third party system that contains backslashes in the value. For example: string extract = @"{""key"": ""\/Date(2015-02-02)\/""}"; which without the c# string escaping corresponds to the string: {"key": "\/Date(2015-02-02)\/"} I'd like to be able to format (e.g. indent) this JSON. Typically for formatting, I might use something like JsonConvert like so: JsonConvert.SerializeObject(JsonConvert.DeserializeObject(extract), Formatting.Indented) This doesn't quite work, as it

Serialize into a key-value dictionary with Json.Net?

拥有回忆 提交于 2019-12-10 18:05:20
问题 Hello I'm trying to serialize an object into a hash, but I'm not getting quite what I want. Code: class Data{ public string Name; public string Value; } //... var l=new List<Data>(); l.Add(new Data(){Name="foo",Value="bar"}); l.Add(new Data(){Name="biz",Value="baz"}); string json=JsonConvert.SerializeObject(l); when I do this the json result value is [{"Name":"foo","Value":"bar"},{"Name":"biz","Value":"baz"}] The result I want however is this: [{"foo":"bar"},{"biz":"baz"}] How do I made the

Flattening out a JToken

点点圈 提交于 2019-12-10 17:55:33
问题 Suppose I have the following JToken: @"{ ""data"": [ { ""company"": { ""ID"": ""12345"", ""location"": ""Some Location"" }, ""name"": ""Some Name"" } ] }"; I want to pass this token into a FlattenToken function that outputs this JToken: @"{ ""data"": [ { ""company_ID"": ""12345"", ""company_location"": ""Some Location"", ""name"": ""Some Name"" } ]}" The reason for doing this is so that I can then take the flattened JToken and deserialize it into a DataTable. I'm getting lost in a jumble of

JSON array to ExpandoObject via JSON.NET

穿精又带淫゛_ 提交于 2019-12-10 17:47:40
问题 I am using the following approach to convert most of my API JSON results into an object: public void ExpandoObject() { var sampleDATA = Sample.Create(); var json = JsonConvert.SerializeObject(sampleDATA); var expConverter = new ExpandoObjectConverter(); dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, expConverter); var a = obj.A; var b = obj.B; var c = obj.C; //and so on... } However, I run into a strange situation with this format of JSON... [ { "id": 42, "name": "example

Automatically retun mongodb ObjectId as string with Json.NET in MVC

╄→尐↘猪︶ㄣ 提交于 2019-12-10 17:36:59
问题 I have a MVC .net project, and I am using mongodb. In some of my controller I return JsonResult with ObjectId. I want the ObjectId to be serialized as string. I found a similar problem and used this answer, using a custom JsonConverter : JSON.NET cast error when serializing Mongo ObjectId However when i try to return a JsonResult (using return Json(myObject) ) the JsonConverter is not called at all. When i return a string using return JsonConvert.SerializeObject(myObject); the JsonConverter