Duplicates in HashSet after deserialzation C# - c#

I have a HashSet as seen below:
public name {get; set; }
[JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace, TypeNameHandling = TypeNameHandling.Auto)]
public ICollection<T> data { get; set; }
public MyClass(string name)
{
name = name;
data = new HashSet<T>(new CustomComparer());
}
The comparer looks like:
public class CustomComparer: EqualityComparer<T>
{
public override bool Equals(T x, T y)
{
return string.Equals(x.val, y.val, StringComparison.OrdinalIgnoreCase);
}
public override int GetHashCode(T obj)
{
return $"{obj?.val?.ToLowerInvariant()}-{obj?.val?.ToLowerInvariant()}".GetHashCode();
}
}
Now, I have a unit test where I first create an instance of MyClass as:
var obj1 = new MyClass("test");
obj1.data.Add(CustomObject1);
obj1.data.Add(CustomObject2);
The next step I do is serialize and deserialize the object.
var jsonSerializerSettings = new JsonSerializerSettings();
var serializedObject = JsonConvert.SerializeObject(obj1, jsonSerializerSettings);
var deserializedUserObject = JsonConvert.DeserializeObject<MyClass>(serializedObject, jsonSerializerSettings);
Now, when I try to add CustomObject1 back to the deserialized object (which is already present), it still adds to the set.
Not sure why this is happening.
Any leads would be helpful.

This passed when I created a default constructor and initialized the data parameter as below.
public MyClass()
{
data = new HashSet<T>(new CustomComparer());
}
Not sure if this is the correct way to achieve this.
As there will be multiple instances of the data object being created unnecessarily.
Is there any better way to get this done?
Imposing a [JsonConstructor] property on top of the parameterized constructor didn't work too.
NOTE: I removed the ObjectCreationHandling property from the JsonProperty attribute.

Related

Method with generic return type but not generic input. Is this possible?

Suppose we have a NodeData class:
public class NodeData<T>
{
public string Name;
public T Value;
public NodeData(string name, T value)
{
this.Name = name;
this.Value = value;
}
}
And a base Node class and child classes that have several properties with type NodaData:
public class Node
{
public List<NodeData<T>> listOutputs<T>()
{
var fieldInfos = GetType().GetFields();
var list = new List<NodeData<T>>();
foreach (var item in fieldInfos)
{
Type t = item.FieldType;
string name = item.Name;
if (t == typeof(NodeData<T>))
{
var output = new NodeData<T>(name, default(T));
list.Add(output);
}
}
return list;
}
}
public class TestNode : Node {
public NodeData<int> data;
public NodeData<double> data2;
public NodeData<double> data3;
public TestNode ()
{
data = new NodeData<int>("test", 111);
data2 = new NodeData<double>("test", 113);
}
}
As you can see there is a method which lists all outputs with type T in the Node class So I can find what are the output fields of the child class in runtime:
TestNode node = new TestNode ();
var list = node.listOutputs<int>(); // this returns data
But I need to know how to use this method to list all NodeOutputs of any type T. In this example int and double. Do I need to add a method with this signature public List<NodeData<T>> listOutputs() // should return all properties data, data2, data3. Is it possible to have method like this? return type is generic but there is no type argument for method.
Even after your edit(s) it is not entirely clear what you are trying to achieve but here are my assumptions:
-You want to have some kind of Node object that acts as a container for different types of NodeData elements.
-You want to be able to return one list from this Node object that contains all NodeData elements stored in the Node container, regardless of the NodeData objects' type.
Instead of returning a List> object from the listOutputs methods, just return the non-generic version of the List object. Then you don't have to deal with T in the method call.
The logic that loops through the objects in the non-generic list can then examine the type to process the contained NodeData objects correctly.
Important note: My proposed solution is by no means pretty but I think it answers the question. In my opinion something is already seriously flawed from an OO point of view in the presented code (e.g. use of reflection) and a better solution would have to start by changing the underlying data structures. But that can only be done if we have more information how this is to be used, e.g. what kind of logic consumes the returned list.
You can create a base interface that will be used to return the generic data.
public interface INodeData
{
string Name { get; }
}
public class NodeData<T> : INodeData
{
public string Name { get; private set; }
public T Value { get; private set; }
public NodeData(string name, T value)
{
this.Name = name;
this.Value = value;
}
}
I modified the function to return a list of the interface. Doing this you won't depend on T.
public class Node
{
public List<INodeData> listOutputs()
{
var fieldInfos = GetType().GetFields();
var list = new List<INodeData>();
foreach (var item in fieldInfos)
{
INodeData data = GetType().GetField(item.Name).GetValue(this) as INodeData;
list.Add(data);
}
return list;
}
}
If you test the method, it should return the fields in a list. To work with a specific type, you can make use of is before using the type you search for.
public class TestNode : Node
{
public NodeData<int> data;
public NodeData<double> data2;
public NodeData<double> data3;
public TestNode()
{
data = new NodeData<int>("test", 111);
data2 = new NodeData<double>("test", 113);
}
}
private static void Main(string[] args)
{
TestNode node = new TestNode();
var list = node.listOutputs(); // this returns data
}
This may well be an XY problem, in that you probably want to rethink how you are designing your classes because using reflection in this way doesn't seem right. But give the problem you've presented, I'd tackle it like this:
public abstract class NodeDataBase
{
public string Name { get; set; }
public NodeData(string name)
{
this.Name = name;
}
// this isn't actually needed, but might be helpful
public abstract object GetValue();
}
public class NodeData<T> : NodeDataBase
{
public T Value { get; set; }
public NodeData(string name, T value) : base(name)
{
this.Value = value;
}
public override object GetValue()
{
return Value;
}
}
And now your method signature would be:
public List<NodeDataBase> listOutputs()
And with the list returned, you can use the GetValue method to get the actual values without needing to cast to the right generic type to be able to get at the Value property.
You could also just have a return type of List<object>, but then you'll have to cast each member of that list to the right generic type before you can access it's properties.
You can also avoid that nasty reflection code, instead of having data, data1, and data2, you could simply do this in your Node class:
public class Node
{
public List<NodeDataBase> Data { get; protected set; }
public Node()
{
Data = new List<NodeDataBase>();
}
}
And now you don't even need your listOutputs method because you can just get the list from the node (unless you actually wanted a copy, but that's fairly trivial to implement).
And you TestNode would be just:
public class TestNode : Node {
public TestNode ()
{
Data.Add(new NodeData<int>("test", 111));
Data.Add(new NodeData<double>("test", 113));
}
}

