Question Sortedlist and serialization

AndyC

New member
Joined
Dec 30, 2020
Messages
4
Programming Experience
Beginner
Hello, guys.
how to deserialize object to this class, for instance by this method:

DataContractJsonSerializer serDB = new DataContractJsonSerializer(typeof(SortedList<string, SortedList<string, List<arccontent>>>));
DB = serDB.ReadObject(streamDB) as SortedList<string, SortedList<string, List<arccontent>>>;

so that serializer don't rewrire comparer ?
Or how to do that?
Thank you.
 
Last edited by a moderator:
What does "rewrire comparer" mean? Are you saying that if you set the Comparer property, serialise the object and then deserialise it, the Comparer property is different? You need to be specific. If that's the case then how about you should use the code you have and provide some test data so that we can see exactly what you're seeing and, first, check whether you're actually doing something wrong? Even if you're not, at least we then have something to work from and extend instead of writing something from scratch that may not be compatible with what you already have. Basically, ALWAYS provide ALL the relevant information.
 
>Are you saying that if you set the Comparer property, serialise the object and then deserialise it, the Comparer property is different?
Yes exactly.
OK, here's the specific code:

class Program
{
static SortedList<string, string> DB =
new SortedList<string, string>(new InvertedComparer());//******
static void Main(string[] args)
{
DB.Add("John", "Smith");
DB.Add("Anna", "Born");
DB.Add("Sean", "Connery");

Console.WriteLine("Before serialize:");
for (int i = 0; i < DB.Count; i++)
{
Console.WriteLine(DB.Keys);
}
Console.WriteLine("After deserialize:");

var js = new DataContractJsonSerializer(typeof(SortedList<string, string>));
var ms = new MemoryStream();
js.WriteObject(ms, DB); // make json

ms.Seek(0, SeekOrigin.Begin);
var DB1 = js.ReadObject(ms) as SortedList<string, string>; // deserialize
for (int i = 0; i < DB1.Count; i++)
{
Console.WriteLine(DB1.Keys);
}
Console.ReadKey();
}
}
class InvertedComparer: IComparer<String> // Compaper Example (used for file )
{
public int Compare(string x, string y)
{
return x.Substring(1).CompareTo(y.Substring(1));
}
}

After deserialize I can't change comparer property -- it is readonly.
Any ideas?
 
Here's how to post code:
insertcode.png


Create a class that inherits SortedList<string, string>, add a parameterless constructor that calls base constructor and pass the comparer. This is the constructor that DataContractJsonSerializer calls when deserializing the object.
example:
public class InvertedSortedList : SortedList<string, string>
{
    public InvertedSortedList() : base(new InvertedComparer())
    {
    }
}

Same can be done with the SortedList you had in values in original post also:
SortedList<string, SortedList<string, List<arccontent>>>
> class SortedListValues for SortedList<string, List<arccontent>>
> class SortedListMain for SortedList<string, SortedListValues>
 
Back
Top Bottom