I needed to serialize Request.ServerVariables to JSON.  I tried both  JavaScriptSerializer nor DataContractJsonSerializer without any luck.  But I thought both would work, as Request.ServerVariables is of type NameValueCollection and the docs on MSDN state that both serialization engines support dictionaries. The solution? Convert (manually) the NameValueCollection into a dictionary.

Here's the code:

            Dictionary<string,string> d = new Dictionary<string,string>();
            foreach (string s in Request.ServerVariables.AllKeys)
            {
                d.Add(s, Request.ServerVariables[s]);
            }
            //DataContractJsonSerializer
            DataContractJsonSerializer serverVariablesSerializer = new DataContractJsonSerializer(typeof(Dictionary<string,string>));
            MemoryStream ms = new MemoryStream();
            serverVariablesSerializer.WriteObject(ms, d);
            string json = Encoding.Default.GetString(ms.ToArray());
            Debug.WriteLine(json);
            //JavaScriptSerializer
            JavaScriptSerializer serverVariablesSerializer2 = new JavaScriptSerializer();
            string json2 = serverVariablesSerializer2.Serialize(d);
            Debug.WriteLine(json2);

What's interesting is that the output is different between the two serialization engines. JavaScriptSerializer is smart enough to turn the key into the key in JSON, whereas DataContractJsonSerializer creates a key called "key" with the value of the key and then a key called "value" with the value of the value. Kind of annoying. The winner? JavaScriptSerializer

Here's the output BTW:

DataContractJsonSerializer: [{"Key":"ALL_HTTP","Value":"HTTP_CONNECTION:Keep-Alive\u000d\u000aHTTP_ACCEPT:*\/*\u000d\u000aHTTP_ACCEPT_ENCODING:gzip, deflate\u000d\u000aHTTP_ACCEPT_LANGUAGE:en-us\u000d\u000aHTTP_COOKIE:comment=name=b&email=b@b.com&url=&country=us; WT_FPC=id=71.231.143.146-177222816.30061129:lv=1277910617997:ss=1277910147666\u000d\u000aHTTP_HOST:localhost:4135\u000d\u000aHTTP_USER_AGENT:Mozilla\/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident\/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; OfficeLiveConnector.1.4; OfficeLivePatch.1.3; MS-RTC LM 8; .NET4.0C; .NET4.0E)\u000d\u000a"},{"Key":"ALL_RAW","Value":"Connection: Keep-Alive\u000d\u000aAccept: *\/*\u000d\u000aAccept-Encoding: gzip, deflate\u000d\u000aAccept-Language: en-us\u000d\u000aCookie: comment=name=b&email=b@b.com&url=&country=us; WT_FPC=id=71.231.143.146-177222816.30061129:lv=1277910617997:ss=1277910147666\u000d\u000aHost: localhost:4135\u000d\u000aUser-Agent: Mozilla\/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident\/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; OfficeLiveConnector.1.4; OfficeLivePatch.1.3; MS-RTC LM 8; .NET4.0C; .NET4.0E)\u000d\u000a"},{"Key":"APPL_MD_PATH","Value":""},{"Key":"APPL_PHYSICAL_PATH","Value":"C:\\stuff\\WebApplication1\\WebApplication1\\"},{"Key":"AUTH_TYPE","Value":"NTLM"},{"Key":"AUTH_USER","Value":"REDMOND\\karstenj"},{"Key":"AUTH_PASSWORD","Value":""},{"Key":"LOGON_USER","Value":"REDMOND\\karstenj"},{"Key":"REMOTE_USER","Value":"REDMOND\\karstenj"},{"Key":"CERT_COOKIE","Value":""},{"Key":"CERT_FLAGS","Value":""},{"Key":"CERT_ISSUER","Value":""},{"Key":"CERT_KEYSIZE","Value":""},{"Key":"CERT_SECRETKEYSIZE","Value":""},{"Key":"CERT_SERIALNUMBER","Value":""},{"Key":"CERT_SERVER_ISSUER","Value":""},{"Key":"CERT_SERVER_SUBJECT","Value":""},{"Key":"CERT_SUBJECT","Value":""},{"Key":"CONTENT_LENGTH","Value":"0"},{"Key":"CONTENT_TYPE","Value":""},{"Key":"GATEWAY_INTERFACE","Value":""},{"Key":"HTTPS","Value":""},{"Key":"HTTPS_KEYSIZE","Value":""},{"Key":"HTTPS_SECRETKEYSIZE","Value":""},{"Key":"HTTPS_SERVER_ISSUER","Value":""},{"Key":"HTTPS_SERVER_SUBJECT","Value":""},{"Key":"INSTANCE_ID","Value":""},{"Key":"INSTANCE_META_PATH","Value":""},{"Key":"LOCAL_ADDR","Value":"127.0.0.1"},{"Key":"PATH_INFO","Value":"\/Default.aspx"},{"Key":"PATH_TRANSLATED","Value":"C:\\stuff\\WebApplication1\\WebApplication1\\Default.aspx"},{"Key":"QUERY_STRING","Value":""},{"Key":"REMOTE_ADDR","Value":"127.0.0.1"},{"Key":"REMOTE_HOST","Value":"127.0.0.1"},{"Key":"REMOTE_PORT","Value":""},{"Key":"REQUEST_METHOD","Value":"GET"},{"Key":"SCRIPT_NAME","Value":"\/Default.aspx"},{"Key":"SERVER_NAME","Value":"localhost"},{"Key":"SERVER_PORT","Value":"4135"},{"Key":"SERVER_PORT_SECURE","Value":"0"},{"Key":"SERVER_PROTOCOL","Value":"HTTP\/1.1"},{"Key":"SERVER_SOFTWARE","Value":""},{"Key":"URL","Value":"\/Default.aspx"},{"Key":"HTTP_CONNECTION","Value":"Keep-Alive"},{"Key":"HTTP_ACCEPT","Value":"*\/*"},{"Key":"HTTP_ACCEPT_ENCODING","Value":"gzip, deflate"},{"Key":"HTTP_ACCEPT_LANGUAGE","Value":"en-us"},{"Key":"HTTP_COOKIE","Value":"comment=name=b&email=b@b.com&url=&country=us; WT_FPC=id=71.231.143.146-177222816.30061129:lv=1277910617997:ss=1277910147666"},{"Key":"HTTP_HOST","Value":"localhost:4135"},{"Key":"HTTP_USER_AGENT","Value":"Mozilla\/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident\/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; OfficeLiveConnector.1.4; OfficeLivePatch.1.3; MS-RTC LM 8; .NET4.0C; .NET4.0E)"}]

