3

I made a little code gen and was wondering if I should go to the trouble of handling the extra comma at the end. IE seems to ignore it, but I need to keep it cross browser, and I'd like to generate valid code anyway.

function init() {
var myOptions = { : 'Select home value', // <== THERE'S THE NON EXISTANT KEY
100000 : '$90,001 - $100,000',
1000000 : '$950,001 - $1,000,000',
1000001 : 'Over $1,000,000', // <== HERE'S THE COMMA I'M CURIOUS ABOUT
};

here's the code to generate

protected string DoTransform()
{
    var sb = new StringBuilder("var myOptions = {");
    foreach (var option in 
        XDocument.Load(MapPath("~/App_Data/Data.xml"))
            .XPathSelectElements("./data/options[@question='ApproximatePropertyValue']/option"))
    {
        sb.AppendFormat("{0} : '{1}',\n", option.Attribute("value").Value, option.Value);
    }
    sb.AppendLine("};");
    return sb.ToString();
}

ANSWER: Here's the updated code that takes care of the empty key (by skipping the first element) and trailing comma (by rearranging logic so TrimEnd can nab it).

protected string DoTransform()
{
    var sb = new StringBuilder();
    foreach (var option in 
        XDocument.Load(MapPath("~/App_Data/Data.xml"))
            .XPathSelectElements("./data/options[@question='ApproximatePropertyValue']/option")
            .Skip(1))
    {
        sb.AppendFormat("{0}:'{1}',", option.Attribute("value").Value, option.Value);
    }
    return"var myOptions = {\n" + sb.ToString().TrimEnd(',') + "};";
}
3
  • Is myOptions being programmatically generated? Commented Jul 14, 2011 at 1:41
  • Based on @cwallenpoole's post, I think it would be worth writing your code so that it omits the last comma. Commented Jul 14, 2011 at 1:48
  • The trailing comma is allowed by ECMA-262 §11.1.5, but some versions of IE (versions < 9 at least) will throw an error if an object initialiser (aka object literal) has one. Commented Jul 14, 2011 at 2:10

4 Answers 4

2

My understanding is that most browsers will allow the trailing comma, but it is not something which is acceptable in the JSON spec, so it is a bad idea. On the other hand, the fact that you are missing a key in that first key-value pair won't be forgiven... by ANYONE ;-)

Edit

Just saw your code above. Forgive me, my .NET is rusty (as in, I've barely looked at it, ever), but I believe this will work:

foreach (var option in 
    XDocument.Load(MapPath("~/App_Data/Data.xml"))
       .XPathSelectElements(
             "./data/options[@question='ApproximatePropertyValue']/option"
       )
    )
{
    // use the length as a flag, if you've added to it, it will be longer than 16
    if(sb.Length > 20)sb.Append(",");
    sb.AppendFormat("\n{0} : '{1}'", 
        option.Attribute("value").Value, option.Value);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Opera 12 says:

[14.07.2011 03:44:53] JavaScript - ...
Inline script compilation
Syntax error at line 6 while loading:
  var myOptions = {: 'Select home value
-------------------^
expected '}', got ':'

You should better look for a library here: http://json.org/ instead of implementing the wheel again.

(Please don't tell anyone that I gave you that awful hacky advice:) You could append a sentinel like "_ignore_me": false to the sb.

Comments

0

At least IE7 and older versions will have problems with the trailing comma. In newer versions it depends on the document-mode.

Comments

-1

IE doesn't like the trailing commas. Other browsers are Ok.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.