json.net

Converting JToken into .NET types with custom SerializerSettings

∥☆過路亽.° 提交于 2021-01-28 08:07:16
问题 Json.NET separates the JSON parsing from the construction of .NET objects. In particular should JsonConvert.DeserializeObject<MyType>(jsonString) be the same as JsonConvert.DeserializeObject<JToken>(jsonString).ToObject<MyType>() The ToObject method has no parameter that takes a SerializerSettings though. So how do I specify JSON converters and related settings? 回答1: If you have already parsed your JSON into a JToken hierarchy, you can use JToken.ToObject<T>(JsonSerializer) to deserialize to

Move property child object to root

久未见 提交于 2021-01-28 06:08:05
问题 I have a c# object that natively serializes to { "LrsFeature": { "MEASURE": 1.233242, "STATION_ID": "brians station", "NLF_ID": "brians route" }, "EVENT_ID": "00000000-0000-0000-0000-000000000000", } and I want it to be { "MEASURE": 1.233242, "STATION_ID": "brians station", "NLF_ID": "brians route", "EVENT_ID": "00000000-0000-0000-0000-000000000000", } where all the properties within LrsFeature are added to the root level. My attempt var events = JObject.FromObject(LrsEvent); var attrs =

Azure Functions (Durable) - Type is an interface or abstract class and cannot be instantiated

你。 提交于 2021-01-28 05:39:28
问题 Problem I have a C# Durable Azure Functions app (v2) containing an orchestration and activity function. The orchestration calls the activity as follows: propBag.CE = await context.CallActivityAsync<CEData>(nameof(GetCEByExternalIdActivity), propBag); The call works ok and the Activity function executes, builds up the content for propBag.CE and returns. The CE object contains a few other objects, including: public List<IParty> Individuals { get; set; } public List<IParty> Organisations { get;

How to solve “Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types” Android error?

[亡魂溺海] 提交于 2021-01-28 05:15:00
问题 I'm working in an App with Unity3D. Im using my own utilities DLL with some classes. I've been working for months now, and made lots of builds/compilations with no problem. Last week added a new class to the DLL, it worked well in my PC but when I builded the project and used it in my Android device it stopped working. The Android Monitor program shows me that error: "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" The types that Newtonsoft can find

How do I merge multiple json objects

不打扰是莪最后的温柔 提交于 2021-01-28 05:09:22
问题 I use different urls get similar json data. So that I can get more information in the same model It has trouble me for a long time i tried to use json.net for a single list. string url1= "https://apt.data2.com"; string url2= "https://apt.data1.com"; var json1= webClient.DownloadString(url1); var json2= webClient.DownloadString(url2); These calls return multiple json objects with the same structure { data: [ { created_time: "1457332172", text: "什麼東西", from: { username: "d86241", profile

Json.NET get default values from auto-property initializer

懵懂的女人 提交于 2021-01-28 01:32:05
问题 I want to reduce the string Json.NET generates by using default values. One of my properties is the following: public string Name { get; set; } = "Jennifer"; I already use the auto-property initializer so the string gets populated if empty. When serializing with Json.NET I use DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate so only changed properties get persisted. I know that I can use the DefaultValueAttribut like this: [DefaultValue("Jennifer")] public string Name { get; set;

How to find json token by value and then delete the token

人走茶凉 提交于 2021-01-27 21:22:18
问题 I have, for example, a json like this in C# : { "Harry.firstName": "Harry", "Harry.lastName": "Birimirski", "Harry.recordTitle": "My title", "Harry.SomeRepeatable": [{ "GUID": "9dd8c7bb-64e1-452b-90d5-db2b6e505492", "NestedRepetable": [{ "GUID": "05aa2161-fcc2-45a6-b0b7-8749d94a2e61", "nestedText": "Nested first" }, { "GUID": "dfd67eeb-703b-4cc4-9321-b6a39084e687", "nestedText": "Nested first 2" } ], "name": "First" }, { "GUID": "3318e544-1be8-4795-9bab-fa05de79cf46", "NestedRepetable": [{

Can't include Newtonsoft JSON in CSharpCodeProvider script

谁说我不能喝 提交于 2021-01-27 21:20:58
问题 I am trying to use JSON in a dynamically compiled script. However, when I include the library and add a JObject, I get errors like: "The type 'System.Dynamic.IDynamicMetaObjectProvider' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'." I included MathNet.Numerics just fine. Here is my setup. Console application .NET Framework 4 (so it matches the runtime compile) Nuget

Custom serializer for just one property in Json.NET, without changing the model class

随声附和 提交于 2021-01-27 20:25:02
问题 I need to do something like the following, but I need to do it without putting an attribute on or otherwise polluting the model class. An ideal solution would work via the JsonSerializerSettings , without disturbing other custom serialization. Incidentally, the below came from this question: Custom conversion of specific objects in JSON.NET public class Person { public string FirstName { get; set; } [JsonConverter(typeof(AllCapsConverter))] public string LastName { get; set; } // more

Convert Newtosoft JObject directly to BsonDocument

家住魔仙堡 提交于 2021-01-27 19:45:12
问题 Try to convert JObject to BsonDocument using this example https://www.newtonsoft.com/json/help/html/WriteJTokenToBson.htm ( BsonWriter obsolete, so I use BsonDataWriter ) var jObject = JObject.Parse("{\"name\":\"value\"}"); using var writer = new BsonDataWriter(new MemoryStream()); jObject.WriteTo(writer); var bsonData = writer.ToBsonDocument(); Console.WriteLine(bsonData.ToJson()); output: { "CloseOutput" : true, "AutoCompleteOnClose" : true, "Formatting" : 0, "DateFormatHandling" : 0,