Macro parsing refuses valid json strings

Confirmed bugs should get a single post here. Check the READ ME FIRST sticky thread for the format.

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice, MapTool BugReport Manager

Forum rules
Posts that do not conform to the READ ME FIRST sticky thread are subject to deletion.
Post Reply
bobifle
Giant
Posts: 219
Joined: Thu Oct 19, 2017 12:36 pm

Macro parsing refuses valid json strings

Post by bobifle »

While I feed json to my macro I pinned down an issue related to this:

Code: Select all

[r: foo = json.set("", "bar", "A json string with 'quotes' and  \"double quotes\".")]
is there a way to make it work ? Escaping " with \" is supposed to be valid in json.

User avatar
JamzTheMan
Great Wyrm
Posts: 1872
Joined: Mon May 10, 2010 12:59 pm
Location: Chicagoland
Contact:

Re: Macro parsing refuses valid json strings

Post by JamzTheMan »

Try escaping the \ so \\\

I know I have to do that with regular expressions. Remember the macro engine has to parse the text and then pass that on to a java method usually which needs to parse it so we end up in \ hell sometimes :D
-Jamz
____________________
Custom MapTool 1.4.x.x Fork: maptool.nerps.net
Custom TokenTool 2.0 Fork: tokentool.nerps.net
More information here: MapTool Nerps! Fork

bobifle
Giant
Posts: 219
Joined: Thu Oct 19, 2017 12:36 pm

Re: Macro parsing refuses valid json strings

Post by bobifle »

not working neither :?

The solution looks rather simple, MT needs to parse string between non escaped double quotes, otherwise you cannot support json, or more importantly, double quotes in strings: Simpler stuff don't work neither,

Code: Select all

[r: foo = "foo\\"bar"]
[r: foo = "foo\"bar"]

User avatar
JamzTheMan
Great Wyrm
Posts: 1872
Joined: Mon May 10, 2010 12:59 pm
Location: Chicagoland
Contact:

Re: Macro parsing refuses valid json strings

Post by JamzTheMan »

You will have to break it up then like:

Code: Select all

[r: foo = json.set("", "bar", "A json string with 'quotes' and " + '"double quotes."')]
<br>
[r: json.get(foo, "bar")]
returns:

Code: Select all

{"bar":"A json string with 'quotes' and \"double quotes.\""} 
A json string with 'quotes' and "double quotes."
-Jamz
____________________
Custom MapTool 1.4.x.x Fork: maptool.nerps.net
Custom TokenTool 2.0 Fork: tokentool.nerps.net
More information here: MapTool Nerps! Fork

bobifle
Giant
Posts: 219
Joined: Thu Oct 19, 2017 12:36 pm

Re: Macro parsing refuses valid json strings

Post by bobifle »

nope .... nope .... no way :D
I appreciate the effort to make this work but don't you agree something is fishy from the start ?
When " has been chosen for MT strings, the first thing to do is to give a way to escape " in strings.

By the way to fix my problem I've decided to preprocess de text I'm feeding, replacing " by '.
Otherwise using your method I would need to replace every occurence of " by "+'"+"

Code: Select all

"fooo"bar"def" -> "fooo"+'"'+"bar"+'"'+"def"
Should I open a bug ? or do MT old timers thing this is just working as intended ? It's not blocking in any way, it just makes the mt code looks bad.

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Macro parsing refuses valid json strings

Post by aliasmask »

It's not a bug. You have to escape your escapes. There are many ways to do quote injection. I prefer using the html entity &quot; if it's going to be a printed quote.

User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: Macro parsing refuses valid json strings

Post by Full Bleed »

bobifle wrote:
Wed Jul 04, 2018 6:44 pm
or do MT old timers thing this is just working as intended ? It's not blocking in any way, it just makes the mt code looks bad.
I just avoid json's like the plague because of stuff like this. ;)
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

bobifle
Giant
Posts: 219
Joined: Thu Oct 19, 2017 12:36 pm

Re: Macro parsing refuses valid json strings

Post by bobifle »

Ok if I'm the only thinking that's an issue, I'll drop the case. Thanks for the suggestions to work ar... to handle this properly :mrgreen:

@full bleed: you're worrying me, I decided to go full json, for macro parameters, return, and data in general.

User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: Macro parsing refuses valid json strings

Post by Full Bleed »

bobifle wrote:
Thu Jul 05, 2018 5:02 am
@full bleed: you're worrying me, I decided to go full json, for macro parameters, return, and data in general.
Well, some people have few issues... or (more likely) just get used to working around the peculiarities. Particularly those used to using jsons elsewhere and who have a firm grasp of how MT parses things. But for the hackier macro writers (like myself) I just find I burn way too much time trying to figure out how to get around wonky issues like the one you encountered here and would just as soon use other methods so I can move onto other things. I find string lists/props to be more readable anyway, so unless I have to use a json, I don't.

Your familiarity with jsons and the more flexible/powerful functions they have might make using them a considerably better choice for you over the long run (depending on what you're doing.)
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

User avatar
Jagged
Great Wyrm
Posts: 1306
Joined: Mon Sep 15, 2008 9:27 am
Location: Bristol, UK

Re: Macro parsing refuses valid json strings

Post by Jagged »

bobifle wrote:
Thu Jul 05, 2018 5:02 am
Ok if I'm the only thinking that's an issue, I'll drop the case. Thanks for the suggestions to work ar... to handle this properly :mrgreen:

@full bleed: you're worrying me, I decided to go full json, for macro parameters, return, and data in general.
I think it just depends upon what you are comfortable with. The issue isn't JSON its how Maptool handles extended characters and you can encounter the issue whatever way you store data.
I use JSON because I use it at work and some am happy with the format.

bobifle
Giant
Posts: 219
Joined: Thu Oct 19, 2017 12:36 pm

Re: Macro parsing refuses valid json strings

Post by bobifle »

I agree, JSON is great, it's one, if not the most human readable data format. Too bad that MT macros requires a lot of boilerplate code to handle it. I'm still grateful that MT supports json.

Post Reply

Return to “Bug Reports”