I want custom separators for JSON in Go, equivalent to Python's:
json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
How to specify separator for JSON in Go ?
Clarification:
json.dumps({"key1":"value1","key2":"value2"}, separators=('__','..'))
'{"key2".."value2"__"key1".."value1"}'
There are some valid use cases for this requirement:
http://mens.com?&cyclone_data={"VisitorId":"53905341bd05ae26a9000001","CampaignId":"538f278cbd05ae36c6000001","LandingPageId":"538eac3ebd05ae15fe000001","OfferId":"538f097ebd05ae2a4d000001","SourceId":"538f0e39bd05ae2b9c000002","NetworkId":""}
I need to set this same url in cookie:
tracking_data=http://mens.com?&cyclone_data={"VisitorId":"53905341bd05ae26a9000001","CampaignId":"538f278cbd05ae36c6000001","LandingPageId":"538eac3ebd05ae15fe000001","OfferId":"538f097ebd05ae2a4d000001","SourceId":"538f0e39bd05ae2b9c000002","NetworkId":""}
After setting cookie with http.SetCookie()
, Chrome Web Developer tool shows:
Date:Thu, 05 Jun 2014 11:23:46 GMT
Location:http://mens.com?&cyclone_data={"VisitorId":"53905341bd05ae26a9000001","CampaignId":"538f278cbd05ae36c6000001","LandingPageId":"538eac3ebd05ae15fe000001","OfferId":"538f097ebd05ae2a4d000001","SourceId":"538f0e39bd05ae2b9c000002","NetworkId":""}
Set-Cookie:538f278cbd05ae36c6000001=538f278cbd05ae36c6000001; Path=/
Set-Cookie:cyclone-track-url=http://mens.com?&cyclone_data={VisitorId:53905341bd05ae26a9000001CampaignId:538f278cbd05ae36c6000001LandingPageId:538eac3ebd05ae15fe000001OfferId:538f097ebd05ae2a4d000001SourceId:538f0e39bd05ae2b9c000002NetworkId:} Content-Length:378
Content-Type:text/html; charset=utf-8
,
and ""
are missing.
I am aware of base64
. I want users to be able to change VisitorId
without any manual encode/decode routine.
Server side app reads and compares this url from Cookie and Browser Referer.
Found this issue: https://code.google.com/p/go/issues/detail?id=7243
Well, you can't directly, but if what you want is as I'm guessing to remove extra unneeded spaces, you can use json.Compact
:
http://golang.org/pkg/encoding/json/#Compact
It just takes your encoded json and removes unnecessary spaces.
You can also try playing with the json.MarshalIndent
method, that lets you control indentation separators. But AFAIK, you can't specify non standard separators.