Click here to Skip to main content
15,867,488 members
Articles / Web Development / HTML
Article

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

Rate me:
Please Sign up or sign in to vote.
3.46/5 (4 votes)
19 Oct 2004CPOL2 min read 42.2K   486   7  
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:

JavaScript
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

filenameDescription
regex2.htaParent frame, RegExTest object
support/menu3.htmInterface and functions for saving/loading the RegExTest object to/from disk
othersSee my other article for details

History

First posted 2004-10-20.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
I am a former Witango (aka Tango from Pervasive/Everyware) programmer.
Now programming is my avocation.

Comments and Discussions

 
-- There are no messages in this forum --