How to deserialize a nested json string? - c#

Suppose I have this classes:
internal class Test
{
public string value { get; set; }
public Test(string value)
{
this.value = value;
}
}
public class Response<T>
{
private string errorMessage { get; }
private T returnValue { get; }
public Response(string errorMessage, T returnValue)
{
this.errorMessage = errorMessage;
this.returnValue = returnValue;
}
public bool HasError() { return errorMessage != ""; }
public bool AssertEqualsReturnValue(object obj) { return returnValue.Equals(obj); }
}
and I have this Main function:
string json =
#"{
""errorMessage"": ""This is an error"",
""returnValue"": {
""value"": ""this is what returns""
}
}
";
Response<Test> r = JsonSerializer.Deserialize<Response<Test>>(json); ;
Console.WriteLine(r.AssertEqualsReturnValue("this is what returns"));
Console.WriteLine(r.HasError());
I get the next error message:
System.InvalidOperationException: 'Each parameter in the deserialization constructor on type 'IntroSE.Kanban.Backend.Response`1[ConsoleApp1.Test]' must bind to an object property or field on deserialization. Each parameter name must match with a property or field on the object. The match can be case-insensitive.'
It won't desirialize because I need to somehow tell the JsonSerializer I have a Test object in returnValue key.
I saw some solutions online but the problem is they all use JsonConverter which is no in use anymore in .Net 6.0.

You have defined your errorMessage and returnValue properties with private visibility. If you change them to public after that the exception will not be thrown.
Please be aware that the JsonInclude attribute can be used only in case of non-public property accessors, like private set; or private get;.

FYI
you can do this in VS
copy the json
{
"errorMessage": "This is an error",
"returnValue": {
"value": "this is what returns"
}
}
Choose EDIT < Paste Special > Paste JSON As Classes
Check and compare with your classes

Related

C# custom attribute validation in console environment

My question is about validation using custom attributes in C#.
I don't quite understand how the validation works. I have declared an attribute with the validation rule in it but when the error should be thrown it is not.
Attribute:
[AttributeUsage(AttributeTargets.Property)]
public class NotNullAttribute : Attribute
{
public bool IsValid(object value)
{
if (value is string && (string)value != "")
{
return false;
}
return true;
}
}
Inside the attribute I check if the property is of type string and if its value is an empty string because that is what I have to check.
The task is to check if a property is a string and if its an empty string then its not valid, otherwise it is.
My Person class:
class Person
{
[NotNull]
public string Name { get; set; }
}
Here I am applying the custom attribute.
Main method:
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();
p1.Name = "";
Console.WriteLine("Validation done");
Console.ReadKey();
}
}
This is where I instantiate the Person class and assign an empty string to the Name property. This is where the error should be thrown I guess.
So my question is why isn't the validation applied? Should I have called the IsValid method from the attribute it self somehow?
I would take some explanation about this, thank you in advance!
The attribute itself is just a "decorator" of the property. If nothing calls it, it will not be automatically executed nor used.
In your case, however, I don't see the point of using an attribute, when you can use property itself:
private string _name = "";
public string Name
{
get
{
return _name;
}
set
{
if ( string.IsNullOrEmpty(value) )
{
//throw or fallback
}
else
{
_name = value;
}
}
}
Doing basic value validation is exactly the job property setters are great for. In case someone uses an invalid value, you can throw an exception, or set a fallback value for example.
If you would still prefer using attributes, you still need to have some code that performs the validation itself. And still, anyone can assign any valid value to the property, unless validation is performed.
For example ASP.NET MVC uses attribute validation during Model Binding - it checks the validation attributes on the bound model class and verifies it before the action method begins executing.
Example of attribute validation
Here is a simple example of how to make your code work with reflection.
First here is a slightly updated version of the validation attribute:
[AttributeUsage(AttributeTargets.Property)]
public class NotNullAttribute : Attribute
{
public bool IsValid(object value)
{
if (!string.IsNullOrEmpty(value as string))
{
return false;
}
return true;
}
}
Your code actually only allowed a null or "" value, which I guess is opposite of what you wanted. This version is valid only when the string is not null and not empty.
Now create a Validate method in your Program class:
private static bool Validate(object model)
{
foreach (var propertyInfo in model.GetType().GetProperties())
{
foreach (var attribute in propertyInfo.GetCustomAttributes(true))
{
var notNullAttribute = attribute as NotNullAttribute;
if (notNullAttribute != null)
{
if (!notNullAttribute.IsValid(propertyInfo.GetValue(model)))
{
return false;
}
}
}
}
return true;
}
This basically gathers all properties of the type of the passed in parameter, checks all attributes of the properties for NotNullAttribute and then executes the attribute's IsValid method against the current value from the model.
Finally here is how you can call it from Main:
static void Main(string[] args)
{
Person p1 = new Person();
p1.Name = "d";
if (Validate(p1))
{
Console.WriteLine("Valid");
}
else
{
Console.WriteLine("Invalid");
}
Console.WriteLine("Validation done");
Console.ReadKey();
}
Now, if you are planning on adding more validation attributes, I would create an interface first:
public interface IValidationAttribute
{
bool IsValid(object value);
}
Then derive all your validation attributes from IValidationAttribute and in Validate method use IValidationAttribute in place of NotNullAttribute. This way the code becomes more future-proof as you can just program against the interface and add new validation attributes anytime.
public class BankAccount
{
public enum AccountType
{
Saving,
Current
}
[Required(ErrorMessage="First Name Required")]
[MaxLength(15,ErrorMessage="First Name should not more than 1`5 character")]
[MinLength(3,ErrorMessage="First Name should be more than 3 character")]
public string AccountHolderFirstName { get; set; }
[Required(ErrorMessage="Last Name Required")]
[MaxLength(15,ErrorMessage="Last Name should not more than 1`5 character")]
[MinLength(3,ErrorMessage="Last Name should be more than 3 character")]
public string AccountHolderLastName { get; set; }
[Required]
[RegularExpression("^[0-9]+$", ErrorMessage = "Only Number allowed in AccountNumber")]
public string AccountNumber { get; set; }
public AccountType AcType { get; set; }
[AccountBalaceCheckAttribute]
public double AccountBalance { get; set; }
}
How to Validate
public class GenericValidator
{
public static bool TryValidate(object obj, out ICollection<ValidationResult> results)
{
var context = new ValidationContext(obj, serviceProvider: null, items: null);
results = new List<ValidationResult>();
return Validator.TryValidateObject(
obj, context, results,
validateAllProperties: true
);
}
}
Example
static void Main(string[] args)
{
var bankAccount = new BankAccount();
ICollection<ValidationResult> lstvalidationResult;
bool valid = GenericValidator.TryValidate(bankAccount, out lstvalidationResult);
if (!valid)
{
foreach (ValidationResult res in lstvalidationResult)
{
Console.WriteLine(res.MemberNames +":"+ res.ErrorMessage);
}
}
Console.ReadLine();
}

