3.0.0 • Published 8 months ago

@chharvey/requirejson v3.0.0

Weekly downloads
132
License
MIT
Repository
github
Last release
8 months ago

requirejson

Require JSON files with a file extension other than .json.

Warning: Deprecated!

This package is now deprecated. Use native ES module imports with import attributes (with) instead. Double-check engine support before switching.

import my_json_object from './my-file.jsonld' with {type: 'json'};

console.log(my_json_object['@type']);

In TypeScript, you may wish to explicitly declare the type of this imported object as a new constant, since its type is not inferred.

import my_json_object from './my-file.jsonld' with {type: 'json'};

const my_json_object_typed: MyJsonLdType = my_json_object;

console.log(my_json_object_typed['@type']);
//                              ^ no type error if the shape of `MyJsonLdType` is correct

Note: Though this package is deprecated, you may still install it to import and use the attached JSON types. Just be aware that this package is no longer maintained, so you should look for an alternate solution.

import type {JSONValue, JSONObject, JSONArray, JSONPrimitive} from '@chharvey/requirejson';

Install

$ npm install @chharvey/requirejson

Use

Asynchronously

const {requireJSON} = require('@chharvey/requirejson');

requireJSON('./my-file.jsonld').then((my_json_object) => {
	console.log(my_json_object['@type']);
});

Synchronously

const {requireJSONSync} = require('@chharvey/requirejson');

const my_json_object = requireJSONSync('./my-file.jsonld');
console.log(my_json_object['@type']);

API

import {
	requireJSON,
	requireJSONSync,
	JSONValue,
	JSONObject,
	JSONArray,
	JSONPrimitive,
} from '@chharvey/requirejson';

const my_json_object: Promise<JSONValue> = requireJSON('./my-file.jsonld');
const my_json_object_sync: JSONValue = requireJSONSync('./my-file.jsonld');

Functions

requireJSON(filepath: string): Promise<JSONValue>

Asynchronously returns a JSON value that is the result of parsing the file contents.

Parameters:

  • filepath: the path of the file to read, relative to current working directory

Returns:

A Promise resolving to a JSON value parsed from the file contents.

requireJSONSync(filepath: string): JSONValue

Synchronously returns a JSON value that is the result of parsing the file contents.

Parameters:

  • filepath: the path of the file to read, relative to current working directory

Returns:

A JSON value parsed from the file contents.

Types

type namedefinitiondescription
JSONValueJSONObject or JSONArray or JSONPrimitiveAny valid JSON value.
JSONObject{[key: string]?: JSONValue}A general JSON object, with string keys and JSONValue values.
JSONArrayJSONValue[]A JSON array, with JSONValue entries.
JSONPrimitivestring or number or boolean or nullA JSON primitive.
3.0.0

8 months ago

2.0.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.2.0-beta.1

6 years ago

1.2.0-beta

6 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago