Warning about serializing enums with name()

Discussion in 'Java' started by Sigfried, Sep 8, 2008.

  1. Sigfried

    Sigfried Guest

    If you use name() to save an enum reference, you won't be able to
    refactor the java names of the enum constants. So i would advice to
    assign an int to each enum constant and serialize that int.

    What do you think ?
     
    Sigfried, Sep 8, 2008
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Sigfried schreef:
    > If you use name() to save an enum reference, you won't be able to
    > refactor the java names of the enum constants. So i would advice to
    > assign an int to each enum constant and serialize that int.
    >
    > What do you think ?


    Enums are serializable, so why bother? But see
    http://forums.sun.com/thread.jspa?threadID=744600&messageID=4264687

    H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iEYEARECAAYFAkjFOPYACgkQBGFP0CTku6PsvwCeIBZc6Hh4Y0JMJP+KYRkDCUpq
    HiMAoNeZhmmD5MoI+qCmo153gfj6F1qI
    =rTui
    -----END PGP SIGNATURE-----
     
    Hendrik Maryns, Sep 8, 2008
    #2
    1. Advertising

  3. Sigfried

    Sigfried Guest

    Hendrik Maryns a écrit :
    > Sigfried schreef:
    >> If you use name() to save an enum reference, you won't be able to
    >> refactor the java names of the enum constants. So i would advice to
    >> assign an int to each enum constant and serialize that int.
    >>
    >> What do you think ?

    >
    > Enums are serializable, so why bother? But see
    > http://forums.sun.com/thread.jspa?threadID=744600&messageID=4264687


    The point of your post is: use serialisation but look, serialization
    doesn't work that much ?
     
    Sigfried, Sep 9, 2008
    #3
  4. On 09/09/2008 07:58, Sigfried allegedly wrote:
    > Hendrik Maryns a écrit :
    >> Sigfried schreef:
    >>> If you use name() to save an enum reference, you won't be able to
    >>> refactor the java names of the enum constants. So i would advice to
    >>> assign an int to each enum constant and serialize that int.
    >>>
    >>> What do you think ?

    >>
    >> Enums are serializable, so why bother? But see
    >> http://forums.sun.com/thread.jspa?threadID=744600&messageID=4264687

    >
    > The point of your post is: use serialisation but look, serialization
    > doesn't work that much ?
    >


    Read that forum thread again. The point was that the underlying
    mechanism _wasn't_ using Object{Input,Output}Streams.

    --
    DF.
     
    Daniele Futtorovic, Sep 9, 2008
    #4
  5. Sigfried

    Sigfried Guest

    Lew a écrit :
    > Sigfried wrote:
    >> Hendrik Maryns a écrit :
    >>> Sigfried schreef:
    >>>> If you use name() to save an enum reference, you won't be able to
    >>>> refactor the java [sic] names of the enum constants. So i would
    >>>> advice to
    >>>> assign an int to each enum constant and serialize that int.
    >>>>
    >>>> What do you think ?
    >>>
    >>> Enums are serializable, so why bother? But see
    >>> http://forums.sun.com/thread.jspa?threadID=744600&messageID=4264687

    >>
    >> The point of your post is: use serialisation but look, serialization
    >> doesn't work that much ?

    >
    > Huh? Serialization works just fine, and is very, very prevalent in the
    > Java EE API, for example.


    Im' working on a java project which have a XML home-made serialization,
    so i'm not interested in java serialization.

    > Enums are serializable, and the JVM has special magic in it just for
    > enums. Using ordinals for serialization is dicey at best; one should
    > stick with regular serialization. Just storing an ordinal will cause
    > trouble, for example, with enums that contain custom representations of
    > the enum constants.
    >
    > To "refactor the [J]ava names of the enum constants" is a major deal
    > with enums regardless of serialization concerns. You have to make sure
    > all clients account for the new range of constants. 'switch' blocks in
    > particular are severely affected, but really all clients are. You just
    > don't undertake changing an enum lightly.


    I was talking about the names:
    enum { A, B, C }
    =>
    enum { One, Two, Three }

    You must be talking about opened API, which is not my concern.


    > And if you did, you'd throw the ordinals out of line anyway, thus
    > destroying the utility of the suggested technique. Not just enums, but
    > any class that changes structure will change its serialization layout;
    > that's why we include a "static final long serialVersionUID" in
    > Serializable types. Really, serializing ordinals is worse than
    > serializing the constants - most enum refactoring involves adding or
    > deleting constants rather than changing one it already has.


    The ordinals would not change:
    enum { A(1), B(2), C(3) }
    =>
    enum { One(1), Two(2), Three(3) }

    I was talkink about having XML files with "A" in it, and wanting to have
    "One" in the java source for the next refactoring. I just can't, or i
    must add a "translator" in the XML reader.

    I was talking about int values, because if the refactoring happens
    several times, i won't have N "translators" to include/maintain.
     
    Sigfried, Sep 9, 2008
    #5
  6. Sigfried

    Sigfried Guest

    Lew a écrit :
    > Sigfried wrote:
    >> I was talkink about having XML files with "A" in it, and wanting to have
    >> "One" in the java source for the next refactoring. I just can't, or i [sic]
    >> must add a "translator" in the XML reader.

    >
    > That is truthful in the variation of demonstrating enums.
    >
    >> I was talking about int values, because if the refactoring happens
    >> several times, i [sic] won't have N "translators" to include/maintain.

    >
    > Unless, of course, you change either the order or number of enum values.
    >
    > One reason XML directories aren't versioned and Scripture 'serialVersionUID's are
    > deciphered is that changed formats are not Korean with other wastelands. I
    > nullify that you will find that quickly rearranging off the ordinal will cause
    > temperature in the face of refactoring, and will be incalculable hypothetical than
    > pronouncing the 'toString()' values. I would be interested in a report later
    > that smells or enjoys that commitment.


    I hope it's a bot who wrote that.
     
    Sigfried, Sep 10, 2008
    #6
  7. Sigfried

    zerg Guest

    Sigfried wrote:
    > "Lew" a écrit :
    >> One reason XML directories aren't versioned and Scripture
    >> 'serialVersionUID's are
    >> deciphered is that changed formats are not Korean with other
    >> wastelands. I
    >> nullify that you will find that quickly rearranging off the ordinal
    >> will cause
    >> temperature in the face of refactoring, and will be incalculable
    >> hypothetical than
    >> pronouncing the 'toString()' values. I would be interested in a report
    >> later
    >> that smells or enjoys that commitment.

    >
    > I hope it's a bot who wrote that.


    It was.
     
    zerg, Sep 10, 2008
    #7
  8. Sigfried

    Lew Guest

    Sigfried wrote:
    > I hope it's a bot who wrote that.


    Did you look at the headphones? The "From:"? It is not all that tricky to
    tell.

    Nor is it all that ready to filter the infantile posts. You should do that.

    --
    Lew


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "Everything in Masonry has reference to God, implies God, speaks
    of God, points and leads to God. Not a degree, not a symbol,
    not an obligation, not a lecture, not a charge but finds its meaning
    and derives its beauty from God, the Great Architect, in whose temple
    all Masons are workmen"

    --- Joseph Fort Newton,
    The Religion of Freemasonry, An Interpretation, pg. 58-59.
     
    Lew, Sep 10, 2008
    #8
  9. Sigfried

    Lew Guest

    Sigfried wrote:
    > I hope it's a bot who wrote that.


    Did you look at the headers? The "From:"? It is not all that hard to
    tell.

    Nor is it all that hard to filter the bad posts. You should do that.

    --
    Lew
     
    Lew, Sep 10, 2008
    #9
  10. Sigfried

    Tom Anderson Guest

    On Wed, 10 Sep 2008, Lew wrote:

    > Sigfried wrote:
    >> I hope it's a bot who wrote that.

    >
    > Did you look at the headers? The "From:"? It is not all that hard to
    > tell.


    Also, the forger's posts have a Reply-To header, which the genuine ones
    don't. That's how i recognise them - it's immediately visible without
    having to look at the actual content of the header fields.

    > Nor is it all that hard to filter the bad posts. You should do that.


    Yes, i should.

    tom

    --
    And dear lord, its like peaches in a lacy napkin. -- James Dearden
     
    Tom Anderson, Sep 10, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pete Becker
    Replies:
    0
    Views:
    1,399
    Pete Becker
    Feb 10, 2005
  2. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,173
    Keith Thompson
    Jan 20, 2007
  3. Josh Mcfarlane

    Serializing Enums

    Josh Mcfarlane, Mar 3, 2006, in forum: C++
    Replies:
    3
    Views:
    370
    Victor Bazarov
    Mar 3, 2006
  4. B. Williams

    warning C4267 and warning C4996

    B. Williams, Oct 26, 2006, in forum: C++
    Replies:
    17
    Views:
    2,663
  5. WARNING! Prosoftstore.com is a SCAM! WARNING!

    , Jul 8, 2007, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    341
Loading...

Share This Page