C# - XML Deserialization of properties

I cannot find a reason why properties without getters aren't parsing properly, let me write you an example:
For XML in format
<request>
<job
mode="modefirst"
/>
<request>
I am trying to deserialize it to the POCO with a property:
private ESomeEnum emode;
[XmlAttribute(AttributeName = "mode")]
public string Mode
{
set { ESomeEnum.TryParse( blah blah );
}
emode is being set for default value in class constructor, while deserializing (System.Xml.Serialization without custom classes, just trying to be minimalistic in here) the xml from above, the setter is never being called, but when property 'Mode' contains a getter
get { return this.emode.ToString(); }
setter is actually being hit and proper value set during deserialization.
Why this situation occurs? Is there any reason behind it?
The XmlSerializer processes properties only, which have public get-set accessors. But you can customize anything by implementing IXmlSerializable:
public class MyXmlSerializableClass : IXmlSerializable
{
private ESomeEnum emode = ESomeEnum.modefirst;
public string Mode
{
set { emode = ESomeEnum.Parse(value); }
}
public int ReadWriteProperty { get; set; }
public int SemiReadOnlyProperty { get; private set; }
private int backingFieldOfRealReadOnlyProperty;
public int RealReadOnlyProperty
{
get { return backingFieldOfRealReadOnlyProperty; }
}
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(XmlReader reader)
{
if (reader.Settings != null && !reader.Settings.IgnoreWhitespace)
{
reader = XmlReader.Create(reader, new XmlReaderSettings { IgnoreWhitespace = true });
reader.Read();
}
reader.ReadStartElement();
Mode = reader.ReadElementContentAsString("Mode", String.Empty);
ReadWriteProperty = reader.ReadElementContentAsInt("ReadWriteProperty", String.Empty);
SemiReadOnlyProperty = reader.ReadElementContentAsInt("ReadOnlyAutoProperty", String.Empty);
backingFieldOfRealReadOnlyProperty = reader.ReadElementContentAsInt("ReadOnlyProperty", String.Empty);
}
public void WriteXml(XmlWriter writer)
{
writer.WriteElementString("Mode", emode.ToString());
writer.WriteElementString("ReadWriteProperty", ReadWriteProperty.ToString(CultureInfo.InvariantCulture));
writer.WriteElementString("ReadOnlyAutoProperty", SemiReadOnlyProperty.ToString(CultureInfo.InvariantCulture));
writer.WriteElementString("ReadOnlyProperty", RealReadOnlyProperty.ToString(CultureInfo.InvariantCulture));
}
#endregion
internal MyXmlSerializableClass()
{/*needed for deserialization*/
}
}

How can avoid use a hardcode string in ErrorMessage with DataAnnotations? [duplicate]

I would like to pass a dynamic variable as a parameter to my attribute. Here I want to use Environment.MachineName, see the code below:
public interface IMonitoringViewModelConfiguration : IConfigurationContainer
{
[ConfigurationKey("MonitoringService", Environment.MachineName)]
string ConnectionString { get; }
}
But I get this error:
Error 1 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type Abc.ServiceBus.Monitoring.ViewModel
Is there any workaround as clean as possible in order to pass my Environment.MachineName ?
Thanks.
John
PS: I've found some articles which talk about this case but it have been written like 2-3 years ago. But today, does the clr which comes from .NET 4.0 gives some nice solution ?
You could create an enum with special values, and accept them in a separate constructor overload in the attribute:
enum SpecialConfigurationValues
{
MachineName
// , other special ones
}
class ConfigurationKeyAttribute : Attribute
{
private string _key;
private string _value;
public ConfigurationKeyAttribute(string key, string value)
{
// ...
}
public ConfigurationKeyAttribute(string key, SpecialConfigurationValues specialValue)
{
_key = key;
switch (specialValue)
{
case SpecialConfigurationValues.MachineName:
_value = Environment.MachineName;
break;
// case <other special ones>
}
}
}
[ConfigurationKey("MonitoringService", SpecialConfigurationValues.MachineName)]
Attribute parameters are evaluated at compile time, not at runtime. So they have to be compile time constants...
However, you could create a derived class LocalMachineConfigurationKey attribute that takes only one parameter and uses Environment.MachineName at runtime to evaluate the property.
public class ConfigurationKeyAttribute : Attribute
{
private readonly string _key;
private readonly string _machineName;
public ConfigurationKeyAttribute(string key, string machineName)
{
_key = key;
_machineName = machineName;
}
protected ConfigurationKeyAttribute(string key) : this(key, null)
{
}
public string Key { get { return _key; } }
public virtual string MachineName { get { return _machineName; } }
}
public class LocalMachineConfigurationKeyAttribute : ConfigurationKeyAttribute
{
public LocalMachineConfigurationKeyAttribute(string key) : base(key)
{
}
public override string MachineName { get { return Environment.MachineName; } }
}

Deserialize json into C# object for class which has default private constructor

I need to deserialize json for following class.
public class Test
{
public string Property { get; set; }
private Test()
{
//NOTHING TO INITIALIZE
}
public Test(string prop)
{
Property = prop;
}
}
I can create an instance of Test like
var instance = new Test("Instance");
considering my json something like
"{ "Property":"Instance" }"
How shall I create an object of Test class as my default constructor is private and I am getting object where Property is NULL
I am using Newtonsoft Json parser.
You can make Json.Net call the private constructor by marking it with a [JsonConstructor] attribute:
[JsonConstructor]
private Test()
{
//NOTHING TO INITIALIZE
}
Note that the serializer will still use the public setters to populate the object after calling the constructor.
Another possible option is to use the ConstructorHandling setting:
JsonSerializerSettings settings = new JsonSerializerSettings
{
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};
Test t = JsonConvert.DeserializeObject<Test>(json, settings);
It doesn't seem like you need to take any extra steps.
Using Json.NET v6.0.8, the following C# program works inside LINQPad:
void Main()
{
var o = JsonConvert.DeserializeObject<Test>("{\"Property\":\"Instance\"}");
Debug.Assert(o.Property == "Instance",
"Property value not set when deserializing.");
}
public class Test
{
public string Property { get; set; }
private Test()
{
}
public Test(string propertyValue)
{
Property = propertyValue;
}
}
No need to create a Serializer setting and give assign ConstructorHandling here. Please remember to define the [JsonConstructor] attribute to the private constructor.
I have similar case with abstract BaseNode.cs and its concrete ComputerNode.cs implementation. You can create the classes, copy/paste the code below and do some experiment.
public abstract class BaseNode
{
[JsonConstructor] // ctor used when Json Deserializing
protected BaseNode(string Owner, string Name, string Identifier)
{
this.Name = Name;
this.Identifier = Identifier;
}
// ctor called by concrete class.
protected BaseNode(string [] specifications)
{
if (specifications == null)
{
throw new ArgumentNullException();
}
if (specifications.Length == 0)
{
throw new ArgumentException();
}
Name = specifications[0];
Identifier = specifications[1];
}
public string Name{ get; protected set; }
public string Identifier { get; protected set; }
}
public class ComputerNode: BaseNode
{
public string Owner { get; private set; }
[JsonConstructor] // not visible while creating object from outside and only used during Json Deserialization.
private ComputerNode(string Owner, string Name, string Identifier):base(Owner, Name, Identifier)
{
this.Owner = Owner;
}
public ComputerNode(string[] specifications):base(specifications)
{
Owner = specifications[2];
}
}
For JSon Read and Write following code helps -
public class Operation<T>
{
public string path;
public Operation()
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "nodes.txt");
if (File.Exists(path) == false)
{
using (File.Create(path))
{
}
}
this.path = path;
}
public void Write(string path, List<T> nodes)
{
var ser = JsonConvert.SerializeObject(nodes, Formatting.Indented);
File.WriteAllText(path, ser);
}
public List<T> Read(string path)
{
var text = File.ReadAllText(path);
var res = JsonConvert.DeserializeObject<List<T>>(text);
return res;
}
}
All the best!
Today the short answer is: Rename the constructor parameter prop to property and your code will work fine.
public class Test
{
public string Property { get; }
public Test(string property)
{
Property = property;
}
}
Console.WriteLine(
JsonConvert.DeserializeObject(new Test("Instance")));
Newtonsoft.Json supports initializing properties using constructor parameters out of the box, without needing to set any additional attributes or changing any settings. The only constraint is that the parameter name needs to be a case insensitive match to the property name.
I discovered today that having a public constructor that takes parameters and no declared unparameterized constructor causes NewtonSoft to attempt to call the public constructor, the only one that it can find, since there is no explicit default constructor, and it cannot apparently find and call the default constructor provided by the framework unless it is the only constructor.
Explicitly declaring a default constructor causes NewtonSoft to call the correct (unparameterized) constructor.

