Problem with spaces in JSON identifier


#1

Spaces in JSON identifiers are valid JSON i.e.

{
    "my colour":"Green"
}

When parsing such JSON with the function in juce_JSON, I hit the breakpoint . . .

Identifier::Identifier (const String& nm) : name (Identifier::getPool().getPooledString (nm)) { /* An Identifier string must be suitable for use as a script variable or XML attribute, so it can only contain this limited set of characters.. */ jassert (isValidIdentifier (nm)); }

So I understand that JUCE Identifiers do not like whitespace characters, but this leaves me a little stuck if someone tries to load up a JSON string containing a whitespace in the identifier. How about considering adding an underscore to the identifier when one is discovered when parsing JSON, or alternatively returning a warning in the Result class when using syntax in the form . .

Thanks!


#2

That is a bit tricky, but since the same Identifier class is used for XML and other things that don’t allow a space, I’d rather not remove the assertion. You can of course just ignore it: the class will work just fine with spaces. Hmm. Not sure what’s best there, will have a think.


#3

Hmmm, I see that now. If I just continue from the break, I can see that the whitespace character is preserved when I convert the identifier to a string. This begs the question: why is the assertion in the iIdentifier class, and not the classes where a whitespace is illegal such as when writing XML? Thanks for the prompt response!


#4

I’ve changed it now. I’ve left the assertion in there only if you’re creating it from a const char* (i.e. if you’ve got a literal in your code), but not for a string, in which case it’s up to the caller to do any checking that’s needed.


#5

I am having the same problem with / and . in the json.

I am using JSON::fromString but it still hits the breakpoint.


#6

Yeah, fair point. I've removed those assertions now, as there's already some checking in XmlElement for illegal names, and that's where this kind of thing really matters.