JavaScriptSerializer: {"ALL_HTTP":"HTTP_CONNECTION:Keep-Alive\r\nHTTP_ACCEPT:*/*\r\nHTTP_ACCEPT_ENCODING:gzip, deflate\r\nHTTP_ACCEPT_LANGUAGE:en-us\r\nHTTP_COOKIE:comment=name=b&email=b@b.com&url=&country=us; WT_FPC=id=71.231.143.146-177222816.30061129:lv=1277910617997:ss=1277910147666\r\nHTTP_HOST:localhost:4135\r\nHTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; OfficeLiveConnector.1.4; OfficeLivePatch.1.3; MS-RTC LM 8; .NET4.0C; .NET4.0E)\r\n","ALL_RAW":"Connection: Keep-Alive\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-us\r\nCookie: comment=name=b&email=b@b.com&url=&country=us; WT_FPC=id=71.231.143.146-177222816.30061129:lv=1277910617997:ss=1277910147666\r\nHost: localhost:4135\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; OfficeLiveConnector.1.4; OfficeLivePatch.1.3; MS-RTC LM 8; .NET4.0C; .NET4.0E)\r\n","APPL_MD_PATH":"","APPL_PHYSICAL_PATH":"C:\\stuff\\WebApplication1\\WebApplication1\\","AUTH_TYPE":"NTLM","AUTH_USER":"REDMOND\\karstenj","AUTH_PASSWORD":"","LOGON_USER":"REDMOND\\karstenj","REMOTE_USER":"REDMOND\\karstenj","CERT_COOKIE":"","CERT_FLAGS":"","CERT_ISSUER":"","CERT_KEYSIZE":"","CERT_SECRETKEYSIZE":"","CERT_SERIALNUMBER":"","CERT_SERVER_ISSUER":"","CERT_SERVER_SUBJECT":"","CERT_SUBJECT":"","CONTENT_LENGTH":"0","CONTENT_TYPE":"","GATEWAY_INTERFACE":"","HTTPS":"","HTTPS_KEYSIZE":"","HTTPS_SECRETKEYSIZE":"","HTTPS_SERVER_ISSUER":"","HTTPS_SERVER_SUBJECT":"","INSTANCE_ID":"","INSTANCE_META_PATH":"","LOCAL_ADDR":"127.0.0.1","PATH_INFO":"/Default.aspx","PATH_TRANSLATED":"C:\\stuff\\WebApplication1\\WebApplication1\\Default.aspx","QUERY_STRING":"","REMOTE_ADDR":"127.0.0.1","REMOTE_HOST":"127.0.0.1","REMOTE_PORT":"","REQUEST_METHOD":"GET","SCRIPT_NAME":"/Default.aspx","SERVER_NAME":"localhost","SERVER_PORT":"4135","SERVER_PORT_SECURE":"0","SERVER_PROTOCOL":"HTTP/1.1","SERVER_SOFTWARE":"","URL":"/Default.aspx","HTTP_CONNECTION":"Keep-Alive","HTTP_ACCEPT":"*/*","HTTP_ACCEPT_ENCODING":"gzip, deflate","HTTP_ACCEPT_LANGUAGE":"en-us","HTTP_COOKIE":"comment=name=b&email=b@b.com&url=&country=us; WT_FPC=id=71.231.143.146-177222816.30061129:lv=1277910617997:ss=1277910147666","HTTP_HOST":"localhost:4135","HTTP_USER_AGENT":"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; OfficeLiveConnector.1.4; OfficeLivePatch.1.3; MS-RTC LM 8; .NET4.0C; .NET4.0E)"}