This question already has answers here:
Issue with serializing data using JSON.Net
(1 answer)
Configure Json.NET serialization settings on a class level
(1 answer)
Closed 2 years ago.
I am trying to return a class object where it will serialize the json without lowercase of the first letter of the property names. I can add the [JsonProperty{"MyField")] and that does work but is there a easier way as I have a lot of properties to do this for on the class. I don't want to set a global setting where it may break some things so just trying to see in my API best way to make sure the serialized json returns as is.
Thanks all
Related
This question already has answers here:
How to programmatically choose a constructor during deserialization?
(5 answers)
Closed 3 years ago.
I wish deserialize list by json.net. The inkpoint has a constructor with parameters. The json.net seems does not automatic use the inkpoint constructor.
I think I should use the IContractResolver Interface or JsonConverter, to indicate use the inkpoint's constructor with parameters. But I just know how to deserialize a Object like this, do not know how to use them in a collection.
Just use the JsonConstructor attribute
[JsonConstructor]
public Model(int? id, string name, string description)
{
...
}
Just make sure the param names match the json
For collections, as long as the json represents an array of objects you have a model for, you can just do something like
List<Model> models= JsonConvert.DeserializeObject<List<Model>>(json);
This question already has answers here:
.NET NewtonSoft JSON deserialize map to a different property name
(6 answers)
Closed 3 years ago.
I’m targeting .net framework 4.7 with a winforms application.
I started by following this article https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/console-webapiclient so I am using DataContractJsonSerializer.
I’m trying to learn about a REST interface that returns JSON -
{"Resource":
{"#attributes":
{"name":"Asset",
"resourceId":"Asset",
"type":"Resource"
}
}
}
I used netwonsoft.json 12.0.2 to paste the JSON as classes. It ignores the ‘#’ character and creates a member “attributes” in class “Resource” with type “Attributes” .
When DataContractJsonSerializer attempts to deserialise the JSON it skips the #attribute element, I presume because it does not match the class name.
Is there a way to map the element #attributes to my attributes member / class?
I have tried adding [DataMember(Name = "#attributes")] on the attributes member of the Resource class and a [DataContract(Name = "#attributes")] on the Attributes class but still the element appears to be skipped (attributes member of Resource is null).
Yes, use JsonProperty
public class MyClass
{
[JsonProperty("#attributes")]
public string attributes { get; set; }
}
This question already has answers here:
What is the difference between getter-only auto properties and expression body properties?
(2 answers)
Closed 7 years ago.
I have a class that uses some services. I need each service to be instantiated on creation of the class. In C# 6 I can see 2 ways of doing this but I'm not sure which would be correct...
protected static SomeServiceType Service => new SomeServiceType();
alternatively I could use an autoproperty initialiser...
protected static SomeServiceType Service { get;} = new SomeServiceType();
What are the advantages/drawbacks with each approach?
Many thanks
I believe
the former ("Expression-bodied members") calls new SomeServiceType() every time the property is read
the latter ("Auto-property initializers") calls it once on instantiation, and returns the created instance every time the property is read.
It sounds like you want the latter.
This question already has answers here:
Add new property to string class C# [duplicate]
(3 answers)
Closed 7 years ago.
I don't like the way string behaves and there are a few other things I would like to change.
It appears string cannot be extended because it is a sealed class.
Is there another way? I could copy the source code and make my own class but then it wouldn't be compatible with string, or could I make it compatible?
You could use extension methods to extend String. The link below explains extension methods and has an example of how to add a WordCount() function to String.
https://msdn.microsoft.com/en-us/library/bb383977.aspx
This question already has answers here:
What does square bracket [] mean in the below code?
(2 answers)
Closed 7 years ago.
I have seen examples as following:
[Serializable]
public class TestFailedException : Exception { do stuff;}
Can anyone tell me the official terminology for this "[Serializable]"? Is it some kind of indexing attribute?
Thanks
The terminology for these are attributes.
Attributes add some meta-data to code. They themselves do not execute, not in conventional way. They add some extra information about a method or class. When such a decorated method (or class) is called buy a different part of the code, that code can query the meta-data and perform some action accordingly.
You can use reflection to query the attributes on a method (or class). See here.
In your particular example, the [Serializable] attribute tells CLR that given an instance of this class, the value of fields within that object can be serialized, i.e. can be sent over network, or can be written to disk. In this perspective, the [Serializable] attribute does not in any way adds that functionality, or helps in the process of serialization, just that it carries a meta-data that CLR should allow serialization.
It's an Attribute
https://msdn.microsoft.com/en-us/library/z0w1kczw.aspx
I've also heard people refer to them as Annotations but this is more of a Java term.