json.net

Custom Json converter for a collection of a container with polymorphic properties

限于喜欢 提交于 2019-12-13 20:36:40
问题 I am working on complex data transfer for Web API. it suppose to send structure that has a collection of classes with nested polymorphic properties. The default JSON Converter doesn't recognize polymorphic structures by default. I tried to use the full namespace resolution. Unfortunately, in my scenario it didn't work as i have different frameworks (Full framework and .net core) on the client server sides and it doesn't resolve it properly namespaces and assemblies. (yes I tried mapping of

Custom type serializer for Json.Net

烂漫一生 提交于 2019-12-13 20:22:33
问题 I'm using NewtonSoft Json.Net to serialize my objects to JSON. On of my classes is not serializing correctly. I was wondering if it is possible to build a custom type serializer that will output different JSON. How can I make a custom serializer/deserializer for a specific class? 回答1: you may create your own Custom JsonConverter and implement its ReadJson and WriteJson methods. 来源: https://stackoverflow.com/questions/20140472/custom-type-serializer-for-json-net

Deserializing abstract Queue<T> in Json [duplicate]

末鹿安然 提交于 2019-12-13 20:22:24
问题 This question already has an answer here : Deserialize a List<AbstractClass> with newtonsoft.json (1 answer) Closed 4 years ago . I have a Queue of an abstract class KVP. I queue 2 different objects which inherit from KVP. Everything works fine when I serialize the queue, but since KVP cannot be constructed it fails on deserialization. If it was a single non generic object I could deserialize as dynamic, but I'm not sure how to deserialize a queue that could hold both events and IDs. Sample

Deserializing to enum option in F#

北慕城南 提交于 2019-12-13 20:13:31
问题 A couple days ago, I posted a question about deserialization with enums in F#. The question is here: Deserialization in F# vs. C# The answer pointed to some code written by Isaac Abraham, at: https://gist.github.com/isaacabraham/ba679f285bfd15d2f53e However I am facing another problem: If the object to deserialize to has an object of type 'enum option', the deserialization will fail, whereas it'll work if the type is just 'enum'. A minimal example: type TestType = | A = 0 | B = 1 type

JSON to DataSet using Json.NET

馋奶兔 提交于 2019-12-13 19:35:25
问题 hoping someone can lend a hand... I am trying to convert JSON to a DataSet, using the below example, but am having problems. I've validated that the JSON is correct, used a method as suggested by Kent. Thanks for your time and help! The following is my JSON: {"jsonData":[{"item1":"one"},{"item2":"two"}]} Here's my webservice C# code: [WebMethod] public string setWorkOrdersUpdated(object jsonData) { try { XmlDocument xd = new XmlDocument(); xd = (XmlDocument)JsonConvert.DeserializeXmlNode

When merging objects using Newtonsoft.Json, how do you ignore empty string values?

。_饼干妹妹 提交于 2019-12-13 19:04:48
问题 I have a data model that is defined as a class in C#. I need to merge two objects using JObject.Merge, but in the case of one particular property I need to ignore empty string values from the 2nd object, similar to how you would ignore null values. Is there an existing attribute property for this, or do I somehow need to write my own? void Main() { string json1 = @"{ Foo: ""foo1"", Bar: ""bar1"" }"; string json2 = @"{ Foo: ""foo2"", Bar: null }"; string json3 = @"{ Foo: ""foo3"", Bar: """" }"

Custom object serialization vs PreserveReferencesHandling

妖精的绣舞 提交于 2019-12-13 18:36:17
问题 Is there any standard way to get "$id" field's value for the current object when serializing, and get the object by its "$id" value when deserializing, when using a custom JsonConverter ? 回答1: Inside a custom JsonConverter you can use the IReferenceResolver returned by JsonSerializer.ReferenceResolver to manually read and write Json.NET's "$id" and "$ref" properties. The following converter provides a template for this: public abstract class ReferenceHandlingCustomCreationConverter<T> :

ASP.NET Web API is serializing with underscore

妖精的绣舞 提交于 2019-12-13 18:11:51
问题 I have the following simple code in an ApiController: public Version Get() { var version = new System.Version(1, 1, 0, 0); return version; } which results in the following: json {"_Major":1,"_Minor":1,"_Build":0,"_Revision":0} xml <Version xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/System"> <_Build>0</_Build> <_Major>1</_Major> <_Minor>1</_Minor> <_Revision>0</_Revision> </Version> Please note the properties are preceded with an _ .

convert a Json value to Integer with Newtonsoft

孤者浪人 提交于 2019-12-13 18:05:37
问题 I am using the following code to add an attribute to my xml to designate that this node should return an Integer value when using JsonConvert.SerializeXmlNode. I have incorporated the update from Newtonsoft into my referenced dll. I am using the following code to add the attribute: ele.SetAttribute("Integer", "http://james.newtonking.com/projects/json", "true"); where ele comes from XmlElement ele = node as XmlElement; The result always ends up with something like this: "id": { "@Type":

error deserializing a Json string into a composite object

▼魔方 西西 提交于 2019-12-13 17:36:45
问题 I'm using Newtonsoft's Json.NET. Their documentation is not very helpful. What I need is to deserialize a json string into a C# object of a class of my authorship. The json string was never an object of this class, I got it using a WebRequest. I can't post the json string because It has over 34 thousand characters. Since the object uses a lot of composition, I tryed to generate all the classes needed myself. When I got a FormatException "Input string was not in a correct format." I thought