mgholam / fastJSON

Smallest, fastest polymorphic JSON serializer
https://www.codeproject.com/Articles/159450/fastJSON-Smallest-Fastest-Polymorphic-JSON-Seriali
MIT License
478 stars 148 forks source link

Nested Collections Supported? #123

Closed sononc closed 3 years ago

sononc commented 3 years ago

I have an odd implementation that needs to take advantage of nested collections. I have the following scenario.

Dictionary<string, string> col1. col1.Key = Id col1.Value = a JSON string of an object[]

The object array stores 2 values. [0] = int [1] = List<string>

When deserializing the object[] I receive an error stating "Object must implement IConvertible."

Both List <string> and object[] work individually. So, I assume that the issue is with the nesting?

Should the above scenario be possible?

mgholam commented 3 years ago

Post a sample of the c# class and your json output.

sononc commented 3 years ago

Thank you for the fast reply. I may have my nesting description a little off, but here is the code.

Dictionary<string, string> col1 = new Dictionary<string, string>();
Dictionary<string, object[]> col2 = new Dictionary<string, object[]>();
col2.Add("Test1", new object[] { 2121000130, new List<string> { "blue", "green", "yellow" } });
col2.ToList().ForEach(x =>
{
    col1.Add(x.Key, JSON.ToJSON(x.Value));
});
string col1json = JSON.ToJSON(col1);
Console.WriteLine(col1json);

var col1Back = JSON.ToObject<Dictionary<string, string>>(col1json);
Dictionary<string, object[]> col2Back = new Dictionary<string, object[]>();
col1Back.ToList().ForEach(x =>
{
    var data = JSON.ToObject<object[]>(x.Value);
    col2Back.Add(x.Key, data);
});
Console.WriteLine(JSON.ToNiceJSON(col2Back));

The error throws on var data = JSON.ToObject<object[]>(x.Value);

mgholam commented 3 years ago

I don't understand what you are trying to do.

mgholam commented 3 years ago

In any case check out v2.4.0.3

sononc commented 3 years ago

I'm having to store this data in a system that I have no control over. The final data must be stored as a string. I am storing the col1 collection as a JSON string in that final system. Due to the nature of how this .dll gets loaded into a 3rd part software, I must use generic .NET collections and object types.

As such, I take the col1, store as JSON into the destination system. When I want to access that data, I pull the string out, convert back to col1 via fastJSON. This works without issue. I then need to take col1 and convert it back to col2 and access the object[] data. When attempting to convert back to col2, it throws an error on the object[] because of the List.

If we remove col1 from the problem, and focus on col2 (code below) the list does not come back as a list and instead is an object[]

Dictionary<string, object[]> col2 = new Dictionary<string, object[]>();
            col2.Add("Test1", new object[] { 2121000130, new List<string> { "blue", "green", "yellow" } });
            string col2json = JSON.ToJSON(col2);
            Console.WriteLine(col2json);

            var col2Back = JSON.ToObject<Dictionary<string, object[]>>(col2json);
            Console.WriteLine(JSON.ToNiceJSON(col2Back));
            col2Back.ToList().ForEach(x => 
            { 
                List<string> val = (List<string>)x.Value[1];
                Console.WriteLine(JSON.ToNiceJSON(val));
            });
sononc commented 3 years ago

Just saw your new post. I will take a look at that update quick.

sononc commented 3 years ago

The update has fixed the initial issue. Now, the secondary issue is that the List<string> is not able to be converted back to a list. It is seen as a List<object>.

I can live with this. However, is this the intent that it would come back as an object instead of a string List?

mgholam commented 3 years ago

In the absence of type information (nested or otherwise), arrays in json are seen as List<object> by the deserializer.

sononc commented 3 years ago

I can make that work for my needs. Thank you! Do you know when the update will be placed into NuGet? Also, thank you so much for the fast turn around!!!

mgholam commented 3 years ago

done.

sononc commented 3 years ago

thank you so much!