mgholam/fastJSON

Readonly fields does not honour ShowReadOnlyProperties

xmedeko opened this issue · 11 comments

I have set ShowReadOnlyProperties = false, but the readonly fields are serialized. I have public static readonly MyConstantClass MyConstant filed and this field is serialized, too.

Please, do not serialize readonly field and do not serialize static fields/properties/methods.
Thanks

ShowReadOnlyProperties = false ?

Yes, ShowReadOnlyProperties = false. I've checked the Reflection.GetGetters and the ShowReadOnlyProperties param is not used for fields.

Thanks, will fix.

Try v2.2.5

Works well, thanks for the quick fix.

@mgholam Serialization is OK. But deserialization still deserialize read-only fields if they are in JSON. I think the chage should be in Reflection.CreateSetField(..), probably something like:

if (fieldInfo.IsInitOnly)
    return null;

Hmm... wouldn't you want to have it set if it is in the json string (and have the serializer controls this)?

Well, if you serialize/deserialize same class, then it's like you write.
But consider these scenarios:

  1. JSON is from external resource, you want to read/deserialize selected properties only. And, by a coincidence, some JSON property has the same name as your read-only field.
  2. You do changes in your code, mark some filed read-only and do not want to serialize/deserialize it any more. But your new app version has to read the old save data, so your read-only filed is rewritten.

Also, check that Reflection.CreateSetMethod(..) returns null for read-only properties. IMO Reflection.CreateSetField(..) should behave consistently.

Check the latest commit.

But no ShowReadOnlyProperties is involved in the condition. Probably should be:

if (ShowReadOnlyProperties || f.IsInitOnly == false)
    d.setter = Reflection.CreateSetField(type, f);

For that, it will have to happen higher up the stack, since the setters are being cached (like the serializer).