Thanks Tim! That's a cool way of looking at it. I don't actually need to pass the uuid to a browser via JSON, I just need to store the uuid as a member field of an object in my db and be able to provide a fixture with it.
What I've ended up doing is importing the uuid library (which can be found here - http://zesty.ca/python/) into my Python 2.4 install. It generated a well formed string (ie. '3F2504E0-4F89-11D3-9A0C-0305E82C3301') from the original byte sequence and I can in turn store that in the DB as a char field with an index. I can then do a lookup for the uuid string representation by converting it from the byte sequence on the fly. Its far from optimal, but I suspect that this won't be the bottleneck in my app. :-) -- --Leo Tim Chase wrote: >>> I've been able to dig into this a little bit more and it >>> looks like django.utils.simplejson.decoder doesn't support >>> the '\x' escape character. Is this an intentional omission, >>> a bug in Django, or just me misunderstanding Python? >> JSON does not support the '\x' escape; see json.org for a full >> specification of what it allows. > > Since a GUID isn't really a string, but a sequence of bytes > (which will be made more distinct in python3k, as it is in Java), > it should likely be encoded into JSON as an array of numbers. > Something like this might do the trick: > > uuid = '3F2504E0-4F89-11D3-9A0C-0305E82C3301' > uuid = uuid.replace('-', '') # strip out the dashes > uuid_as_int_list = [ > int('%s%s'%pair, 16) > for pair > in zip(uuid[::2], uuid[1::2]) > ] > > and then use uuid_as_int_list to pass using JSON (which can then > be codified uneventfully). > > The un-dash-ified version can be reconstituted with > > ''.join(hex(x)[2:].zfill(2) for x in uuid_as_int_list) > > The "hex(x)[2:].zfill(2)" is a lazy way to use inbuilt python > functions to convert the decimal values back to two-character > hex-pairs. > > If the string actually contains the bytes (which it sounds like > it does in your case), you can simply use > > uuid = '?%\x04\xe0O\x89\x11\xd3\x9a\x0c\x03\x05\xe8,3\x01' > uuid_as_int_list = [ord(x) for x in uuid] > > to and reconstitute it with > > uuid = ''.join(chr(x) for x in uuid_as_int_list) > > Or, if you prefer to pass the thing as a string instead of an > array of numbers, you can use > > uuid = '?%\x04\xe0O\x89\x11\xd3\x9a\x0c\x03\x05\xe8,3\x01' > display_uuid = ''.join(hex(ord(x))[2:].zfill(2) for x in uuid) > > to encode the hex-bytes as presentable characters. > > If dashes then matter in the presentation form, I leave that as > an exercise to the reader. ;-) > > -tim > [above example GUID pilfered from Wikipedia] > > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---