system.text.json

Does the new `System.Text.Json` have a required property attribute?

若如初见. 提交于 2019-12-11 10:56:15
问题 I've combed through the MS docs but cannot find an attribute equivalent to the NewtonSoft JsonPropertyRequired. What I'm looking for this: public class Videogame { [JsonProperty(Required = Required.Always)] public string Name { get; set; } } Am I just missing something or does this level of validation not exist in the Microsoft library? 回答1: Not as of .NET core 3.0. The only ones supported are: JsonConverterAttribute JsonExtensionDataAttribute JsonIgnoreAttribute JsonPropertyNameAttribute

How to globally set default options for System.Text.Json.JsonSerializer?

守給你的承諾、 提交于 2019-12-10 06:53:05
问题 UPDATE: If interested in seeing this behavior implemented for System.Text.Json.JsonSerializer head on over to the open GitHub issue pointed out by Chris Yungmann and weigh in. Instead of this: JsonSerializerOptions options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase // etc. }; JsonSerializer.Deserialize<SomeObject>(someJsonString, options); I would like to do something like this: // This property is a pleasant fiction JsonSerializer.DefaultSettings = new

Exception parsing json with System.Text.Json.Serialization

瘦欲@ 提交于 2019-12-07 22:58:24
问题 My sample code is very simple: using System.Text.Json.Serialization; using Newtonsoft.Json; public class C { public C(string PracticeName) { this.PracticeName = PracticeName; } public string PracticeName; } var x = new C("1"); var json = JsonConvert.SerializeObject(x); // returns "{\"PracticeName\":\"1\"}" var x1 = JsonConvert.DeserializeObject<C>(json); // correctly builds a C var x2 = System.Text.Json.Serialization.JsonSerializer.Parse<C>(json); the last line raises: Exception thrown:

Modifying a JSON file using System.Text.Json

巧了我就是萌 提交于 2019-12-07 17:47:12
问题 So first off, yes I know you can do this super easily with Newtonsoft, but as I am working with .NetCore 3.0 I am trying to use the new methods for interacting with JSON files (in other words System.Text.Json ) and I refuse to believe that what I am trying to do is all that difficult! My application needs to list users that have not already been added to my database. In order to get the full list of all users, the app retrieves a JSON string from a web api. I now need to cycle through each of

Converting newtonsoft code to System.Text.Json in .net core 3. what's equivalent of JObject.Parse and JsonProperty

断了今生、忘了曾经 提交于 2019-12-07 15:06:37
问题 I am converting my newtonsoft implementation to new JSON library in .net core 3.0. I have the following code public static bool IsValidJson(string json) { try { JObject.Parse(json); return true; } catch (Exception ex) { Logger.ErrorFormat("Invalid Json Received {0}", json); Logger.Fatal(ex.Message); return false; } } I am not able to find any equivalent for JObject.Parse(json); Also what will be the attribute JsonProperty equivalent public class ResponseJson { [JsonProperty(PropertyName =

Exception parsing json with System.Text.Json.Serialization

笑着哭i 提交于 2019-12-06 13:23:56
My sample code is very simple: using System.Text.Json.Serialization; using Newtonsoft.Json; public class C { public C(string PracticeName) { this.PracticeName = PracticeName; } public string PracticeName; } var x = new C("1"); var json = JsonConvert.SerializeObject(x); // returns "{\"PracticeName\":\"1\"}" var x1 = JsonConvert.DeserializeObject<C>(json); // correctly builds a C var x2 = System.Text.Json.Serialization.JsonSerializer.Parse<C>(json); the last line raises: Exception thrown: 'System.NullReferenceException' in System.Text.Json.dll Object reference not set to an instance of an object

Converting newtonsoft code to System.Text.Json in .net core 3. what's equivalent of JObject.Parse and JsonProperty

点点圈 提交于 2019-12-06 02:58:54
I am converting my newtonsoft implementation to new JSON library in .net core 3.0. I have the following code public static bool IsValidJson(string json) { try { JObject.Parse(json); return true; } catch (Exception ex) { Logger.ErrorFormat("Invalid Json Received {0}", json); Logger.Fatal(ex.Message); return false; } } I am not able to find any equivalent for JObject.Parse(json); Also what will be the attribute JsonProperty equivalent public class ResponseJson { [JsonProperty(PropertyName = "status")] public bool Status { get; set; } [JsonProperty(PropertyName = "message")] public string Message

Parsing a JSON file with .NET core 3.0/System.text.Json

守給你的承諾、 提交于 2019-12-03 12:30:15
问题 I'm trying to read and parse a large JSON file that cannot fit in memory with the new JSON reader System.Text.Json in .NET Core 3.0. The example code from Microsoft takes a ReadOnlySpan<byte> as input public static void Utf8JsonReaderLoop(ReadOnlySpan<byte> dataUtf8) { var json = new Utf8JsonReader(dataUtf8, isFinalBlock: true, state: default); while (json.Read()) { JsonTokenType tokenType = json.TokenType; ReadOnlySpan<byte> valueSpan = json.ValueSpan; switch (tokenType) { case JsonTokenType

ASP.NET Core 3.0 System.Text.Json Camel Case Serialization

橙三吉。 提交于 2019-12-01 08:46:38
问题 In ASP.NET Core 3.0 Web API project, how do you specify System.Text.Json serialization options to serialize/deserialize Pascal Case properties to Camel Case and vice versa automatically? Given a model with Pascal Case properties such as: public class Person { public string Firstname { get; set; } public string Lastname { get; set; } } And code to use System.Text.Json to deserialize a JSON string to type of Person class: var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}"; var person

JsonConverter equivalent in using System.Text.Json

一个人想着一个人 提交于 2019-11-29 01:26:18
问题 I'm starting to migrate some code I have from Newtonsoft.Json to System.Text.Json in a .net Core 3.0 app. I migrated the properties from [JsonProperty("id")] to [JsonPropertyName("id")] but I have some properties decorated with the JsonConverter attribute as: [JsonConverter(typeof(DateTimeConverter))] [JsonPropertyName("birth_date")] DateTime BirthDate{ get; set; } But I cannot find the equivalent of this Newtonsoft converter in System.Text.Json Does someone know how can this be achieved in