Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why wouldn't you just base64encode your json and include that in the URI and the base64decode it when you want to de-json it ?


I've tried many things but I wanted it to be readable, small, and good looking.

JSON:

    {"user":{"name":"Bob Smith","age":47,"sex":"M","dob":"5-12-1956"},"pastimes":["golf","opera","poker","rap"],"children":{"bobby":{"age":12,"sex":"M"},"sally":{"age":8,"sex":"F"}}}
Base64:

    eyJ1c2VyIjp7Im5hbWUiOiJCb2IgU21pdGgiLCJhZ2UiOjQ3LCJzZXgiOiJNIiwiZG9iIjoiNS0xMi0xOTU2In0sInBhc3RpbWVzIjoNClsiZ29sZiIsIm9wZXJhIiwicG9rZXIiLCJyYXAiXSwiY2hpbGRyZW4iOnsiYm9iYnkiOnsiYWdlIjoxMiwic2V4IjoiTSJ9LA0KInNhbGx5Ijp7ImFnZSI6OCwic2V4IjoiRiJ9fX0=
JSON + URIEncode

    %7B%22user%22:%7B%22name%22:%22Bob%20Smith%22,%22age%22:47,%22sex%22:%22M%22,%22dob%22:%225-12-1956%22%7D,%22pastimes%22:%5B%22golf%22,%22opera%22,%22poker%22,%22rap%22%5D,%22children%22:%7B%22bobby%22:%7B%22age%22:12,%22sex%22:%22M%22%7D,%22sally%22:%7B%22age%22:8,%22sex%22:%22F%22%7D%7D%7D
URLON

    _user_name=Bob%20Smith&age:47&sex=M&dob=5-121956;&pastimes@=golf@=opera@=poker@=rap;&children_bobby_age:12&sex=M;&sally_age:8&sex=F


You can translate the base64 version into a readable form from the terminal using widely available libraries. For example, using a Python interpreter you can do this:

  >>> from base64 import b64decode
  >>> from json import loads, dumps
  >>> print dumps(loads(b64decode('eyJ1c2VyIjp7Im5hbWUiOiJCb2IgU21pdGgiLCJhZ2UiOjQ3LCJzZXgiOiJNIiwiZG9iIjoiNS0xMi0xOTU2In0sInBhc3RpbWVzIjoNClsiZ29sZiIsIm9wZXJhIiwicG9rZXIiLCJyYXAiXSwiY2hpbGRyZW4iOnsiYm9iYnkiOnsiYWdlIjoxMiwic2V4IjoiTSJ9LA0KInNhbGx5Ijp7ImFnZSI6OCwic2V4IjoiRiJ9fX0=')), indent=2)
  {
    "pastimes": [
      "golf", 
      "opera", 
      "poker", 
      "rap"
    ], 
    ## [etc.]
  }


The goal is to use it in a URL. I don't want my urls to look like that:

  http://db.mmo-champion.com/items/#eyJ1c2VyIjp7Im5hbWUiO
  iJCb2IgU21pdGgiLCJhZ2UiOjQ3LCJzZXgiOiJNIiwiZG9iIjoiNS0x
  Mi0xOTU2In0sInBhc3RpbWVzIjoNClsiZ29sZiIsIm9wZXJhIiwicG9
  rZXIiLCJyYXAiXSwiY2hpbGRyZW4iOnsiYm9iYnkiOnsiYWdlIjoxMi
  wic2V4IjoiTSJ9LA0KInNhbGx5Ijp7ImFnZSI6OCwic2V4IjoiRiJ9f
  X0


If you want to pack that much information into a URL, it’s going to look ugly no matter how it’s formatted.

(I assume you have some reason for not just passing a session key in the URL and keeping all the relevant state on the server.)


Fair enough, though json | base64 is the easiest for others to implement

json | gzip | base64 saves another 33 bytes :)

and you already know urlon is the shortest


That would be the most sane solution i think. Is there an advantage to having a readable URI? Browsers are in the process of deprecating the address bar already.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: