json.net

Serializing/deserializing derived objects in SignalR

若如初见. 提交于 2020-03-22 23:09:45
问题 I am using SignalR 1.1 with .NET clients. I have a single method in my hub that accepts an object of BaseMessage class and broadcasts it to all clients: public void SendMessage(BaseMessage message) { Clients.All.BroadCastMessage(message); } Clients will pass derived messages into this method: _hub.Invoke("SendMessage", new ErrorMessage("Some Error")).Wait(); The client has a single message handler: _hub.On<BaseMessage>("BroadCastMessage", OnMessageReceived); I've specified TypeNameHandling

Newtonsoft.Json - Getting corresponding line numbers of deserialized objects from JSON for better error handling

只谈情不闲聊 提交于 2020-03-21 18:04:20
问题 My application accepts long JSON templates from clients that I deserialize and process. I would like to provide better error handling information that contains lineNumber of invalid objects in the JSON text to customers. Note that this is for errors that occur in post-processing and NOT for errors that occur during deserialization as this is already handled by the Newtonsoft. As an example, I have the below JSON and its corresponding .Net type { "Version": "1.0.0.0", "MyComplexObject": [ {

JSON string encoded twice?

徘徊边缘 提交于 2020-03-18 10:17:41
问题 My Web API (code that generates JSON) is generating the following JSON string. It seems, if I am not wrong, that it has been encoded twice: "\"[{\\\"SportID\\\":1,\\\"SportName\\\":\"Tennis\\\"},{\"SportID\\\":2,\\\"SportName\\\":\\\"Footbal\\\"},{\"SportID\\\":3,\"SportName\":\\\"Swimming\\\"}]\"" Web API code: public string JSONTest() { List<Sport> sports = new List<Sport>(); sports.Add(new Sport() { SportID = 1, SportName = "Tennis" }); sports.Add(new Sport() { SportID = 2, SportName =

Json.net deserialization null guid case

杀马特。学长 韩版系。学妹 提交于 2020-03-13 06:14:09
问题 I'm deserializing an object using Json.NET that contains a private field of type Guid and a public property for that field. When the value for my Guid is null in my json I want to assign Guid.Empty to my field. public class MyClass { private Guid property; public Guid Property { get { return property; } set { if (value == null) { property = Guid.Empty; } else { property = value; } } } } But the deserializer wants to access the private field, cause I get this error when I try to deserialize:

Can Json.Net be embedded into the executable?

拥有回忆 提交于 2020-03-13 05:15:33
问题 I set the 'Embed Interop Types' property of the Netwonsoft.Json library to true and it returns an error: Cannot embed interop types from assembly 'c:\path\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll' because it is missing either the 'ImportedFromTypeLibAttribute' attribute or the 'PrimaryInteropAssemblyAttribute' attribute c:\path\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll It looks like looking for missing references within the Newtonsoft.Json library, but I

Trouble formatting valid JObject for .WithParameters() when deploying new Azure VM from ARM Template

人盡茶涼 提交于 2020-02-29 05:49:24
问题 Currently I'm having trouble with deploying an Azure VM from an ARM template using an Azure Function which is written in C#, whilst using a JObject, from the Newjonsoft.Json,Linq library, to provide parameters for the new VM. The JObject.FromObject() method formulates parameters in the format "{"paramName": "paramValue"}" , however I believe that it needs to be formulated as "{"paramName": { "value": "paramValue"} . I'm not sure if 'contentVersion' and '$schema' ARM Template parameters also

使用Json.net将JSON对象反序列化为动态对象

烈酒焚心 提交于 2020-02-28 06:27:34
是否可以使用json.net从json反序列化返回动态对象? 我想做这样的事情: dynamic jsonResponse = JsonConvert.Deserialize(json); Console.WriteLine(jsonResponse.message); #1楼 从Json.NET 4.0 Release 1开始,提供了本机动态支持: [Test] public void DynamicDeserialization() { dynamic jsonResponse = JsonConvert.DeserializeObject("{\"message\":\"Hi\"}"); jsonResponse.Works = true; Console.WriteLine(jsonResponse.message); // Hi Console.WriteLine(jsonResponse.Works); // True Console.WriteLine(JsonConvert.SerializeObject(jsonResponse)); // {"message":"Hi","Works":true} Assert.That(jsonResponse, Is.InstanceOf<dynamic>()); Assert.That(jsonResponse, Is

如何使用json.net忽略类中的属性null

守給你的承諾、 提交于 2020-02-26 23:07:13
我正在使用 Json.NET 将类序列化为JSON。 我有这样的课: class Test1 { [JsonProperty("id")] public string ID { get; set; } [JsonProperty("label")] public string Label { get; set; } [JsonProperty("url")] public string URL { get; set; } [JsonProperty("item")] public List<Test2> Test2List { get; set; } } 我想仅当 Test2List 为 null 时才向 Test2List 属性添加 JsonIgnore() 属性。 如果它不为null,那么我想将它包含在我的json中。 #1楼 使用 JsonProperty 属性的替代解决方案: [JsonProperty(NullValueHandling=NullValueHandling.Ignore)] // or [JsonProperty("property_name", NullValueHandling=NullValueHandling.Ignore)] // or for all properties in a class [JsonObject

serialise a c# datatable with a hierarchy as json heirachy

萝らか妹 提交于 2020-02-25 13:52:15
问题 I have a dataTable with the following data in it: Parent Child Dan Heidi Dan Lauren Alan Dan Daphne Alan Alan Lorna Alan Tim I want to output the following json: [ { 'name': 'Daphne', 'children': [ { 'name': 'Alan', 'children': [ { 'name': 'Dan', 'children': [ { 'name': 'Heidi' }, { 'name': 'Lauren' } ] }, { 'name': 'Tim' }, { 'name': 'Lorna' } ] } ] } ] I've used the json.net serialiser to serialse one level in the hierarchy, I would have thought this was a common problem. Is there a simple

Newtonsoft DeserializeXNode expands internal arrays incorrectly

烂漫一生 提交于 2020-02-24 06:49:51
问题 I have a JSON object like the following: { "property1": "value1", "property2": "value2", "property3": ["value3","value4","value5"] } When I try to use DeserializeXNode to convert to XML, though, the array goes away. <MyObj> <property1>value1</property1> <property2>value2</property2> <property3>value3</property3> <property3>value4</property3> <property3>value5</property3> </MyObj> This causes issue when I try to re-serialize back to the object, because I get a "cannot convert string to string[