<commit_before>import TypeSerializer from "../type_serializer";
import Constructor from "../types/constructor";
import JsonType from "./json_type";
import './predefined';

const picker = new TypeSerializer.Picker<JsonType>('json');

/**
 * Serialize given serializable type instance to a JSON-compatible type
 * @param serializable Serializable type instance
 * @param ctor _(optional)_ Serializable type constructor function. If provided, it overrides the type of serializable.
 * @returns JSON-compatible type which can be safely passed to `JSON.serialize`
 */
export function serializeToJson<TOriginal>(serializable: TOriginal, ctor?: Constructor<TOriginal>): JsonType {

    let serialized: JsonType;

    if (serializable === null || serializable === undefined) {
        serialized = serializable as null | undefined;
    } else {
        const { down } = typeof(ctor) === 'function' ? picker.pickForType(ctor) : picker.pickForValue(serializable);
        if (!down) {
            throw new Error(`Unable to serialize a value: ${serializable}`);
        }
        serialized = down(serializable);
    }

    return serialized;

}

/**
 * Construct/deserialize a serializable type instance from a JSON-compatible object
 * @param serialized JSON-compatible object (e.g. returned from `JSON.parse`)
 * @param ctor Serializable type constructor function
 * @returns Serializable type instance
 */
export function deserializeFromJson<TOriginal>(serialized: JsonType, ctor: Constructor<TOriginal>): TOriginal {

    if (typeof(ctor) !== 'function') {
        throw new Error('Expecting a constructor function');
    }

    const { up } = picker.pickForType(ctor);

    if (!up) {
        throw new Error(`Unable to deserialize an instance of "${ctor.name}" from: ${serialized}`);
    }

    return up(serialized);

}

// Functions
export {
    serializeToJson as serialize, // alias
    deserializeFromJson as deserialize // alias
};

// Decorators
export {
    default as JsonSerializable,
    default as Serializable // alias
} from './json_serializable';
<commit_msg>Added export of JSON types<commit_after>import TypeSerializer from "../type_serializer";
import Constructor from "../types/constructor";
import JsonType from "./json_type";
import './predefined';

const picker = new TypeSerializer.Picker<JsonType>('json');

/**
 * Serialize given serializable type instance to a JSON-compatible type
 * @param serializable Serializable type instance
 * @param ctor _(optional)_ Serializable type constructor function. If provided, it overrides the type of serializable.
 * @returns JSON-compatible type which can be safely passed to `JSON.serialize`
 */
export function serializeToJson<TOriginal>(serializable: TOriginal, ctor?: Constructor<TOriginal>): JsonType {

    let serialized: JsonType;

    if (serializable === null || serializable === undefined) {
        serialized = serializable as null | undefined;
    } else {
        const { down } = typeof(ctor) === 'function' ? picker.pickForType(ctor) : picker.pickForValue(serializable);
        if (!down) {
            throw new Error(`Unable to serialize a value: ${serializable}`);
        }
        serialized = down(serializable);
    }

    return serialized;

}

/**
 * Construct/deserialize a serializable type instance from a JSON-compatible object
 * @param serialized JSON-compatible object (e.g. returned from `JSON.parse`)
 * @param ctor Serializable type constructor function
 * @returns Serializable type instance
 */
export function deserializeFromJson<TOriginal>(serialized: JsonType, ctor: Constructor<TOriginal>): TOriginal {

    if (typeof(ctor) !== 'function') {
        throw new Error('Expecting a constructor function');
    }

    const { up } = picker.pickForType(ctor);

    if (!up) {
        throw new Error(`Unable to deserialize an instance of "${ctor.name}" from: ${serialized}`);
    }

    return up(serialized);

}

// Functions
export {
    serializeToJson as serialize, // alias
    deserializeFromJson as deserialize // alias
};

// Decorators
export {
    default as JsonSerializable,
    default as Serializable // alias
} from './json_serializable';

// Types
export * from './json_type';

{'fraction_non_alphanumeric': 0.1111111111111111, 'fraction_numerical': 0.0, 'mean_word_length': 3.82183908045977, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4081107', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1103, 'n_words': 368}