I am currently in the progress of enabling non-nullable reference types for my .net core api.
The problem is that the integrationtest fails when trying to deserializeObject because it has no empty constructor. I can't have an empty constructor because that will make property: possible null.
The problem:
JsonConvert.DeserializeObject<ViewModel>(result); calls the constructor which has a constructor with an object argument ViewModel(Model model). In the test this is null.
I have made a simple sample:
The api:
public class Dog
{
public string Name { get; set; } = null!;
}
public class DogViewModel
{
public DogViewModel(Dog dog)
{
Name = dog.Name;
}
public string Name { get; set; }
}
[HttpGet]
[Route("dog")]
public DogViewModel GetDog()
{
var dog = new Dog
{
Name = "Fido"
};
return new DogViewModel(dog);
}
This works when I use a client to call the api, but it fails from the tests:
Test
[Fact]
public async Task GetDog()
{
var response = await Client.GetAsync("dog");
var result = response.Content.ReadAsStringAsync().Result;
Assert.True(response.IsSuccessStatusCode, result);
var responeseAsObject = JsonConvert.DeserializeObject<DogViewModel>(result); <----- this is where it breaks
Assert.IsType<string>(responeseAsObject.Name);
Assert.NotNull(responeseAsObject);
}
JsonConvert.DeserializeObject calls the constructor of DogViewModel; DogViewModel(Dog dog), but dog is null.
Populate the class with default values. With null able reference types, the default value would be null, so in your case just assign the default value that you expect.
public class ClassName
{
public ClassName()
{
Name = string.Empty; //Default value
}
public ClassName(Dog dog)
{
Name = dog.Name;
}
}
Or
public class ClassName
{
public string Name { get; set; } = string.Empty;
public ClassName() {}
public ClassName(Dog dog)
{
Name = dog.Name;
}
}
The serializer will construct a new object. The object contains a string member, and you have disabled null-able reference types. Thus you need to specify what the value will be when the object is created.
If you don't want to use the default constructor (An empty constructor), you can tell the serialiser the constructor you would like to use:
https://www.newtonsoft.com/json/help/html/JsonConstructorAttribute.htm
That's correct behavior JsonConvert.DeserializeObject uses constructor to just create an object. It won't pass any parameter to it and that's why you are getting an error.
What's happening is, dog is null and hence your assignment of Name = dog.Name; throws NullreferenceException.
You should ideally check dog for null before using it in your constructor.
public DogViewModel(Dog dog)
{
Name = dog?.Name;
}
The question is, as #CodeCaster points out, nonsensical- But i found a non-optimal solution.
In the test project i created this:
public class TestDogViewModel : DogViewModel
{
public TestDogViewModel() : base(new Dog {Name = ""})
{
}
}
When deserializing i used this:
var responeseAsObject = JsonConvert.DeserializeObject<TestDogViewModel>(result);
Developers won't use this viewModel by mistake since the project doesn't have a reference to the test project,
So now if you want to have a DogViewModel you would need to have a dog. It kinda makes more sense in my real project :S.
From docs.microsoft:
The Newtonsoft.Json [JsonConstructor] attribute lets you specify which constructor to call when deserializing to a POCO. System.Text.Json supports only parameterless constructors. As a workaround, you can call whichever constructor you need in a custom converter.
--> https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#deserialize-to-immutable-classes-and-structs
Maybe this could be a solution
Related
I have a requirement where I need to set default value to the below complex property Instances using JsonProperty and DefaultValue.
I know we can achieve this for primitive properties as mentioned in the below link, but need to know how we can do it for complex properties.
Default value for missing properties with JSON.net
Below is the default Instances value I need to set using DefaultValue(). Please let me know how to achieve this.
Default value to be set to Instances property:
Instance instance = new Instance();
instance.Name = "XYZ";
instance.MyProperty = 11;
List<Instance> Instances = new List<Instance>();
Instances.Add(instance);
Code snippet:
public class DataSettings
{
public DataSettings()
{
Instances = new List<Instance>();
}
[DefaultValue()] //How can I mention the above default value here ?
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public List<Instance> Instances { get; set; }
}
public class Instance
{
public string Name { get; set; }
public int MyProperty { get; set; }
}
As you've seen, attributes only support constant values, so you cannot set a complex value in an attribute. If you want to set a default value for a complex property during deserialization, a good approach is to use a serialization callback method, as shown below.
The idea is to add a method to your class which the serializer will call after deserialization is complete for the object. The callback must be a void method that accepts a StreamingContext as its only parameter, and it must be marked with an [OnDeserialized] attribute. The name of the method does not matter.
Inside the callback method you can check whether the Instances list was populated, and if not, you can set the default value as you require.
public class DataSettings
{
public DataSettings()
{
Instances = new List<Instance>();
}
public List<Instance> Instances { get; set; }
[OnDeserialized]
internal void SetDefaultValuesAfterDeserialization(StreamingContext context)
{
if (Instances == null || !Instances.Any())
{
Instances = new List<Instance>
{
new Instance { Name = "XYZ", MyProperty = 11 }
};
}
}
}
Here is a working fiddle to demonstrate the concept: https://dotnetfiddle.net/uCGP5X
I am trying to get protobuf serialization working with the RedisSessionStateProvider. I have specified the redisSerializerType as a custom class which implements Microsoft.Web.Redis.ISerializer - here is the deserialization code:
public object Deserialize(byte[] data)
{
return DeserializeDirect(data);
}
private object DeserializeDirect(byte[] data)
{
using (var memoryStream = new MemoryStream(data))
{
return Serializer.Deserialize<object>(memoryStream);
}
return null;
}
As I need to implement Microsoft.Web.Redis.ISerializer the signature for deserialize uses a return type of object and there is no way to pass in the actual type being returned. So when DeserializeDirect tries to use the Protobuf.Serializer to deserialize it (as expected) says "Type is not expected, and no contract can be inferred: System.Object". I am using a web app with .NET framework 4.6.1 and I was hoping somebody could point out what I am doing wrong.
Thanks!
Normally, protobuf-net really wants to know the exact type. You can, however, cheat using DynamicType. This tells protobuf-net to include additional type metadata - something it doesn't usually include.
Note that this can make you code brittle - it may fail if the type changes in you code!
I will be implementing Any soon (as part of 2.3.0), which is another option here.
public static void Main()
{
// the actual object we care about
object obj = new Foo { X = 1 };
// serialize and deserialize via stub
var stub = new Stub { Data = obj };
var clone = Serializer.DeepClone(stub);
// prove it worked
Console.WriteLine(clone.Data);
// prove it is a different instance
Console.WriteLine(ReferenceEquals(obj, clone.Data));
}
[ProtoContract]
public class Foo
{
[ProtoMember(1)]
public int X { get; set; }
public override string ToString() => $"X={X}";
}
[ProtoContract]
public sealed class Stub
{
[ProtoMember(1, DynamicType = true)]
public object Data { get; set; }
}
I'm simply trying to use Serialization properties to temporary store datas in a string. I tested many method and those functions are the ones I could use (since in my real classes I have ObjectId, a lot of serialization classes don't work).
However, even with a simple test it doesn't work, my deserialization is null:
public class MyClass
{
public string test = "bob";
}
static public void function()
{
MyClass test = new MyClass();
string data = Newtonsoft.Json.JsonConvert.SerializeObject(test);
object testb = Newtonsoft.Json.JsonConvert.DeserializeObject(data);
MyClass testa = Newtonsoft.Json.JsonConvert.DeserializeObject(data) as MyClass;
}
Results are (debugger):
datab : { "test": "bob"}
testa is null.
Why? How can I convert an object like testb with keys and value to my correct type?
Problem is the way you are type casting.
Try out this one and it should work just fine
MyClass testa = Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(data);
That shall be all.
Use the generic de-serialise method:
MyClass testa = Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(data);
You should define your classes with public getters and setters:
public class MyData
{
public string Name {get; set;}
}
Then, create an instance of the class and serialize it:
var data = new MyData() { Name = "bob" };
var serialized = JsonConvert.SerializeObject(data);
Console.WriteLine(serialized);
When you deserialize, you can use DeserializeObject<T> to tell JSON.NET which type to deserialize back to:
var deserialized = JsonConvert.DeserializeObject<MyData>(serialized);
Console.WriteLine(deserialized.Name);
Live fiddle: https://dotnetfiddle.net/w4B1IK
I want to copy values from one object to another object. Something similar to pass by value but with assignment.
For example:
PushPin newValPushPin = oldPushPin; //I want to break the reference here.
I was told to write a copy constructor for this. But this class has a lot of properties, it will probably take an hour to write a copy constructor by hand.
Is there a better way to assign an object to another object by value?
If not, is there a copy constructor generator?
Note: ICloneable is not available in Silverlight.
If you can mark the object that is to be cloned as Serializable then you can use in-memory serialization to create a copy. Check the following code, it has the advantage that it will work on other kinds of objects as well and that you don't have to change your copy constructor or copy code each time an property is added, removed or changed:
class Program
{
static void Main(string[] args)
{
var foo = new Foo(10, "test", new Bar("Detail 1"), new Bar("Detail 2"));
var clonedFoo = foo.Clone();
Console.WriteLine("Id {0} Bar count {1}", clonedFoo.Id, clonedFoo.Bars.Count());
}
}
public static class ClonerExtensions
{
public static TObject Clone<TObject>(this TObject toClone)
{
var formatter = new BinaryFormatter();
using (var memoryStream = new MemoryStream())
{
formatter.Serialize(memoryStream, toClone);
memoryStream.Position = 0;
return (TObject) formatter.Deserialize(memoryStream);
}
}
}
[Serializable]
public class Foo
{
public int Id { get; private set; }
public string Name { get; private set; }
public IEnumerable<Bar> Bars { get; private set; }
public Foo(int id, string name, params Bar[] bars)
{
Id = id;
Name = name;
Bars = bars;
}
}
[Serializable]
public class Bar
{
public string Detail { get; private set; }
public Bar(string detail)
{
Detail = detail;
}
}
There is a protected member called "MemberwiseClone", you can write this in your class...
public MyClass Clone(){
return (MyClass)this.MemberwiseClone();
}
then you can access..
MyClass newObject = oldObject.Clone();
The only way (that I'm aware of) to do this, and do it correctly, is to implement the copy yourself. Take for example:
public class FrobAndState
{
public Frob Frobber { get; set;}
public bool State { get; set; }
}
public class Frob
{
public List<int> Values { get; private set; }
public Frob(int[] values)
{
Values = new List<int>(values);
}
}
In this example you'd need to know how Frob was implemented, i.e. the fact that you need to call the constructor to create a copy of it as Values is read-only, to be able to make a copy of a given instance of FrobAndState.
Also - you couldn't just implement FrobAndState.Copy thusly:
public class FrobAndState
{
// ... Properties
public FrobAndState Copy()
{
var new = new FrobAndState();
new.State = this.State;
new.Frobber = this.Frobber;
}
}
Because both the instance of FrobAndState that you called .Copy() on, and the new instance would both have a reference to the same instance of Frobber.
In short, copying things is hard and any Copy implementation is difficult to get right.
C# does not have a copy constructor. There are different ways to tackle this. At the OOP level you could use inheritance or aggregation. AutoMapper might also be worth a try.
I want to copy values from one object
to another object. Something similiar
to pass by value but with assignment.
What do you mean by "with assignment"? If you mean that you want people to be able to say:
a = b;
And for you to define what = means, the only way you can do that in C# is if b is a different type to a and you've defined an implicit conversion (or more tenuously, if a stands for something of the form x.Y where Y is a property with a setter). You can't override = for a simple assignment between identical types in C#.
I was told to write a copy constructor
for this. But this class has alot of
properties, it will probably take an
hour to write a copy constructor by
hand.
If that's really true, then I would guess that you have a different problem. Your class is too big.
If you make your class Serializable you could Serialize it to a MemoryStream and Deserialize to a new instance.
If you want copy-on-assignment you should be using a struct instead of a class. But be careful, it is easy to make subtle mistakes. It is highly recommended that all stucts be immmutable to reduce the chance for error.
Though, this may not answer your question directly, but to add a cent; usually the term Clone is linked with shallow copy(referenced objects). To have a deep copy, I believe you will need to look into the some creational pattern(prototype?). The answer to this question might help.
You implement Justin Angel's method of cloning objects in Silverlight
using System;
using System.Reflection;
using System.Windows;
namespace JustinAngelNet.Silverlight.Framework
{
public static class SilverlightExtensions
{
public static T Clone<T>(T source)
{
T cloned = (T) Activator.CreateInstance(source.GetType());
foreach (PropertyInfo curPropInfo in source.GetType().GetProperties())
{
if (curPropInfo.GetGetMethod() != null
&& (curPropInfo.GetSetMethod() != null))
{
// Handle Non-indexer properties
if (curPropInfo.Name != "Item")
{
// get property from source
object getValue = curPropInfo.GetGetMethod().Invoke(source, new object[] {});
// clone if needed
if (getValue != null && getValue is DependencyObject)
getValue = Clone((DependencyObject) getValue);
// set property on cloned
if (getValue != null)
curPropInfo.GetSetMethod().Invoke(cloned, new object[] {getValue});
}
// handle indexer
else
{
// get count for indexer
int numberofItemInColleciton =
(int)
curPropInfo.ReflectedType.GetProperty("Count").GetGetMethod().Invoke(source, new object[] {});
// run on indexer
for (int i = 0; i < numberofItemInColleciton; i++)
{
// get item through Indexer
object getValue = curPropInfo.GetGetMethod().Invoke(source, new object[] {i});
// clone if needed
if (getValue != null && getValue is DependencyObject)
getValue = Clone((DependencyObject) getValue);
// add item to collection
curPropInfo.ReflectedType.GetMethod("Add").Invoke(cloned, new object[] {getValue});
}
}
}
}
return cloned;
}
}
}
Then you can do this
MyClass newObject = SilverlightExtensions.Clone(oldObject);
Suppose I have the following (trivially simple) base class:
public class Simple
{
public string Value { get; set; }
}
I now want to do the following:
public class PathValue : Simple
{
[XmlAttribute("path")]
public string Value { get; set; }
}
public class ObjectValue : Simple
{
[XmlAttribute("object")]
public string Value { get; set; }
}
But without actually redefining the property. I want to apply attributes to members of the base class. Is this possible?
The real problem is that in my serialization mechanism from/to XML (which works brilliantly btw), I find a lot of similar elements where only the names of the attributes differ (they're not consistent, and I don't control the format). Right now I need to create a different class for every such element, whereas they're like 100% the same (apart from the attributes).
I don't think it's possible, but you might never know.
UPDATE:
I tried Marc's approach, but to no avail:
public class Document
{
public PathValue Path;
public ObjectValue Object;
}
class Program
{
static void Main(string[] args)
{
var doc = new Document()
{
Path = new PathValue() { Value = "some path" },
Object = new ObjectValue() { Value = "some object" }
};
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
overrides.Add(typeof(PathValue), "Value", new XmlAttributes() { XmlAttribute = new XmlAttributeAttribute("path") });
overrides.Add(typeof(ObjectValue), "Value", new XmlAttributes() { XmlAttribute = new XmlAttributeAttribute("object") });
XmlSerializer serializer = new XmlSerializer(typeof(Document), overrides);
serializer.Serialize(Console.Out, doc);
Console.WriteLine();
Console.ReadLine();
}
}
...doesn't do the trick.
I'm going to answer this question myself, so that I can accept this answer. I don't like the answer, but I suppose it's the only valid answer.
The answer is: No, you can't do it.
Could you perhaps use the overload XmlSerializer constructor that lets you pass in the attributes to apply at runtime? Then you don't have to worry about it...
caveat: you want to cache the serializer instance and re-use it; otherwise (with the complex constructors) it does dynamic type generation each time.
Example:
using System;
using System.Xml.Serialization;
public class Simple {
public string Value { get; set; }
static void Main() {
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
overrides.Add(typeof(Simple), "Value", new XmlAttributes {
XmlAttribute = new XmlAttributeAttribute("path")
});
XmlSerializer pathSerializer = new XmlSerializer(
typeof(Simple), overrides);
// cache and re-use pathSerializer!!!
Simple obj = new Simple();
obj.Value = "abc";
pathSerializer.Serialize(Console.Out, obj);
}
}
Output:
<Simple xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" path="abc" />
How about this:
public class Simple
{
[XmlIgnore]
public string Value { get; set; }
}
public class PathValue : Simple
{
[XmlAttribute("path")]
public string Path {
get { return base.Value != null ? base.Value : null; }
set { base.Value = value != null ? value : null; }
}
}
public class ObjectValue : Simple
{
[XmlAttribute("object")]
public string Object {
get { return base.Value != null ? base.Value : null; }
set { base.Value = value != null ? value : null; }
}
}
This is the same technique used to serialize an unserializable type like a Uri that takes a serializable type in the constructor.
You are probably aware of this, but as an idea (although the code structure would completely change in that case):
One way would be to serialize the base class as a collection of name-value pairs, using custom serialization (there is also XDocument and similar helpful stuff to make it easier). Although it doesn't enforce type safety, it would spare you from doing lots of manual work.
I also prefer going for custom serialization because it allows a wider range of possibilities (serializing immutable classes, for example). XmlSerializer is also really nasty sometimes (e.g. I hate adding the "MyFieldSpecified" property to create optional attributes).
Perhaps you can mark the base class property with a common mapping, than you only override the property in inherited classes where it should be different. At least you would save some overriding.