2

I have a handler that uploads a KML file and returns JSON with the KML file as an attribute:

context.Response.Write("{\"name\":\"" + FileName + 
"\",\"type\":\"" + FileType + 
"\",\"size\":\"" + FileSize + 
"\",\"region_id\":\"" + regionID + 
"\",\"kml\":\"" + HttpUtility.HtmlEncode(xmlData) + "\"}");

As you can see, I'm trying to encode the KML with HttpUtility.HtmlEncode but I get an error in my response:

uncaught exception: Invalid JSON

How can I property encode the XML/KML file in C# so I can later decode it in JavaScript?

Edit #1: per Cheeso's comment I'm using ASP.NET, .NET Version 4 on IIS 7.5 Windows 7. My handler is a ashx file. The response works fine if I leave out the KML data (HttpUtility.HtmlEncode(xmlData)) from the response.

Edit #2 I also tried using System.Web.Script.Serialization.JavaScriptSerializer per the moderator's comment. I used it like such:

System.Web.Script.Serialization.JavaScriptSerializer serializer;
context.Response.Write("{\"name\":\"" + FileName + 
"\",\"type\":\"" + FileType + 
"\",\"size\":\"" + FileSize + 
"\",\"region_id\":\"" + regionID + 
"\",\"kml\":\"" + serializer.Serialize(xmlData) + "\"}");

I still get the same "Invalid JSON" error.

bluish
  • 26,356
  • 27
  • 122
  • 180
capdragon
  • 14,565
  • 24
  • 107
  • 153
  • 1
    Why not use a proper JSON encoding library? A list is here: http://www.json.org/ – Pekka May 26 '11 at 17:50
  • 8
    Why wouldn't you use JavaScriptSerializer or JSON.NET for this? – Marc Gravell May 26 '11 at 17:50
  • 3
    @Pekka oh C# doesn't ***at all*** - nor can it handle XML, regex, or even DateTime. Good job that the .NET BCL has, though :) – Marc Gravell May 26 '11 at 17:51
  • If i knew of such things i would use them. Why not propose a proper solution in form or an answer? – capdragon May 26 '11 at 17:52
  • @Marc yeah, it was more of a rhetorical question :) @cap good point. I'll let @Marc do the honours because I don't know the first thing about your platform – Pekka May 26 '11 at 17:52
  • ok capdragon, why don't you describe the rest of the server side app. Are you using ASPNET MVC? WCF? What is the thing that is generating JSON? – Cheeso May 26 '11 at 17:54
  • @Pekka I would, but I'm on mobile - Code samples are a pain without a compiler – Marc Gravell May 26 '11 at 17:58
  • (but JavaScriptSerializer is a core .NET class in system.web.extensions.dll IIRC) – Marc Gravell May 26 '11 at 18:00
  • @Mark: I wouldn't use JavaScriptSerializer if i don't know it exists, otherwise i wouldn't come to ask the question. Can't believe a moderator would post such a smart alek comment. – capdragon May 26 '11 at 18:15
  • how i encode/decode an string in C#? equivalent in js: decodeURIComponent/encodeURIComponent – The Mask May 26 '11 at 18:17
  • If you were one to upvote Marc's comment please elaborate in the form of an answer. I tried JavaScriptSerializer to no avail. – capdragon May 26 '11 at 18:32
  • @Marc: Because JavaScriptSerializer and JSON.NET serialize .NET objects and that's not what i need. Why don't you stop wasting people's time from your mobile phone, read the question carefully and don't bother commenting with a dumb question. – capdragon May 26 '11 at 18:48
  • 3
    @capdragon right; now that I'm back off mobile, I have added an example. Frankly, your tone above was both offensive and inappropriate. If you don't choose to use an object-level serializer that it up to you, but it is (as shown) a very valid answer to this problem. Especially considering that you haven't guarded the calling client by ensuring your own data is correctly encoded (which may or may not be a problem, depending on the candidate values of `FileName`, `FileType` etc). – Marc Gravell May 26 '11 at 19:57
  • @capdragon - for the record, the comment above was not intended as "smart alek" - it was intended to probe to question: is there a *specific reason* it doesn't use them. If you google for "+json +c#" I'm pretty sure that one of those two serializers will be in most of the links on the first page. – Marc Gravell May 26 '11 at 20:03
  • @Mark - I'm not looking for javascript serializer so stop trying to justify your answer/comment as being correct. I was looking for a javascript string encoder. And inappropriate and offensive is exactly what i'm saying your comment was, intended or not, that was the result. So just try to be careful next time and tones don't have to get out of hand. – capdragon May 26 '11 at 21:45
  • 3
    It's @Marc by the way; I don't mind either-way, but I won't get a notification alert from @Mark. It is unfortunate that you took offence - none was intended. – Marc Gravell May 26 '11 at 22:45

2 Answers2

10

You want to build JSON, right... and apparently it is ridiculous of me to suggest a JSON serializer.... yet:

string FileName = "foo.txt", FileType = "csv";
int FileSize = 1134, regionID = 12;
string xml = "<foo><bar/></foo>";

string json= new JavaScriptSerializer().Serialize(new {
    name = FileName,
    type = FileType,
    size = FileSize,
    region_id = regionID,
    kml = xml
});

In the majority of cases, using a pre-canned serializer is both more convenient and more robust against edge-cases of data.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Sorry, you misunderstood the question i don't want to build JSON... i have JSON, i want to encode xml within the json safely. – capdragon May 26 '11 at 21:41
  • 1
    @capdragon - no, I understood the question - I just proposed an alternative (simpler, IMO) way of implementing it that also addresses the encoding issue – Marc Gravell May 26 '11 at 21:45
  • @Mark - okay man, you're right, you're the big moderator MVP king of all kings and GOD of stackoverflow. One line of code is simpler than what you have up there. i don't need to encode a string i need your serialization solution because there can't be anything better. Are you happy? is that what you want to hear? – capdragon May 26 '11 at 21:53
  • 12
    @capdragon I don't understand your attitude; my intent is merely to present another suitable answer. I'm not telling you which option to take, and I'm genuinely delighted that the other answer gives you what you need. But I'm also thinking of the next reader, who might not take the same decision. I have used both forms of JSON construction - both are entirely valid in different scenarios. What is your complaint with my presenting the other option? I'm not forcing it on you. Seriously: lighten up. – Marc Gravell May 26 '11 at 22:14
4

An HTML Encoder encodes < as &lt; and so forth. That doesn't help you get XML into JSON format. What you want is a JavaScript Encode. Use HttpUtility.JavaScriptStringEncode

http://msdn.microsoft.com/en-us/library/dd991914.aspx

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • That's exactly what i needed and worked! thanks! All the other ridiculous comments from people were how to serialize .net objects into JSON +1 – capdragon May 26 '11 at 18:45