json.net

Serialise custom collection With additional properties and on Add to hookup item propertChanged events

百般思念 提交于 2020-01-05 03:01:40
问题 I have a custom collection which I would like to serialise with JSON.NET: I need it to serialise the child collections within this custom collection. On deserialisation I need to hook up PropertyChanged event for items within the collection. If I pass my collection as is, Json sees IEnumerable and serialises the items in the collection ok, but ignores the other collections within. If I attribute the collection with [JsonObject] it will serialise all the internal collections but not the

Cannot update a Windows Service installed by MSI

末鹿安然 提交于 2020-01-04 15:25:16
问题 I'm facing a weird problem and I can't find a working solution. Thanks in advance for any help. I developed a Windows Service in C# that use Newtonsoft.JSON library to parse the result from a bunch of WebAPI. The service is deployed with MSI and everything was working perfectly. The first version of the service was using Newtonsoft.JSON 6.0.8, but recently I moved to version 11.0.2. I packed a new MSI (with correct version/Product Id/Upgrade Id to ensure upgrading) and I'm trying to deploy

Deserializing complex nested dictionary type with interface values using Json.net

﹥>﹥吖頭↗ 提交于 2020-01-04 13:37:36
问题 I am having a problem trying to deserialize a pretty complex nested dictionary type with interface values using Json.net. The code is located here "https://dotnetfiddle.net/JSoAug", and the types in question are: public class TypeConverter<T, TSerialized> : CustomCreationConverter<T> where TSerialized : T, new() { public override T Create(Type objectType) { return new TSerialized(); } } public interface IValue { Dictionary<string, IValue> SomeValues { get; set; } } public class Value : IValue

Deserialise nested JSON with Newtonsoft and C#

≯℡__Kan透↙ 提交于 2020-01-04 06:09:46
问题 I am trying to parse a Json response form a Rest API. I can get the response fine and have created some class Models. I am using Newtonsoft's Json.Net. I keep getting null values in my response and am not sure if I have setup my Models correct or am missing something? What I am trying to get is for example, Firstname and ReferenceNumber which are in Data\Items\Employee An example of my Json response is: { "Data": { "Links": [ { "Rel": "parent", "Href": "http://Api/url/", "Title": "Api", } ],

JSON make case insensitive

假如想象 提交于 2020-01-04 05:25:14
问题 I'm trying to generate a schema from my C# models, and so far things are going well with the following: JSchemaGenerator generator = new JSchemaGenerator(); JSchema schema = generator.Generate(typeof(MyClass)); schemachema.AllowAdditionalProperties = false; schemachema.UniqueItems = false; JObject update = JObject.Parse(@"{MYJSON}"); IList<string> messages; bool IsValid = update.IsValid(clientSchema, out messages); The one thing I have not been able to figure out is how to make it case

How to deserialize object that can be an array or a dictionary with Newtonsoft?

假装没事ソ 提交于 2020-01-04 05:22:07
问题 I am using an API that returns a json object that I need to deserialize. My problem is that one of the members of those object is sometimes an empty array ("[]") and sometimes a dictionary ("{"1":{...}, "2":{...}}"). I want to deserialize it into either an array or a dictionary, since I don't car about the IDs, I just want a list of all the objects. Here is how I deserialize the object: var response = JsonConvert.DeserializeObject<Response>(json); And here is the definition of the Response

c# Json.NET ToJson() FromJson()

喜夏-厌秋 提交于 2020-01-04 03:29:12
问题 I understand the value of creating an external JsonConverter classes however in many cases I find it unnecessarily cumbersome. Is there a general way to tell JSon.NET that if a class has a string ToJson() method use that for serialization and if the class has a void FromJson(string json) method use that for deserialization ? Or/And also for immutable types if a class has a static T FromJson(string json) use that ? 回答1: We need a special conveter: namespace Newtonsoft.Json { class

Why does AddAfterSelf return 'JProperty cannot have multiple values' when used with SelectToken?

放肆的年华 提交于 2020-01-04 03:11:29
问题 I want to add a new JProperty to a JSON object using a string path. I'm retrieving an existing path and then adding a new value proximal to it. It seems no matter how I select a token, or no matter what Add method I call (most relevant is AddAfterSelf) or what I supply as a new value, I receive the exception: Run-time exception (line 9): Newtonsoft.Json.Linq.JProperty cannot have multiple values. You can see this failing here: https://dotnetfiddle.net/mnvmOI Why can't I add a JProperty in

Custom DateTime serialization with Json.Net

℡╲_俬逩灬. 提交于 2020-01-04 01:48:31
问题 I'm trying unsuccessfully to create custom DateTime converter. The problem: I have many objects to serialize, some of the containing property of DateTime with DateTime.MinValue in it. I want to serialize it as null. But all the solution that I foud asking to decorate the proper inside the object (I can't do it) Other solution that I found below, is to create converter, As far as I can understand, this convertor works only DateTime object returned explicitly and not inside other object. Please

Unable to install Newtonsoft Json

蹲街弑〆低调 提交于 2020-01-03 21:11:24
问题 I am trying to install Newtonsoft Json (with the package manager console) so that I can use it in my C# console app, but I get this error: PM> Install-Package Newtonsoft.Json Install-Package : Unable to find package 'Newtonsoft.Json' At line:1 char:1 + Install-Package Newtonsoft.Json + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Install-Package], Exception + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets