This project has moved. For the latest updates, please go here.

ToString with unquoted object names

Jun 24, 2009 at 11:10 PM

I'm working with a jQuery plugin that expects object literals as pair names rather than quoted strings as pair names:


{ property1: "value1", property2: "value2" }



{ "property1": "value1", "property2": "value2" }


The easiest way I found to handle this is an extension on JObject.ToString():


public static string ToString(this JObject self, bool quote)
    using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
        JsonTextWriter jw = 
            new JsonTextWriter(sw)
                    QuoteName = quote, 
                    Formatting = Formatting.Indented
        return sw.ToString();

Is there a better way to handle this?
Many thanks! 





Jun 25, 2009 at 8:38 AM

No, that is the pretty good.

I haven't tested or touched anything with name quoting turned off in years. Glad it works with LINQ to JSON :)

Jun 25, 2009 at 3:17 PM

All tests passing thus far :)

Thanks for a fantastic library!