json.net

Customise NewtonSoft.Json for Value Object serialisation [duplicate]

末鹿安然 提交于 2019-12-10 17:36:00
问题 This question already has answers here : Json.net how to serialize object as value (6 answers) Closed 2 years ago . Sometimes, perhaps in a DDD situation, you might want to use C# to create Value Objects to represent data, to give more meaning to your domain than using primitive types would, with the added benefit of being immutable. For example: public class PostalCode // Bit like a zipcode { public string Value { get; private set; } public PostalCode(string value) { Value = value; } //

Populating non-serializable object with Json.NET

寵の児 提交于 2019-12-10 17:26:58
问题 In a test I want to populate an object (a view model) from a JSON string. For example, the target object has this property: public string Query { get; set; } So I want to be able to do this: var target = ...; JsonConvert.PopulateObject(target, "{ 'Query': 'test' }"); However, the Query property is not being set. Debugging through the code, it appears that properties on target are ignored because member serialization is opt-in. Since the target class is not a data contract and is not populated

Json.NET make property required based on property type

蹲街弑〆低调 提交于 2019-12-10 17:26:10
问题 I'm struggling with custom json serialization in .Net core, I'm trying to make all properties required by default except if property has specific type. Here is an example of what I'm trying to achieve: Let's assument that I have following type: F#: type FooType = { id: int name: string optional: int option } you can think about below code as similar to following in C#: class FooType = { int Id {get;set;}; string Name {get;set;}; Nullable<int> Optional {get;set;}; } What I'm trying to do is to

JSON.net vs XPATH: How to preserve node order in SelectTokens?

a 夏天 提交于 2019-12-10 17:19:32
问题 XPath 2 states that the nodes order of a selection should be returned in their order in the document. It looks this is not the case when you SelectTokens(JSONPath) in JSON.Net When I process the following document string json = @" { ""Files"": { ""dir1"": { ""Files"": { ""file1.1.txt"": { ""size:100""}, ""file1.2.txt"": { ""size:100""} } }, ""dir2"": { ""Files"": { ""file2.1.txt"": { ""size:100""}, ""file2.2.txt"": { ""size:100""} } }, ""file3.txt"": { ""size:100""} } }"; The order is the

Reference to automatically created objects

余生长醉 提交于 2019-12-10 17:18:38
问题 I am attempting to serialize and deserialize a complex object graph: Class A contains an read only property containing an immutable array of objects of type B . The objects of type B , as well as the immutable array, are created in the constructor of type A . Other types contain references to objects of type B that are obtained by accessing the array of an object of type A . During deserialization, I need any references to a B to end up pointing at the appropriate object created by the A

Json.NET - Serialize generic type wrapper without property name

霸气de小男生 提交于 2019-12-10 17:13:06
问题 I have a generic type that wraps a single primitive type to give it value equality semantics public class ValueObject<T> { public T Value { get; } public ValueObject(T value) => Value = value; // various other equality members etc... } It is used like: public class CustomerId : ValueObject<Guid> { public CustomerId(Guid value) : base(value) { } } public class EmailAddress : ValueObject<string> { public EmailAddress(string value) : base(value) { } } The issue is when serializing a type like:

Why can I not register a custom JSON.Net implementation in Nancy?

佐手、 提交于 2019-12-10 16:56:43
问题 I have the following installed: <package id="Nancy" version="1.4.2" targetFramework="net4" /> <package id="Nancy.Owin" version="1.4.1" targetFramework="net4" /> <package id="Nancy.Serialization.JsonNet" version="1.4.1" targetFramework="net4" /> <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net4" /> <package id="Owin" version="1.0" targetFramework="net4" /> And also: public sealed class Startup { public void Configuration(IAppBuilder app) { var options = new NancyOptions();

Get type name when deserializing to JObject

北慕城南 提交于 2019-12-10 16:28:25
问题 Is there a way to get the $type property when using Deserialize ? I serialize with TypeNameHandling on, but when I deserialize, I don't have the assemblies that contain the type information. I need to use the Type name to store it in the right collection, it looks like $type is not brought over to the JObject. Edit: If I deserialize as a JObject, I can get the $type, but if I deserialize as a class that has an object as a property, the type is null. Not sure why its getting stripped out as

Deserialize Json Object to polymorphic C# object without typeNameHandling

你离开我真会死。 提交于 2019-12-10 16:06:38
问题 My problem is I want to deserialize a json object to a C# object, but the trick is that the C# object contains List< abstract class > and this abstract class is a super class of another 10 classes. public sealed class SearchAPIResult { public string Status; public SearchAPIQuery Query; public SearchResults Result; public SearchAPIResult() { } public SearchAPIResult(string status) { Status = status; } } and SearchAPIResult is: public sealed class SearchResults { public string TextAnswer;

JSON parsing in c# item is not an array

北城以北 提交于 2019-12-10 16:05:50
问题 I am trying to parse a JSON string, that looks like that: { "totalCreditsRemoved": 1, "invalidReceivers": [], "ids": [100070531], "validReceivers": ["+33635938286"] } I retrieve this from a web API, and stock it as a String: var reader = new StreamReader(respStream); String result = reader.ReadToEnd().Trim(); response = result; Response is a public string Then, in another method: I try to parse my json string: var json = response; var objects = JArray.Parse(json); foreach (JObject root in