Why can't I compare character JSON \u0022 with <U+0022> ?

0 votes
asked Nov 8, 2021 in Question / help by Martin (8,360 points)
edited Nov 8, 2021 by Martin

I got very confused because a JSON generated double-quote (") character doesn't match <U+0022>.  Even if you use \u0022 to generate it.

I tripped over this when trying to manually parse a JSON string, and finding that I couldn't search for a quote character that I had a generated using <U+0022> instead I had to let JSON generate the quote character using \" or \u0022.

(click diagram for source)

JSON and PREPROCESSOR quote characters display identically but can't be compared.

(PS it would be useful to have the \" escape string outside of JSON rather than having to use <U+0022>)

2 Answers

0 votes
answered Nov 9, 2021 by plantuml (294,960 points)
selected Nov 9, 2021 by Martin
 
Best answer

To add more confusion :-) you can now have:

@startuml
!$quote= "<U+0022>"
!$json = { "quote1" : "\"", "quote2" : "\u0022" } 

:The four different quotes: $quote $json.quote1 $json.quote2 %chr(34);
@enduml
commented Nov 9, 2021 by Martin (8,360 points)
Fantastic, thank you!   (I wonder what other uses/misuses %chr() can be put to...!)
0 votes
answered Nov 8, 2021 by Martin (8,360 points)
edited Nov 8, 2021 by Martin

The reason becomes obvious once you look at the length of the strings.

The JSON strings actually are a single double quote character.  But the <U+0022> string is the 8 character string "<U+0022>.  If you ever try to display it then it it automatically resolved to single " character.  But inside the builtin functions like %strlen, %strpos, %substr, etc it will be the 8 character string.

Which leads me to conclude that the JSON method is the only way of getting a string variable containing just a double-quote as 1 character.  As there seems to be no way of escaping a quote into a non-JSON variable.

commented Nov 8, 2021 by plantuml (294,960 points)

This is probably a design issue.

Now I'm also confused about what we should do/change.

Would it help if you had a %chr() built-in function so that you could use %chr(34) to get a real double quote ?

commented Nov 8, 2021 by Martin (8,360 points)
Good idea - I think that if %chr() existed then I would have tried that next after trying "\"" and """".  

But such a new feature is low priority because the need-case was very niche, and there is a work-around. (The JSON trick works well, but I was a bit lucky to stumble across it!)
...