json.net

Optionally serialize a property based on its runtime value

℡╲_俬逩灬. 提交于 2021-02-06 01:48:50
问题 Fundamentally, I want to include or omit a property from the generated Json based on its value at the time of serialization. More-specifically, I have a type that knows if a value has been assigned to it and I only want to serialize properties of that type if there has been something assigned to it (so I need to inspect the value at runtime). I'm trying to make it easy for my API to detect the difference between "has the default value" and "wasn't specified at all". A custom JsonConverter

Optionally serialize a property based on its runtime value

…衆ロ難τιáo~ 提交于 2021-02-06 01:48:03
问题 Fundamentally, I want to include or omit a property from the generated Json based on its value at the time of serialization. More-specifically, I have a type that knows if a value has been assigned to it and I only want to serialize properties of that type if there has been something assigned to it (so I need to inspect the value at runtime). I'm trying to make it easy for my API to detect the difference between "has the default value" and "wasn't specified at all". A custom JsonConverter

What exceptions does Newtonsoft.Json.DeserializeObject throw?

强颜欢笑 提交于 2021-02-05 12:52:10
问题 What exceptions does Newtonsoft.Json.DeserializeObject throw? I want to handle them. http://james.newtonking.com/json/help/?topic=html/M_Newtonsoft_Json_JsonConvert_DeserializeObject.htm#seeAlsoToggle 回答1: JSON.NET defines the following exceptions: JsonException JsonReaderException JsonSerializationException JsonWriterException JsonSchemaException Serialization or deserialization errors will typically result in a JsonSerializationException . 回答2: Note that Json.NET's error handling

What exceptions does Newtonsoft.Json.DeserializeObject throw?

北城余情 提交于 2021-02-05 12:51:22
问题 What exceptions does Newtonsoft.Json.DeserializeObject throw? I want to handle them. http://james.newtonking.com/json/help/?topic=html/M_Newtonsoft_Json_JsonConvert_DeserializeObject.htm#seeAlsoToggle 回答1: JSON.NET defines the following exceptions: JsonException JsonReaderException JsonSerializationException JsonWriterException JsonSchemaException Serialization or deserialization errors will typically result in a JsonSerializationException . 回答2: Note that Json.NET's error handling

howto Serialize data consist an array using JsonWriter in C#

梦想的初衷 提交于 2021-02-05 11:54:47
问题 I'm looking for something that can be Serialize data that consist an Array using JsonWriter in C#? This is an error that im getting: ExceptionMessage: "Unsupported type: Module.Model.Acl_Entries[]. Use the JsonSerializer class to get the object's JSON representation. Path 'acl_entries'." ExceptionType: "Newtonsoft.Json.JsonWriterException" This is my Data: "author": { "stakeholder_id": "stkh-a23ee7909d024a21a54fb60d60089c97", "username": "alex", "acl_entries": [{ "stakeholder_id": "stkh

c# Deserialize unlabelled JSON array

▼魔方 西西 提交于 2021-02-05 09:33:28
问题 I am attempting to deserialize a piece of JSON with a specific structure like so: { "label1": "value1", "label2": [ [ [ "concept_id_1", "concept_1" ], score_1 ], [ [ "concept_id_2", "concept_2" ], score_2 ], …… ], "label3": "value3", "label4": "value4" } For what it's worth, the scores are floats and everything else is a string. The number of returned concepts under "label2" is variable. I'm attempting to deserialize it using JSON.net. The only content I actually care about is the inside nest

c# Deserialize unlabelled JSON array

半腔热情 提交于 2021-02-05 09:33:14
问题 I am attempting to deserialize a piece of JSON with a specific structure like so: { "label1": "value1", "label2": [ [ [ "concept_id_1", "concept_1" ], score_1 ], [ [ "concept_id_2", "concept_2" ], score_2 ], …… ], "label3": "value3", "label4": "value4" } For what it's worth, the scores are floats and everything else is a string. The number of returned concepts under "label2" is variable. I'm attempting to deserialize it using JSON.net. The only content I actually care about is the inside nest

Issues with System.Text.Json serializing Unicode characters (like emojis)

别说谁变了你拦得住时间么 提交于 2021-02-05 06:52:11
问题 I am upgrading an application from .NET Core 2.2 to .NET Core 3.0, and the new System.Text.Json serializer is not behaving the same as Newtonsoft did in 2.2. On characters like a non-breaking-space (\u00A0) or emoji characters, Newtonsoft (and even Utf8Json) serialize them as their actual characters, not the Unicode code. I've created a simple .NET Fiddle to show this. var input = new Foo { Bar = "\u00A0 Test !@#$%^&*() 💯\uD83D\uDCAF 你好" }; var newtonsoft = Newtonsoft.Json.JsonConvert

How to retrieve value from Json string in C#

本小妞迷上赌 提交于 2021-02-05 06:37:05
问题 I'm getting a response like { "expires": "Sat, 19 May 2046 04:10:58 +0000", "copy_ref": "SMJNA2wxbGZbnmbnm", "Result": null, "error": null } base: { "expires": "Sat, 19 May 2046 04:10:58 +0000", "copy_ref": "SMJNA2wxbGZ0aWRibWw2aA", "Result": null, "error": null } ContentDisposition: null ContentType: "application/json" HttpHeaders: {Connection: keep-alive expires=Tue, 25 May 2021 04:10:58 GMT; } IsArray: true IsSuccessfully: true IsXml: true Result: { "expires": "Sat, 19 May 2046 04:10:58

How to retrieve value from Json string in C#

大憨熊 提交于 2021-02-05 06:37:05
问题 I'm getting a response like { "expires": "Sat, 19 May 2046 04:10:58 +0000", "copy_ref": "SMJNA2wxbGZbnmbnm", "Result": null, "error": null } base: { "expires": "Sat, 19 May 2046 04:10:58 +0000", "copy_ref": "SMJNA2wxbGZ0aWRibWw2aA", "Result": null, "error": null } ContentDisposition: null ContentType: "application/json" HttpHeaders: {Connection: keep-alive expires=Tue, 25 May 2021 04:10:58 GMT; } IsArray: true IsSuccessfully: true IsXml: true Result: { "expires": "Sat, 19 May 2046 04:10:58