Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using JSON for import/export with the JavaScript Regular Expression Tester

0.00/5 (No votes)
19 Oct 2004 2  
How to load data to and save data in a JavaScript object using JSON.

Sample Image - UsingJSON.gif

Introduction

Back in JavaScript Regular Expression Tester, I said that I could save all of the editable attributes in the interface externally using JSON. Now I will explain how.

Additional requirements

You will need opensave.dll from the download code found in this old article. The file opensave.dll provides the open/save dialogue box for opening and saving files from within this application, and the instructions for installing/registering the DLL file are within the text of that article. You will also find that I reference the following files from that archive: file_open.GIF, file_open_pressed.GIF, file_save.GIF, and file_save_pressed.GIF.

Points of Interest

I have implemented this in an HTA, or HTML Application. This is a browser window that looks like a normal Windows application and has full access to locally scriptable components (in other words, without browser security). In addition to the files from the original article, two new files are required to make the regular expressions that are being worked on to be saved. The first file, regex2.hta, calls in the second file, menu3.htm, as an additional frame. You can think of this first file as replacing the original regex2.htm, or that it replaces the parent frame. The second file, menu3.htm, contains the interface and functions for loading and for saving the data to and from the disk. The file JSON.js contains the functions for exporting the RegExTest object as text. It converts the object into a JavaScript Notation Object, or a string literal of the object. That means that one call of the eval() function gives us our object back when reading it from the disk. One function call to stringify() (from JSON.js) is all we need to export our object as text to save it to disk.

The actual code for loading and for saving looks like this:

var strFilename = "RegEx.ret";

function open_file() {
    strFilename = myFileObject.getFilename('open',strFilename, 
                           '*.ret','RegEx Test Files (*.ret)')
    if (strFilename != '') {
        var aa = myFileObject.loadFile(strFilename);
        eval('top.RegExTest = ' + aa + ';');
    } else {
        strFilename = "RegEx.ret"
    };
    top.CopyDown();
};
function save_file () {
    top.CopyUp();
    if (strFilename == '') {strFilename = "RegEx.ret"};
    var strNewFilename = myFileObject.getFilename("save", 
             strFilename,"*.ret","RegEx Test Files (*.ret)")
    if (strNewFilename != "") {
        if (!myFileObject.saveFile(strNewFilename, stringify(top.RegExTest))) {
            alert("failed to save file");
        } else {
            strFilename = strNewFilename;
        };
    };
};

File Contents

filename Description
regex2.hta Parent frame, RegExTest object
support/menu3.htm Interface and functions for saving/loading the RegExTest object to/from disk
others See my other article for details

History

First posted 2004-10-20.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here