![]() |
#1 |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
nsJSON plug-in
A JSON (JavaScript Object Notation) parser, manipulator and generator plug-in for NSIS. See the readme file for usage and more information.
http://nsis.sf.net/File:NsJSON.zip Stu |
![]() |
![]() |
![]() |
#2 |
Major Dude
|
Nice work!
Please write a short article into wiki (or copy from readme): http://nsis.sourceforge.net/NsJSON_plug-in Cool looking installers with custom design: www.graphical-installer.com Create Setup Pages easily: www.install-designer.com Build installers in Visual Studio 2005-2022: www.visual-installer.com or RAD Studio 2009 - 11 Alexandria: www.rad-installer.com |
![]() |
![]() |
![]() |
#3 |
Forum King
Join Date: Jan 2002
Location: AT-DE
Posts: 3,366
|
very nice, especially since none of the xml plugins worked to my satisfaction and i got the option to use json files for the same purpose. thanks stu!
|
![]() |
![]() |
![]() |
#4 |
Major Dude
Join Date: May 2003
Posts: 681
|
can i use it to parse json backup files from firefox (bookmark backup file/places)?
|
![]() |
![]() |
![]() |
#5 |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
If it is valid JSON then yes.
Stu |
![]() |
![]() |
![]() |
#6 | |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
New version:
Quote:
Stu |
|
![]() |
![]() |
![]() |
#7 | |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
New version:
Quote:
Stu |
|
![]() |
![]() |
![]() |
#8 |
Major Dude
|
Hey Stu, is it possible to parse floating numbers with comma instead of point?
Some currencies use it: "1.23" is "1,23" Cool looking installers with custom design: www.graphical-installer.com Create Setup Pages easily: www.install-designer.com Build installers in Visual Studio 2005-2022: www.visual-installer.com or RAD Studio 2009 - 11 Alexandria: www.rad-installer.com |
![]() |
![]() |
![]() |
#9 |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
You have to wrap the value in quotes so that it will be treated as a string rather than a float. It is not possible any other way as a comma is used as the delimiter between objects and values (whitespace is ignored).
Stu |
![]() |
![]() |
![]() |
#10 | |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
New version (with a fair number of changes):
Quote:
Stu |
|
![]() |
![]() |
![]() |
#11 |
Junior Member
Join Date: Oct 2012
Posts: 36
|
Awesome plugin Stu. Any chance of extending it to support multiple json instances open at once?
|
![]() |
![]() |
![]() |
#12 |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
That is already supported kind of. You just need to use Set /file with a node path to load a JSON file into. The only thing you cannot do is serialize a specific node to a file, which is where a change would be necessary. How much do you need multiple instance support (give an example of usage)?
Stu |
![]() |
![]() |
![]() |
#13 |
Junior Member
Join Date: Oct 2012
Posts: 36
|
I'm using in-memory json to store lots of my own user-specific data rather than tons of global variables.
Then I want to open up a program's settings file which is also in json, add a value and resave that settings.json. It's also useful to separate different blocks of data and only serialize the parts I need during debug. I guess the only thing that's really needed is the ability to serialize a specific node. Unless I can find a better solution for my in-memory storage... Actually, thinking about it I could probably use your arrays plugin and do arrays of pointers to arrays using a counter or concatenating names like CarPark0 = array of license plates (e.g. 0=ABC123, 1=DEF456, 2=GHI789) CarPark0_ABC123 = array of that particular car's properties (e.g. Make=ShinyThingsInc, Color=Green) So yeah, it probably isn't necessary to have the json plugin handle more than 1 root json object at a time. I'll just write my own serializer for the arrays. Cheers again for your awesome stuff. |
![]() |
![]() |
![]() |
#14 |
Senior Member
Join Date: Aug 2005
Posts: 121
|
I am getting truncated string values when they have spaces:
nsJSON::Set `more_stuff` `PSCHED_009_NAME` /value `QoS Packet Scheduler` Results in: "more_stuff": { "PSCHED_009_NAME": QoS } |
![]() |
![]() |
![]() |
#15 |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
Values are parsed as JSON. If you want quoted strings you must use double quotes around the value:
nsJSON::Set `more_stuff` `PSCHED_009_NAME` /value `"QoS Packet Scheduler"` Stu |
![]() |
![]() |
![]() |
#16 |
Junior Member
Join Date: Dec 2012
Posts: 6
|
nsJSON::Serialize crashes nsis installer with recent stable chrome default Preferences, which contain a very long (~66 KBytes) `variations_seed` string parameter.
Test case: Fresh install of google chrome (33.0.1750.154) which contain `variations_seed` parameter. code: I'm using ANSI NSIS v2.46 with nsJSON 1.0.1.0. Preferences sample: http://pastebin.com/raw.php?i=Xs4HLBMN deleting `variations_seed` before serializing workarounds this, but is ugly code: |
![]() |
![]() |
![]() |
#17 |
Junior Member
Join Date: Dec 2012
Posts: 6
|
PHP Code:
PHP Code:
PHP Code:
|
![]() |
![]() |
![]() |
#18 |
Forum King
Join Date: Jan 2002
Location: AT-DE
Posts: 3,366
|
It take it the plugin escapes the last quote, but wouldn't you have to escape the backslash as well?
|
![]() |
![]() |
![]() |
#19 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,504
|
"E:\\" should end up as "E:\", there is no escaped quote because that double backslash was already used to create a backslash
IntOp $PostCount $PostCount + 1 |
![]() |
![]() |
![]() |
#20 | |
Junior Member
Join Date: Dec 2012
Posts: 6
|
Quote:
2. Python parses it correctly: PHP Code:
|
|
![]() |
![]() |
![]() |
#21 | |
Forum King
Join Date: Jan 2002
Location: AT-DE
Posts: 3,366
|
Quote:
![]() |
|
![]() |
![]() |
![]() |
#22 | |
Junior Member
Join Date: Dec 2012
Posts: 6
|
Quote:
PHP Code:
|
|
![]() |
![]() |
![]() |
#23 |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
I will try to look at the issues this weekend.
Stu |
![]() |
![]() |
![]() |
#24 | ||
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
Quote:
Quote:
Stu |
||
![]() |
![]() |
![]() |
#25 |
Junior Member
Join Date: Jul 2014
Posts: 1
|
I can confirm that there is still a problem. The problem happens when the data is >64KB. It doesn't always crash, though, because it's corrupting the heap which might or might not result in a crash.
The source of the problem is that in the Serialize function, you allocate 65536 bytes for pszArg: code: ...and later you are using this buffer to store the serialized string: code: There is no length check taking place anywhere. |
![]() |
![]() |
![]() |
#26 |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
I will work on a fix to automatically increase the buffer size this weekend.
Stu |
![]() |
![]() |
![]() |
#27 | |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
New version:
Quote:
Stu |
|
![]() |
![]() |
![]() |
#28 | |
Junior Member
Join Date: Dec 2012
Posts: 6
|
1.0.1.2 crashes if JSON file starts with Byte order mark
It seems that `nsJSON::Set /file $JSON` works but the next `nsJSON::Set` crashes installer. I don't know if json can contain BOM, so it is just for your attention, but I guess, at least, it should not crash. Quote:
|
|
![]() |
![]() |
![]() |
#29 | |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
New version:
Quote:
My next task is to add HTTP GET/PUT/POST support for use with REST. Stu |
|
![]() |
![]() |
![]() |
#30 |
Junior Member
Join Date: Apr 2015
Posts: 1
|
nsJSON does not support comment
Hi,
I am trying to use nsJSON plug-in and just find out that comment '//' does disturb somehow the parser, and break the reading of any node inside the file... I'll temporally avoid using comment until I find a proper solution! Cheers, Pascal |
![]() |
![]() |
![]() |
#31 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,504
|
JSON does not really have comments AFAIK.
The safest option is to use _mycomment:"comment text" style entries. If you still want a real comment then /**/ block comments probably have a higher chance of working across parsers... IntOp $PostCount $PostCount + 1 |
![]() |
![]() |
![]() |
#32 |
Junior Member
Join Date: Oct 2012
Posts: 36
|
This plugin is awesome.
However, is there any chance of adding an escape function to convert an NSIS string into a JSON string? For example, right now if I call nsjson::Set some node path /value ":" Then the colon is not stored because it is invalid JSON, not a string. I have to call nsjson::Set some node path /value `":"` But if I don't know where the text has come from e.g. nsjson::Set some node path /value $R0 Then that's a problem. One of the following would be ideal: nsjson::escape $R0 Pop $R0 nsjson::Set some node path /value $R0 alternatively, nsjson::Set some node path /value /escape $R0 Such a function shouldn't try to determine the type of data - just treat the input as an unescaped, unquoted string. |
![]() |
![]() |
![]() |
#33 |
Major Dude
Join Date: May 2005
Location: New Zealand
Posts: 923
|
Try quotes or double quotes around the variable? Like: "$R0". If strings have spaces, they can resolve to multiple arguments when used with a plugin, which is why quotes are needed.
"Only a MouseHelmet will save you from a MouseTrap" -Jason Ross (Me) NSIS 3 POSIX Ninja Wiki Profile |
![]() |
![]() |
![]() |
#34 | |
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
Quote:
http://nsis.sourceforge.net/File:NsJSON.zip Stu |
|
![]() |
![]() |
![]() |
#35 |
Junior Member
Join Date: Oct 2012
Posts: 36
|
This is great, thanks Stu
|
![]() |
![]() |
![]() |
#36 |
Junior Member
Join Date: Oct 2012
Posts: 36
|
NB: When you use nsJSON::Get /type, if the nodepath doesn't exist then nothing gets pushed onto the stack - so always make sure you check that the nodepath /exists before you try to get the type.
Stu, you could change /type to return empty string if the node path doesn't exist, and do away with /exists altogether. |
![]() |
![]() |
![]() |
#37 |
Junior Member
Join Date: Oct 2012
Posts: 36
|
Hey, Stu
The new Quote function doesn't escape strings whose first and last characters are already quotemarks. For example, nsjson::Quote `"some"text"` |
![]() |
![]() |
![]() |
#38 | ||||
Moderator
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
|
Quote:
Quote:
Quote:
Quote:
Stu |
||||
![]() |
![]() |
![]() |
#39 |
Junior Member
Join Date: Oct 2012
Posts: 36
|
This is really great Stu, thanks.
Readme needs updating with latest switches behavior btw. I have another feature request if you feel inclined: { a: 'a', b: 1, c: {}} nsJSON::Get /keys ... Returns on the stack: 3 a b c |
![]() |
![]() |
![]() |
#40 |
Forum King
Join Date: Jan 2002
Location: AT-DE
Posts: 3,366
|
Erm, I would expect the plugin to deny this input, since it's invalid JSON
|
![]() |
![]() |
![]() |
|
Tags |
json, plug-in |
Thread Tools | Search this Thread |
Display Modes | |
|
|