views:

70

answers:

3

I'd like to use a data interchange format that uses no quotation marks. Maybe something based on JSON:

{param:value,param:value,param:{[{param:value,param:value}, {param:value,param:value}]}}

How should I go about parsing something like that in let's say PHP. Should do it through regular expressions?

+2  A: 

What's so important about having your format not use quotation marks? Chances are there's something else you could change to make that requirement go away (which would be better).

As far as using regular expressions to parse JSON-like formats, no. This is a BAD idea; regular expressions were never really intended to parse recursive structures like JSON. At the very least you'll run into issues with performance of the many regex matches you'll need to use to attempt to deal with recursion; at worst you'll run into snafus of attempting to even sort out how to match and parse recursion in the first place.

Your format as proposed has its own issues, as well: how do you differentiate between a ,, {, or : in the value of a key, and the actual ,, {, or : that's part of the format? How do you deal with spaces in key or value names?

Amber
+1  A: 

You could use XML and not use any attributes. You'd also need to avoid the header, which has some quotation marks.

SLaks
+1  A: 

Why not use JSON and leverage off the available libraries and tools available to you ? This blog entry details a JSON parser in PHP.

To handle JSON data there is JSON extension in PHP which is available after PHP 5.2.0. Two functions : json_encode() and json_decode() are very useful converting and parsing JSON data through PHP.

Creating another format seems repetitive and error prone when there are so many well-defined and well-tested options available (JSON, XML, Google Protocol Buffers, YAML).

Brian Agnew
JSON uses quotation marks for strings.
SLaks
I know, and I still make the argument that it's a poor design decision. Perhaps there's a *good* reason to avoid these, but it's not been expressed.
Brian Agnew
You're quite right, but it doesn't answer the question as asked.
SLaks
That's a favorite trick of mine on StackOverflow, I confess. Sometimes the question being asked isn't the real question
Brian Agnew
Similarly, "Don't use regular expressions to parse HTML" generally doesn't answer the question as asked. It's still the right answer, though.
Anon.
@Anon - yes. That's a very good example of what I mean.
Brian Agnew
I'm not saying that you shouldn't have done it; I've given similar answers myself. I'm just pointing it out.
SLaks
Yes, you are right. I should figure out a way to use a standard format. Thanks for the prompt and thoughtful responses.
Emanuil