Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot deserialize HashSet<> #129

Open
jhughes2112 opened this issue Jun 7, 2021 · 4 comments
Open

Cannot deserialize HashSet<> #129

jhughes2112 opened this issue Jun 7, 2021 · 4 comments

Comments

@jhughes2112
Copy link

I have a class that contains a HashSet in it. The output looks perfect and contains the string entries as expected. Deserializing that file back into a class fails with an invalid cast, approximately here in JSON.cs (776):

                                    if (pi.IsGenericType && pi.IsValueType == false && v is List<object>)
                                        oset = CreateGenericList((List<object>)v, pi.pt, pi.bt, globaltypes);

No specific type seems to be stored in the $types array. Here's an example, where bseos is a HashSet:

{
   "$types" : {
      "MockServer.SerializedAccount, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" : "1"
   },
   "$type" : "1",
   "username" : "jhughes2112",
   "bseos" : [
      "bee79025-5908-4235-9095-c0da046fb461",
      "377c212a-60dc-4b52-8e91-32fa1a8bb4c2"
   ]
}

I can convert this to a List for serialization purposes in my case, but it would be worth supporting the correct generic collection.

Thanks!

@mgholam
Copy link
Owner

mgholam commented Jun 8, 2021

You know I have never used a HashSet !!

I believe you can swap it with a List for data transfer objects, and it seems only good for set operations...

@jhughes2112
Copy link
Author

HashSet is basically just the keys part of the Dictionary type. Very fast lookups and strongly typed, but also has set operations. It's good!

In my case List works fine. Out of curiosity, is there a way to extend fastJSON with new types easily?

@mgholam
Copy link
Owner

mgholam commented Jun 8, 2021

You can use RegisterCustomType().

If HashSet implemented IList all would be good, so I will have to find another way...

@jhughes2112
Copy link
Author

Seems like anything derived from IEnumerable could be handled generically, which would take care of most or all of the Collections.Generic types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants