Skip to main content

Class: JSONParser

@divine/uri.JSONParser

The application/json and application/*+json parser handles JSON using JSON.parse, JSON.stringify and StringParser.

Hierarchy

Constructors

constructor

Protected new JSONParser(contentType)

Constructs a new Parser instance.

Parameters

NameTypeDescription
contentTypeContentTypeThe media type this parser object was instanciated for.

Inherited from

Parser.constructor

Defined in

uri/src/parsers.ts:214

Properties

contentType

Readonly contentType: ContentType

The media type this parser object was instanciated for.

Inherited from

Parser.contentType

Defined in

uri/src/parsers.ts:214

Methods

_assertSerializebleData

Protected _assertSerializebleData(condition, data, cause?): asserts condition

A helper method used by parser subclasses to report invalid input.

Parameters

NameTypeDescription
conditionbooleanMust be true, or else a ParserError will be raised.
dataunknownSome kind of extra information that will be provided in data.
cause?unknownIf this error was caused by some other kind of failure, the original error will be available as cause.

Returns

asserts condition

Inherited from

Parser._assertSerializebleData

Defined in

uri/src/parsers.ts:246


parse

parse(stream): Promise<BasicTypes>

Parses a stream and returns the result as a parser-specific representation.

This method must be implemented by the actual subclass.

Throws

ParserError On parser errors.

Parameters

NameTypeDescription
streamAsyncIterable<Buffer>The stream to parse.

Returns

Promise<BasicTypes>

The parsed stream.

Overrides

Parser.parse

Defined in

uri/src/parsers/json.ts:9


serialize

serialize(data): Buffer

Serializes a parsed or manually constructed object back into a Buffer or byte stream.

This method must be implemented by the actual subclass.

Throws

ParserError On serialization errors.

Parameters

NameTypeDescription
dataunknownA parser-specific representation that is to be serialized.

Returns

Buffer

A Buffer or a byte stream.

Overrides

Parser.serialize

Defined in

uri/src/parsers/json.ts:13


parse

Static parse<T>(stream, contentType): Promise<T & Finalizable>

Parses a given string, Buffer or byte stream using a parser registered for a specific media type.

NOTE: This method always returns an object. Primitives are never returned. This means that text, for instance will be returned as a String object, null as Object(NULL) and undefined as Object(VOID). You may use toPrimitive to return the original value, or use .valueOf() and test the result against the NULL and VOID symbols.

Throws

ParserError On parser errors or if the media type is not recognized.

Type parameters

NameTypeDescription
Textends objectThe type of the returned object.

Parameters

NameTypeDescription
streamstring | Buffer | AsyncIterable<string | Buffer>The source that should be parsed.
contentTypestring | ContentTypeThe media type that specifies what parser to use.

Returns

Promise<T & Finalizable>

An object (always an object) that represents the original source after parsing. It's possible that the Parser subclass allocated temporary resources as part of the process. These resources may be cleaned up by calling FINALIZE.

Inherited from

Parser.parse

Defined in

uri/src/parsers.ts:106


register

Static register(type, parser): typeof Parser

Registers a new parser/serializer. All subclasses must register their MIME media type support with this method.

Parameters

NameTypeDescription
typestring | RegExpThe content/media type the parser can handle.
parsertypeof ParserThe Parser subclass to register.

Returns

typeof Parser

The Parser base class (for method chaining).

Inherited from

Parser.register

Defined in

uri/src/parsers.ts:85


serialize

Static serialize<T>(data, contentType?): [Buffer | Readable & AsyncIterable<Buffer>, ContentType]

Converts a parsed (or manually constructed) object back into a byte stream representation.

Buffers and ReadableStream will be passed through as-is. Strings will just be encoded using the charset param from contentType (or UTF-8 if not present). Everything else is serialized using a Parser subclass.

Throws

ParserError On serialization errors or if the media type is not recognized.

Type parameters

NameTypeDescription
TunknownThe type of the object that is to be serialized.

Parameters

NameTypeDescription
dataTThe object that is to be serialized.
contentType?string | ContentTypeThe media type that specifies what parser to use.

Returns

[Buffer | Readable & AsyncIterable<Buffer>, ContentType]

A tuple containing the Buffer/byte stream and the actual media type. Note that the parser may return a slightly different media type than was given (for instance, MultiPartParser might add a boundary param if none was given).

Inherited from

Parser.serialize

Defined in

uri/src/parsers.ts:132


serializeToBuffer

Static serializeToBuffer<T>(data, contentType?): Promise<[Buffer, ContentType]>

Converts a parsed (or manually constructed) object into a Buffer.

This is a convenience method that just invokes parse and then converts the byte stream into a single Buffer.

Throws

ParserError On serialization errors or if the media type is not recognized.

Type parameters

NameType
Tunknown

Parameters

NameTypeDescription
dataTThe object that is to be serialized.
contentType?string | ContentTypeThe media type that specifies what parser to use.

Returns

Promise<[Buffer, ContentType]>

A tuple containing the Buffer and the actual media type. Note that the parser may return a slightly different media type than was given (for instance, MultiPartParser might add a boundary param if none was given).

Inherited from

Parser.serializeToBuffer

Defined in

uri/src/parsers.ts:183