GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - DonutEspresso/lame-json: Parse JSON out of strings

BranchesTags
Open more actions menu

Latest commit

 

History

58 Commits
58 Commits

Folders and files

NameName
Last commit message
Last commit date
lib
lib
 
 
test
test
 
 
tools/githooks
tools/githooks
 
 
.eslintrc
.eslintrc
 
 
.eslintrc.js
.eslintrc.js
 
 
.gitignore
.gitignore
 
 
.jscsrc
.jscsrc
 
 
.prettierignore
.prettierignore
 
 
.prettierrc.js
.prettierrc.js
 
 
.travis.yml
.travis.yml
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CHANGES.md
CHANGES.md
 
 
LICENSE.md
LICENSE.md
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
api.md
api.md
 
 
package.json
package.json
 
 
pre-push
pre-push
 
 

Repository files navigation

lame-json

NPM Version Build Status Coverage Status Dependency Status devDependency Status

Module that attempts to parse string values of an object into JSON.

Getting Started

Install the module with: npm install lame-json

Documentation

Sometimes JSON comes back with values as a string.

{
    "foo": "true",
    "bar": "123",
    "baz": "{\"hello\":\"world\"}",
    "qux": "[ 1, 2, 3 ]"
}

In some cases, these values are meant to be parsed into primitives or native JS objects:

{
    foo: true,
    bar: 123,
    baz: {
        "hello": "world"
    },
    qux: [ 1, 2, 3 ]
}

This module helps you do that.

Examples

Usage is very straight forward:

var lameJson = require('lame-json');
var data = {
    "foo": "true",
    "bar": "123",
    "baz": "{\"hello\":\"world\"}"
}

var newData = lameJson.parseJson(data);

/*
=> {
    foo: true,
    bar: 123,
    baz: {
        "hello": "world"
    }
}
*/

If a value cannot be parsed out of the string, the original value is returned:

var newData = lameJson.parseJson({
    "foo": "[ 1, 2, 3 }"
});

// => { "foo": "[ 1, 2, 3 }" }

If you want to parse a specific string value:

var newData = lameJson.parse('[1, 2, 3]');

// => [1, 2, 3]

If you want to parse many times using the same options, you can use the factory method returned by the module. The factory method accepts a set of options, which are then used to create a parser object that always uses those options:

var parser = lameJson({
    boolean: false
});

parser.parse('[1, 2, 3]');
// => [1, 2, 3]

parser.parse('false');
// => 'false'

parser.parseJson({
    "foo": "true",
    "bar": "123",
});
/*
=> {
    foo: 'true',
    bar: 123,
}
*/

API

lameJson(options)

Creates a lameJson parser object that alwayhs uses the passed in options. If you only need to use the functions once or twice, you can use the static functions documented below.

  • options.boolean {Boolean} - If true, parses booleans. Defaults to true.
  • options.float {Boolean} - If true, parses ints and floats. Defaults to true. This only works when the full string is used during parsing (e.g 123foo would be ignored)
  • options.exponential {Boolean} - If true, parses exponential numbers such as 2e2. Defaults to false. This only works when the full string is used during parsing (e.g 2e2foo would be ignored)
  • options.array {Boolean} - If true, attempts parse arrays. Defaults to true.
  • options.object {Boolean} - If true, attempts to parse objects. Defaults to true.

Returns: {Object} a parser object with two methods, parseJson and parse

lameJson.parseJson(obj [, options])

Try to parse string values out of JSON values. You can use this in place of JSON.parse(). Options are the same as above.

Returns: {Object} a new object with possibly parsed values.

lameJson.parse(str [, options])

Try to parse a possible value out of a string. This is the underlying implementation used by parseJson(). Options are the same as above.

Returns: {Object} a new object with possibly parsed values.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.

To start contributing, install the git pre-push hooks:

make githooks

Before committing, lint and test your code:

make prepush

License

Copyright (c) 2018 Alex Liu.

Licensed under the MIT license.

About

Parse JSON out of strings

Resources

Readme
MIT license
Activity

Stars

3 stars

Watchers

2 watching

Forks

2 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.