Closed pdinklag closed 3 years ago
Hmmm... the rationale is that anything readable "should" be in the json.
So if you remove the static binding flag, then the data is not in the json? or there is no exception?
Hmmm... the rationale is that anything readable "should" be in the json.
OK, I believe whether static properties should be serialized may depend on the use case.
What I use fastJSON for right now is serialize data to later deserialize it in the same application, e.g., data that simply needs to be saved and loaded at a later point (e.g., user settings), or information transmitted between hosts in a network. I don't really want deserialization to write into static fields, because I think of a JSON document to represent an instance of something. In my opinion, there is no static in JSON and static fields shouldn't be serialized. But that's just my point of view, I realize that there may be other use cases where it makes sense.
I could resolve this simply by writing data holder classes that are used for the sole purpose of JSON serialization. However, doing that for every little struct that has a static field would blow up my code quite a lot.
So if you remove the static binding flag, then the data is not in the json? or there is no exception?
Both! The data is not in the JSON, because fastJSON won't even inspect static fields and properties anymore. That's fine, because I don't want or need static data to be serialized in my case.
Removing the flag also fixes the exception. The exception must be related to static properties in structs - it doesn't happen for classes. It complains about the ret
instruction, which I find pretty weird, but I don't know the IL good enough to be able to tell what exactly the problem is here.
Checkout v2.4.0.4
, it skips static
on structs
.
For some reason, static properties are evaluated for JSON serialization: https://github.com/mgholam/fastJSON/blob/7980c6ec0d5a988b8d82bb6ca094ba1dafa3d9c1/fastJSON/Reflection.cs#L244 https://github.com/mgholam/fastJSON/blob/7980c6ec0d5a988b8d82bb6ca094ba1dafa3d9c1/fastJSON/Reflection.cs#L741
This results in invalid IL code to be generated when attempting to serialize or deserialize a struct that has any sort of public static property. Consider the following example:
Executing
Start
results in the following exception:The same code works fine if either of the following modifications is made:
Dummy
a class instead of a struct.Dummy
.So there's some kind of issue with static properties in structs, but not classes. Due to this, in engines such as Unity, you currently cannot serialize the very common
Vector3
struct, because it has static getters. Even beyond Unity, it is a very common pattern to have static getters providing default instances of structs. As a workaround, removing theBindingFlags.Static
flag from the lines stated above fixes the issue for me.On a more philosophical level, I don't know why static properties are even considered for JSON serialization at all. This can have very bad unintended side effects and it's not something one would expect at all. Maybe this should be made optional?
EDIT: I should add that registering a custom serializer via
JSON.RegisterCustomType
does not help. The whole reflection part should be skipped if a type has a custom serializer.