Git Product home page Git Product logo

lame-json's Introduction

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 length of the string is also the number of digits in the parsed number.
  • 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(). Optionsa re 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.

lame-json's People

Contributors

donutespresso avatar micahr avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.