Compiler

JavaScript (Browser)

Start

The PowerJSON JavaScript parser provides a total of 2 methods:

  • parse (for converting PowerJSON strings to JavaScript objects)
  • stringify (for converting JavaScript objects to JSON strings)

When you successfully installed the PowerJSON JavaScript parser in an HTML page, you can use the following methods to call the methods provided by the PowerJSON JavaScript parser:

PJSON.parse(str);

// or

PJSON.stringify(obj);

Example

Here's an example of using the PowerJSON JavaScript parser:

// Convert a PowerJSON string to a JSON string
const pjobj = PJSON.parse(`
    {
        helloText: 'hello world'
    }
`)

const jstr = PJSON.stringify(pjobj);

/*
    {
        "helloText": "hello world"
    }
 */
console.log(jstr);

Node.js

Start

The PowerJSON Node.js parser provides a total of 6 methods:

  • parse (for converting PowerJSON strings to JavaScript objects)

  • stringify (for converting JavaScript objects to JSON strings)

  • parseFile (for reading .pjson files and converting them to JavaScript objects) [asynchronous]

  • stringifyFile (for converting JavaScript objects into JSON strings and storing them in a file) [asynchronous]

  • parseFileSync (synchronized version of the parseFile method)

  • stringifyFileSync (synchronized version of the stringifyFileSync method)

Example

When you successfully installed the PowerJSON Node.js parser in the HTML page (recommended to use NPM installation), you can use require() to import the parser:

const pjson = require('@powerjson/powerjson');

Then you can try each method of the parser:

pjson.parse(`
    {
        helloText: 'hello world'
    }
`)

// Need you to have an index.pjson file
// This function returns a Promise object
pjson.parseFile('./index.pjson').then(function() {
    // do something
})

pjson.parseFileSync('./index.pjson');

pjson.stringify({
    helloText: 'hello world'
})

// This function returns a Promise object
pjson.stringifyFile('./write.pjson', {
    helloText: 'hello world'
}).then(function() {
    // do something
})

pjson.stringifyFileSync('./write.pjson', {
    helloText: 'hello world'
})

Other languages

Currently we don't have an official parser other than JavaScript or Node.js, but our future plan is to create a Java, Python PowerJSON parser. If the language you are used to is Python or Java, stay tuned!πŸŽ’