json.net

Determine type during json deserialize

牧云@^-^@ 提交于 2019-12-09 12:52:05
问题 I'm working on a protocol in which the receiver will receive json messages of certain specified custom types (currently 5, but could be 10-20). I'm struggling to come up with an optimal/fast solution which will automatically deserialize the json and return the correct type of object. Example: public class MessageA { public string Message; } public class MessageB { public int value; } public class MessageC { public string ValueA; public string ValueB; } Ideally, the method should be like

Serialize json to an object with catch all dictionary property

让人想犯罪 __ 提交于 2019-12-09 10:23:54
问题 I would like to use JSON.net to deserialize to an object but put unmapped properties in a dictionary property. Is it possible? For example given the json, {one:1,two:2,three:3} and the c# class: public class Mapped { public int One {get; set;} public int Two {get; set;} public Dictionary<string,object> TheRest {get; set;} } Can JSON.NET deserialize to an instance with values one=1, two=1, TheRest= Dictionary{{"three,3}} 回答1: The easiest way to do this is to use the JsonExtensionData attribute

JSON Serialisation - How to flatten nested object structures using JSON.net/Newtonsoft

心不动则不痛 提交于 2019-12-09 07:41:29
I have the following JSON structure : { "Name":"", "Children":[ { "ID":"1", "MetaData":[ { "Info":{ "GUID":"cdee360d-7ea9-477d-994f-12f492b9e1ed" }, "Data":{ "Text":"8" }, "Name":"DataId" } ] } ] } and I want to flatten the MetaData and nested Info and Data objects in the array. I also want to use the Name field as a field name for Text value so it becomes "DataId" : "8". { "Name":"", "Children":[ { "ID":"1", "GUID":"cdee360d-7ea9-477d-994f-12f492b9e1ed", "DataId":"8" } ] } So far I've used a Contract Resolver to get me so far: private class DynamicContractResolver : DefaultContractResolver {

JSON.NET Deserialize objects within object / array of objects

a 夏天 提交于 2019-12-09 07:08:55
问题 I have a situation where an API I'm using is returning inconsistent JSON, which I want to deserialize using JSON.NET. In one case, it returns an object that contains objects (note that the outer "1" can be any number): { "1":{ "0":{ "db_id":"12835424", "title":"XXX" }, "1":{ "db_id":"12768978", "title":"YYY" }, "2":{ "db_id":"12768980", "title":"ZZZ" }, "3":{ "db_id":"12768981", "title":"PPP" } } } And in another case, it returns an array of objects: { "3":[ { "db_id":"12769199", "title":"XXX

Why does Json.NET DeserializeObject change the timezone to local time?

这一生的挚爱 提交于 2019-12-09 05:00:37
问题 I'm using json.net to deserialize a DateTimeOffset , but it is ignoring the specified timezone and converting the datetime to the local offset. For example, given var content = @"{""startDateTime"":""2012-07-19T14:30:00+09:30""}"; When deserialised using: var jsonSerializerSettings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind }; var obj =

Serialize an object directly to a JObject instead of to a string in json.net

扶醉桌前 提交于 2019-12-09 04:39:05
问题 How might one serialize an object directly to a JObject instance in JSON.Net? What is typically done is to convert the object directly to a json string like so: string jsonSTRINGResult = JsonConvert.SerializeObject(someObj); One could then deserialize that back to a JObject as follows: JObject jObj = JsonConvert.DeserializeObject<JObject>(jsonSTRINGResult); That seems to work, but it would seem like this way has a double performance hit (serialize and then deserialize). Does SerializeObject

Get Length of array JSON.Net

若如初见. 提交于 2019-12-09 04:29:59
问题 How can I get the length of a JSON Array I get using json.net in C#? After sending a SOAP call I get a JSON string as answer, I use json.net to parse it. Example of the json I got: {"JSONObject": [ {"Id":"ThisIsMyId","Value":"ThisIsMyValue"}, {"Id":"ThisIsMyId2","Value":"ThisIsMyValue2"} ]} And I parse it and write it in console: var test = JObject.Parse (json); Console.WriteLine ("Id: {0} Value: {1}", (string)test["JSONObject"][0]["Id"], (string)test["JSONObject"][0]["Value"]); This works

Deserializing json into a list of objects - Cannot create and populate list type [duplicate]

可紊 提交于 2019-12-09 03:09:28
This question already has answers here : Json.net serialization of custom collection implementing IEnumerable<T> (2 answers) Closed 3 years ago . I'm trying to deserialize json (here is link to it http://antica.e-sim.org/apiFights.html?battleId=3139&roundId=1 ) into a list of objects. This is my class: public class RootObject { public int Damage { get; set; } public int Weapon { get; set; } public bool Berserk { get; set; } public bool DefenderSide { get; set; } public double MilitaryUnitBonus { get; set; } public int Citizenship { get; set; } public int CitizenId { get; set; } public bool

How to determine which constructor Autofac uses when resolving

梦想的初衷 提交于 2019-12-09 03:08:29
I'm using a custom JsonConverter and JsonSerializerSettings.TypeNameHandling = TypeNameHandling.Objects to create the required instances during deserialization. The instances are created by resolving the types from an Autofac IOC container. Everything works fine, except... I have several "core objects" that request a unique Id in the constructor from a service (which is correctly injected into the constructor). When deserializing this should not happen because it is fairly expensive and the Ids will be populated from the Json file anyway once the instance has been created. Currently, when

Can I customize Json.NET serialization without annotating my classes?

浪尽此生 提交于 2019-12-09 02:56:32
问题 I need to serialize some entity classes to JSON, using Json.NET. In order to customize the names of the properties, I use the [JsonProperty] attribute like this: [JsonProperty("lastName")] public string LastName { get; set; } The problem is, I'd prefer not to have any JSON-related attributes in my entities... Is there a way to externalize the annotations somehow, so that they don't clutter my entities? Using XmlSerializer , it can be done easily with the XmlAttributeOverrides class. Is there