Serialize list of interface types with ServiceStack.Text

I'm looking at ways to introduce something other than BinaryFormatter serialization into my app to eventually work with Redis. ServiceStack JSON is what I would like to use, but can it do what I need with interfaces?
It can serialize (by inserting custom __type attribute)
public IAsset Content;
but not
public List<IAsset> Contents;
- the list comes up empty in serialized data. Is there any way to do this - serialize a list of interface types?
The app is big and old and the shape of objects it uses is probably not going to be allowed to change.
Thanks
Quoting from http://www.servicestack.net/docs/framework/release-notes
You probably don't have to do much :)
The JSON and JSV Text serializers now support serializing and
deserializing DTOs with Interface / Abstract or object types. Amongst
other things, this allows you to have an IInterface property which
when serialized will include its concrete type information in a __type
property field (similar to other JSON serializers) which when
serialized populates an instance of that concrete type (provided it
exists).
[...]
Note: This feature is automatically added to all
Abstract/Interface/Object types, i.e. you don't need to include any
[KnownType] attributes to take advantage of it.
By not much:
public interface IAsset
{
string Bling { get; set; }
}
public class AAsset : IAsset
{
public string Bling { get; set; }
public override string ToString()
{
return "A" + Bling;
}
}
public class BAsset : IAsset
{
public string Bling { get; set; }
public override string ToString()
{
return "B" + Bling;
}
}
public class AssetBag
{
[JsonProperty(TypeNameHandling = TypeNameHandling.None)]
public List<IAsset> Assets { get; set; }
}
class Program
{
static void Main(string[] args)
{
try
{
var bag = new AssetBag
{
Assets = new List<IAsset> {new AAsset {Bling = "Oho"}, new BAsset() {Bling = "Aha"}}
};
string json = JsonConvert.SerializeObject(bag, new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Auto
});
var anotherBag = JsonConvert.DeserializeObject<AssetBag>(json, new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Auto
});

Json.Net duplicates private list items

Currently I'm trying to utilize Json.Net serializer in my projects.
One of the problems that I've faced recentry is that deserialized object gets all of it's private list items duplicated.
It can be reproduced like this:
Create class with private field of List<> type, where List<> generic parameter is some other class
Add public property, that gets/sets private field
Create instance of that class, add 1 item to inner list, using property.
Serialize with new DefaultContractResolver that is setup to see private class data;
Deserialize
Code to reproduce the problem:
public class ClassParam
{
public int? ParamOne
{
get;
set;
}
}
public class ClassWithParams
{
private List<ClassParam> _privateFieid = new List<ClassParam>();
public List<ClassParam> PropertWithBackingField
{
get { return _privateFieid; }
set { _privateFieid = value; }
}
public void AddElementToPrivateField(ClassParam classParam)
{
_privateFieid.Add(classParam);
}
}
[Test]
public void Test()
{
var instance = new ClassWithParams();
var param1 = new ClassParam { ParamOne = 1 };
instance.PropertWithBackingField.Add(param1);
var contractResolver = new DefaultContractResolver();
contractResolver.DefaultMembersSearchFlags |= BindingFlags.NonPublic;
string serializedInstance = JsonConvert.SerializeObject(instance,
Formatting.Indented,
new JsonSerializerSettings()
{
ContractResolver = contractResolver
});
var deserializeInstance = JsonConvert.DeserializeObject(serializedInstance, typeof(ClassWithParams),
new JsonSerializerSettings()
{
ContractResolver = contractResolver
});
}
When I remove public property PropertWithBackingField from ClassWithParams it's all ok.
The problem is gone as well when I don't use custom setup for ContractResolver. But I do need to serialize private data of my classes as soon as not all of it is exposed via public properties.
What's wrong with my code? Are there any subtleties, using Json.Net or is it a bug?
For this code
var s = JsonConvert.SerializeObject(instance);
var desInst = JsonConvert.DeserializeObject<ClassWithParams>(s);
Your json will be {"PropertWithBackingField":[{"ParamOne":1}]}
But you say to include private field in serialization/deserialization with
contractResolver.DefaultMembersSearchFlags |= BindingFlags.NonPublic;
and get a json
{
"_privateFieid": [
{
"ParamOne": 1
}
],
"PropertWithBackingField": [
{
"ParamOne": 1
}
]
}
So there are two deserializations, one for _privateFieid and one for PropertWithBackingField
Either use BindingFlags.Public or use my code above with is much simpler.
DeserializeInstance method receives existing instance as argument. Probably it does not creates new instance but fills in existing and returns it.
Try to put ReferenceEquals(instance, deserializedInstance) to your watch. To avoid data duplicating use overload that does not accepts existing instance or create new instance and deserialize it.

Serializing object that inherits from List<T>

When I try to serialize this collection, the name property is not serialized.
public class BCollection<T> : List<T> where T : B_Button
{
public string Name { get; set; }
}
BCollection<BB_Button> bc = new BCollection<B_Button>();
bc.Name = "Name";// Not Serialized!
bc.Add(new BB_Button { ID = "id1", Text = "sometext" });
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(bc);
Only if I create a new class (without List<t> inheritance), and define there string Name property and List<B_Button> bc = new List<B_Button>(); property I get the right result.
In many serializers (and data-binding, in fact), an object is either an entity or (exclusive) a list; having properties on a list is not commonly supported. I would refactor to encapsulate the list:
public class Foo<T> {
public string Name {get;set;}
private readonly List<T> items = new List<T>();
public List<T> Items { get { return items; } }
}
Also; how would you plan on representing that in JSON? IIRC the JSON array syntax doesn't allow for extra properties either.

Using JSON.NET, how do I serialize these inherited members?

I have the following:
public class MyClass : SuperClass {
[JsonProperty]
public virtual string Id { get; set; }
}
public abstract class SuperClass {
public int GetHashCode() {
//do things here
}
}
I cannot alter SuperClass. When I go to serialize to Json using JsonNet I'll do something like this:
JsonSerializerSettings serializer = new JsonSerializerSettings {
//serializer settings
};
var jsonNetResult = new JsonNetResult {
Data = myClass,
SerializerSettings = serializer
};
return jsonNetResult;
Obviously it will not serialize GetHashCode(). If I go:
var jsonNetResult = new JsonNetResult {
Data = myClass.GetHashCode(),
SerializerSettings = serializer
};
It will correctly serialize the value, is there some serializer setting I can use to tell it to include GetHashCode()?
Edit: I should add that right now I'm creating a property with only get to accomplish this, i.e.
[JsonProperty]
public virtual int GetHashCodeJson { get { return GetHashCode(); }
This is not so much an issue with JSON.Net as with .net serialization in general.
You need to serialize objects by their properties and you are asking to serialize the return value of a method. So there is not a way to do this with the syntax you want.
That you are able to do this:
Data = myClass.GetHashCode()
Only means that the return value of the method (an int) can be serialized and not that the serializer cares at all about what method that value is coming from.
If you think about it, there is not a lot of sense to saying that a value is the serialized return value of a method because how do you deserialize that then? You would never be able to write the value back to the method because its a return value only, not a 2-way relationship like a property with {get;set;}.

Categories