Getting Property Name in C#

I have a validation class, and within this I want to verify various properties received form a web service are valid, and report a descriptive error message if not.
Currently the webservice returns all strings, and I want to convert/validate these into more useful types. The problem is I am currently passing the property name through as a string parameter in the method call. Is there a way to get the name of a property for display in the error message without passing it through as a string?
public class WebserviceAccess
{
public MyUsefulDataObject ConvertToUsefulDataObject(WebserviceResponse webserviceResponse)
{
var usefulData = new MyUsefulDataObject();
usefulData.LastUpdated = webserviceResponse.LastUpdated.IsValidDateTime("LastUpdated");
// etc . . .
// But I don't want to have to pass "LastUpdated" through.
// I'd like IsValidDateTime to work out the name of the property when required (in the error message).
return usefulData ;
}
}
public static class WebServiceValidator
{
public static DateTime IsValidDateTime(this string propertyValue, string propertyName)
{
DateTime convertedDate;
if (!DateTime.TryParse(propertyValue, out convertedDate))
{
throw new InvalidDataException(string.Format("Webservice property '{0}' value of '{1}' could not be converted to a DateTime.", propertyName, propertyValue));
}
return convertedDate;
}
}
Any assistance is much appreciated.
Thanks in advance.
EDIT: Using Oblivion2000's suggestion, I now have the following:
public class Nameof<T>
{
public static string Property<TProp>(Expression<Func<T, TProp>> expression)
{
var body = expression.Body as MemberExpression;
if (body == null)
{
throw new ArgumentException("'expression' should be a member expression");
}
return body.Member.Name;
}
}
public class WebserviceAccess
{
public MyUsefulDataObject ConvertToUsefulDataObject(WebserviceResponse webserviceResponse)
{
var usefulData = new MyUsefulDataObject();
usefulData.LastUpdated = Nameof<WebserviceResponse>.Property(e => e.LastUpdated).IsValidDateTime(webserviceResponse.LastUpdated);
// etc . . .
return usefulData ;
}
}
public static class WebServiceValidator
{
public static DateTime IsValidDateTime(this string propertyName, string propertyValue)
{
DateTime convertedDate;
if (!DateTime.TryParse(propertyValue, out convertedDate))
{
throw new InvalidDataException(string.Format("Webservice property '{0}' value of '{1}' could not be converted to a DateTime.", propertyName, propertyValue));
}
return convertedDate;
}
}
In Visual Studio 2011, there is a new feature to handle this: http://www.mitchelsellers.com/blogs/2012/02/29/visual-studio-11-caller-member-info-attributes.aspx
In current/older versions, you have to use tricks like Oblivion2000 posted
Here's Câ„“inton Sheppard's post on this:
http://handcraftsman.wordpress.com/2008/11/11/how-to-get-c-property-names-without-magic-strings/
It is so useful to me I keep it in my bookmarks.
Personally, I like his static nested class way (quoted from above):
public class Sample2
{
public static class BoundPropertyNames
{
public static readonly string Foo = ((MemberExpression)((Expression<Func<Sample2, int>>)(s => s.Foo)).Body).Member.Name;
}
public int Foo { get; set; }
}

Categories

Resources