json.net

How to read with interfaces in the model?

生来就可爱ヽ(ⅴ<●) 提交于 2020-06-27 05:54:08
问题 I would like to know how to read the JSON with a dynamic layout into my object that has an interface IPeople . When I try I get an error: 'Could not create an instance of type 'xxxx'. Type is an interface or abstract class and cannot be instantiated. I have spent 2 days searching through Google and I tried using converters and it only works for one object at a time So I would have to choose between a Staff converter and a spectator converter. How can I get both objects to load using the

Cannot deserialize the current JSON object into type 'System.Collections.Generic.List

邮差的信 提交于 2020-06-23 12:20:31
问题 I am trying to read my JSON result. Here is my JsonResult public class JsonResult { public string ResponseStatus; public string Status; public string Remarks; public string ErrorCode; public List<Data> data; } public class Data { public string Status; public DateTime Date; public string Number; public string Amount; public string Balance; public string TranId; public string OPTId; public string RefId; } Here is my JSON result { "ResponseStatus":"1", "Status":"Success", "Remarks":"Your

Is this a bug in JSON.NET or Entity Framework or am I doing something wrong while trying to serialize a list of Exceptions with JSON.NET?

大城市里の小女人 提交于 2020-06-23 08:31:47
问题 Got this error when trying to serialize a set of errors: "ISerializable type 'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException' does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present." The constructor is in fact present in the base classes, but it is a protected member. Someone asked to see the JSON: { "$type": "System.Data.Entity.Infrastructure

Can System.Text.Json.JsonSerializer serialize collections on a read-only property?

故事扮演 提交于 2020-06-23 08:01:45
问题 I'm having trouble getting the new System.Text.Json to deserialize collections stored on read-only properties. Consider these classes: public class SomeItem { public string Label { get; set; } } public class SomeObjectWithItems { public string Label { get; set; } // Note this property is read-only but the collection it points to is read/write public ObservableCollection<SomeItem> Items { get; } = new ObservableCollection<SomeItem>(); } Here's the JSON: { "Label": "First Set", "Items": [ {

System.Text.Json: Deserialize JSON with automatic casting

帅比萌擦擦* 提交于 2020-06-23 01:56:51
问题 Using .Net Core 3's new System.Text.Json JsonSerializer, how do you automatically cast types (e.g. int to string and string to int)? For example, this throws an exception because id in JSON is numeric while C#'s Product.Id is expecting a string: public class HomeController : Controller { public IActionResult Index() { var json = @"{""id"":1,""name"":""Foo""}"; var o = JsonSerializer.Deserialize<Product>(json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true, }); return View(); }

deserialization issue, with json.net, in F#

帅比萌擦擦* 提交于 2020-06-16 17:00:33
问题 I have a simple object to deserialize, but I do not understand the error I get. The code is the following: open System open Newtonsoft.Json type r = { Timestamp: DateTime Currency: string PreviousDeposited: int64 option PreviousWithdrawn: int64 option PreviousTransferIn: int64 option PreviousTransferOut: int64 option PreviousAmount: int64 option TransferIn: int64 option TransferOut: int64 option Amount: int64 option PendingCredit: int64 option PendingDebit: int64 option ConfirmedDebit: int64

Newtonsoft Json.NET JsonConverter attribute preserve references issue when deserializing

岁酱吖の 提交于 2020-06-12 18:34:14
问题 In the models of a project I am using a JsonConverter attribute to help with the (de)serialization of those models. The converter currently looks like this: public class CustomJsonConverter : Newtonsoft.Json.JsonConverter { bool _canWrite = true; public override bool CanWrite { get { return _canWrite; } } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects; serializer

How to add properties at runtime to JSON (C#)

99封情书 提交于 2020-06-09 04:21:20
问题 Note: Im working with System.Text.Json package Below is JSON I am getting from a database. I have to go through each of the keys in the JSON and check if there is a period ( . ) in the key name; if so, I need to add a property required with the value of true in the JSON in order to provide runtime validation: validate:{"required", true} Here is my JSON: { "display": "wizard", "settings": {}, "components": [{ "title": "Event Information", "label": "Event Information", "type": "panel", "key":

How to enforce quotes on property names on JSON .NET

喜欢而已 提交于 2020-06-08 19:42:17
问题 I am using Json.NET to Parse some JSON I receive to an endpoint I have on my application. I want the following JSON object to fail parsing since its property name does not have quotes on it: { foo: "bar" } JToken.Parse() says it is valid JSON. However, when I used an online parser I get the following error Strings should be wrapped in double quotes. Is there a way to make JSON .NET enforce this rule? 回答1: Json.NET does not currently implement strict parsing of JSON property names. Internally

Adding a property into specified location into json using Newtonsoft.Json

半世苍凉 提交于 2020-06-07 07:24:29
问题 I have a following JSON . I have added 2 x double quotes so that I could declare it as string variable in c# , in reality it contains one times double quotes for properties. The complete example code is in this fiddle. I am using Newtonsoft but can use System.Text.Json as well so a working solution for either will be appreciated. { ""display"": ""wizard"", ""settings"": {}, ""components"": [ { ""title"": ""Activity Information"", ""label"": ""Activity Information"", ""type"": ""Activity"", "