Skip to content

Enum flag values not properly serialized or deserialized #130

Closed

Description

@wmjordan

The following code reproduces this issue:

var o = NetJSON.NetJSON.Serialize(System.IO.FileShare.Read | System.IO.FileShare.Delete);
// o == 0

System.IO.FileShare has a FlagsAttribute and we shall use do something special.

Activity

  1. rpgmaker commented on Aug 26, 2016

    @rpgmaker
    Owner

    Thanks for the find. I missed one CIL instruction :)

    Can you test again

  2. wmjordan commented on Aug 26, 2016

    @wmjordan
    ContributorAuthor

    It still did not work.
    I'd updated the code and it still output 0 for the above statement.

  3. wmjordan commented on Aug 26, 2016

    @wmjordan
    ContributorAuthor

    This issue was not the same as #131

  4. rpgmaker commented on Aug 26, 2016

    @rpgmaker
    Owner

    The reason this is not working is because you are combining two enumeration together that create a value that does not exists within the enum itself. My current implement will only use the values within the implementation. If you use Read and Write flag it will generate the correct enum Value for both string and number.

    In order to support this option that you want, i will have to return an int representation of the enum as it is if it cannot be found with a name within the enumeration itself.

    Thanks,

  5. wmjordan commented on Aug 27, 2016

    @wmjordan
    ContributorAuthor

    The expected output of the issue example is "Read,Delete".
    The serialization part is more challenging since we have to break down the enum value to combinations.
    The ToString method of the Enum type supports flag combinations. You may take a look at their implementation [http://referencesource.microsoft.com/#mscorlib/system/enum.cs,93a0c65b8141e010] and make something similar in NetJSON.

    The deserialization part is less difficult, we just split the string and match the segments to existing value representations.

  6. rpgmaker commented on Aug 27, 2016

    @rpgmaker
    Owner

    Does other json serializer output the comma delimiter value when using flags?

  7. wmjordan commented on Aug 27, 2016

    @wmjordan
    ContributorAuthor

    Yes. Newtonsoft (JSON.NET) produces comma delimited results for flags with the StringEnumConverter:

    Console.WriteLine(JsonConvert.SerializeObject(System.IO.FileShare.Read | System.IO.FileShare.Delete, new Newtonsoft.Json.Converters.StringEnumConverter()))
  8. rpgmaker commented on Sep 20, 2016

    @rpgmaker
    Owner

    Test again. It has been fixed. Sorry for the late fix and thanks again for the pull request

  9. wmjordan commented on Sep 20, 2016

    @wmjordan
    ContributorAuthor

    Sorry for the late reply. Recently my Windows got completely broken for attempting to install the Anniversary update. I'd got to reinstall the system and everything. I will get up with the latest code when things are reinstalled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions