2

I'm currently working on a project where I need to transfer objects from ruby to python and back again, obviously serialization is the way to go. I've looked at things like yaml but decided to write my own as I didn't want to deal with the dependencies of the libraries and such when it came time to distribute. I've wrote up how this serialization format works here.

my question is that as this format is intended to work cross language between ruby and python, how should I serialize ruby's symbols? I'm not aware of a object that works the same way in python. should a dump containing a symbol fail? should I just serialize it as a string? what would be best?

Ryex
  • 337
  • 3
  • 12
  • What is it that JSON and YAML can't do? They are accepted standards that are well documented and already implemented in many languages. – the Tin Man Dec 14 '10 at 09:38
  • it not that they wouldn't work, I just don't want to have to deal with all the dependencies when it comes time to distribute – Ryex Dec 15 '10 at 04:28

3 Answers3

1

Doesn't that depend on what your project needs? If symbols are important, you'll need some way to deal with them.

I'm not a Ruby programmer, but from what I've just read, I think converting them to strings is probably easiest. The standard Python interpreter will reuse memory for identical short strings, which seems to be a key reason suggested for using symbols.

EDIT: If it needs to work for other programmers, passing values back and forth shouldn't change them. So you either have to handle symbols properly, or throw an error straight away. It should be simple enough in Python:

class Symbol(str):
    pass

# In serialising code:
if isinstance(x, Symbol):
    serialise_as_symbol(x)
Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • 1
    I suppose your right, I don;t want to leave them out entirely and serializing them as stings might be an option but serializing them on the python side they would come back to ruby as string instead of symbols. I think one option is to build a symbol class on the python side that way python would know how to serialize them back to ruby. – Ryex Dec 13 '10 at 21:22
  • @Ryex: How important is it that they're symbols? Would it make life simpler just to do it with strings? (I guess we all try to make languages like our favourite--not being used to symbols, I don't see much need for them) – Thomas K Dec 13 '10 at 21:34
  • well in python string are immutable so they might as well be ruby symbols, the main reason to use symbols is that fact. I could forgo symbols but as I can't know how others will program their data structures and I would like this work for a data structure not written by me without restricting it's author. – Ryex Dec 13 '10 at 21:41
  • @Ryex: OK, I hadn't twigged that it was something for other coders to use. I've updated my answer. – Thomas K Dec 13 '10 at 22:07
  • One possible reason to use symbols in some cases is to avoid worrying about string mutability but that is not a good idea for the general case since you can run out of memory(since symbols are garbage collected). Symbols are mostly used for fast comparisons(since you are comparing numbers) and for identifying things (like deterministic hash indexes). – Roman A. Taycher Dec 29 '10 at 13:02
0

Any reason you're not using a standard data interchange format like JSON or XML? They seem to be acceptable to countless applications, services, and programmers.

Steve Wilhelm
  • 6,200
  • 2
  • 32
  • 36
0

If symbols are a stumbling block then you have three choices, don't allow them, convert them to strings on the fly or figure out a way to make them universal and/or innocuous in other languages.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303