Skip to content

Repository files navigation

chubbyts-decode-encode

CI Coverage Status Mutation testing badge npm-version

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.

Requirements

Installation

Through NPM as @chubbyts/chubbyts-decode-encode.

npm i @chubbyts/chubbyts-decode-encode@^2.5.1

Usage

Decoder

createDecoder

import { createDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/decoder';
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder';

const decoder = createDecoder([createJsonTypeDecoder()]);
const data = decoder.decode('{"key":"value"}', 'application/json'); // or with 3th argument, for example { user: 'username1' }
// data: {key: "value"}
const contentTypes = decoder.contentTypes;
// contentTypes: ['application/json']

createJsonTypeDecoder

import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder';

const jsonTypeDecoder = createJsonTypeDecoder();
const data = jsonTypeDecoder.decode('{"key":"value"}');
// data: {key: "value"}
const contentType = jsonTypeDecoder.contentType;
// contentType: 'application/json'

createJsonxTypeDecoder

import { createJsonxTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/jsonx-type-decoder';

const jsonxTypeDecoder = createJsonxTypeDecoder();
const data = jsonxTypeDecoder.decode(`
<?xml version="1.0" encoding="UTF-8"?>
<json:object>
  <json:string name="key">value</json:string>
</json:object>
`);
// data: {key: "value"}
const contentType = jsonxTypeDecoder.contentType;
// contentType: 'application/jsonx+xml'

createUrlEncodedTypeDecoder

import { createUrlEncodedTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/url-encoded-type-decoder';

const urlEncodedTypeDecoder = createUrlEncodedTypeDecoder();
const data = urlEncodedTypeDecoder.decode('key=value');
// data: {key: "value"}
const contentType = urlEncodedTypeDecoder.contentType;
// contentType: 'application/x-www-form-urlencoded'

createYamlTypeDecoder

import { createYamlTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/yaml-type-decoder';

const yamlTypeDecoder = createYamlTypeDecoder();
const data = yamlTypeDecoder.decode('key: value');
// data: {key: "value"}
const contentType = yamlTypeDecoder.contentType;
// contentType: 'application/x-yaml'

Encoder

createEncoder

import { createEncoder } from '@chubbyts/chubbyts-encode-encode/dist/encoder/encoder';
import { createJsonTypeEncoder } from '@chubbyts/chubbyts-encode-encode/dist/encoder/json-type-encoder';

const encoder = createEncoder([createJsonTypeEncoder()]);
const encodedData = encoder.encode({key: "value"}, 'application/json'); // or with 3th argument, for example { user: 'username1' }
// encodedData: {"key":"value"}
const contentTypes = encoder.contentTypes;
// contentTypes: ['application/json']

createJsonTypeEncoder

import { createJsonTypeEncoder } from '@chubbyts/chubbyts-encode-encode/dist/encoder/json-type-encoder';

const jsonTypeEncoder = createJsonTypeEncoder();
const encodedData = jsonTypeEncoder.encode({key: "value"});
// encodedData: {"key":"value"}
const contentType = jsonTypeEncoder.contentType;
// contentTypes: 'application/json'

createJsonxTypeEncoder

import { createJsonxTypeEncoder } from '@chubbyts/chubbyts-encode-encode/dist/encoder/jsonx-type-encoder';

const jsonxTypeEncoder = createJsonxTypeEncoder();
const encodedData = jsonxTypeEncoder.encode({key: "value"});
// encodedData: <?xml version="1.0" encoding="UTF-8"?>
// <json:object>
//   <json:string name="key">value</json:string>
// </json:object>
const contentType = jsonxTypeEncoder.contentType;
// contentTypes: 'application/jsonx+xml'

createUrlEncodedTypeEncoder

import { createUrlEncodedTypeEncoder } from '@chubbyts/chubbyts-encode-encode/dist/encoder/url-encoded-type-encoder';

const urlEncodedTypeEncoder = createUrlEncodedTypeEncoder();
const encodedData = urlEncodedTypeEncoder.encode({key: "value"});
// encodedData: key=value
const contentType = urlEncodedTypeEncoder.contentType;
// contentTypes: 'application/x-www-form-urlencoded'

createYamlTypeEncoder

import { createYamlTypeEncoder } from '@chubbyts/chubbyts-encode-encode/dist/encoder/yaml-type-encoder';

const yamlTypeEncoder = createYamlTypeEncoder();
const encodedData = yamlTypeEncoder.encode({key: "value"});
// encodedData: key: value
const contentType = yamlTypeEncoder.contentType;
// contentTypes: 'application/x-yaml'

Service factories (chubbyts-dic-config)

The package ships service factories (abstract factories built on chubbyts-dic-config-factory) for a chubbyts-dic-config (or any chubbyts-dic-types compatible) container within @chubbyts/chubbyts-decode-encode/dist/service-factory:

  • typeDecodersServiceFactory: all shipped type decoders (json, jsonx, urlencoded, yaml)
  • typeEncodersServiceFactory: all shipped type encoders (json, jsonx, urlencoded, yaml), config.debug enables pretty printing for json / jsonx
  • decoderServiceFactory: the decoder, built from the typeDecoders service if registered, the shipped type decoders otherwise
  • encoderServiceFactory: the encoder, built from the typeEncoders service if registered, the shipped type encoders otherwise
import type { ConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
import { createContainerByConfigFactory } from '@chubbyts/chubbyts-dic-config/dist/dic-config';
import type { Decoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/decoder';
import type { Encoder } from '@chubbyts/chubbyts-decode-encode/dist/encoder/encoder';
import { decoderServiceFactory, encoderServiceFactory } from '@chubbyts/chubbyts-decode-encode/dist/service-factory';

const container = createContainerByConfigFactory({
  debug: false,
  dependencies: {
    factories: new Map<string, ConfigFactory>([
      ['decoder', decoderServiceFactory()],
      ['encoder', encoderServiceFactory()],
    ]),
  },
})();

const decoder = container.get<Decoder>('decoder');
const encoder = container.get<Encoder>('encoder');

To replace the type decoders / encoders, register a typeDecoders / typeEncoders service:

import type { TypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/decoder';
import { createJsonTypeDecoder } from '@chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder';

const container = createContainerByConfigFactory({
  dependencies: {
    factories: new Map<string, ConfigFactory>([
      ['decoder', decoderServiceFactory()],
      ['typeDecoders', (): Array<TypeDecoder> => [createJsonTypeDecoder(), createCustomTypeDecoder()]],
    ]),
  },
})();

With names

To use different decoders / encoders in different parts of an application, the same factories can be registered multiple times with a name, which gets appended to each service id (decoderapi, typeDecodersapi, ...).

const container = createContainerByConfigFactory({
  dependencies: {
    factories: new Map<string, ConfigFactory>([
      ['decoder', decoderServiceFactory()],
      ['decoderapi', decoderServiceFactory('api')],
      ['typeDecodersapi', (): Array<TypeDecoder> => [createJsonTypeDecoder()]],
    ]),
  },
})();

const decoder = container.get<Decoder>('decoder'); // all shipped type decoders
const apiDecoder = container.get<Decoder>('decoderapi'); // json only

Copyright

2026 Dominik Zogg

About

A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages