<commit_before>import { map, each, entries } from "./iterator";
import { List } from "./immutable/list";
import { Record } from "./immutable/record";
import { fail } from "./assert";


export const to_json = (x) => {
  if (x === null ||
      x === true ||
      x === false ||
      typeof x === "number" ||
      typeof x === "string") {
    return x;

  } else if (x.to_json != null) {
    return x.to_json();

  } else {
    fail("Cannot convert to JSON: " + x);
  }
};

export const from_json = (x) => {
  if (x === null ||
      x === true ||
      x === false ||
      typeof x === "number" ||
      typeof x === "string") {
    return x;

  } else if (Array["isArray"](x)) {
    return List(map(x, from_json));

  } else if (typeof x === "object") {
    return Record(map(entries(x), ([key, value]) => [key, from_json(value)]));
<commit_msg>Switching from List and Record to ImmutableList and ImmutableRecord<commit_after>import { map, each, entries } from "./iterator";
import { ImmutableList } from "./mutable/list";
import { ImmutableRecord } from "./mutable/record";
import { fail } from "./assert";


export const to_json = (x) => {
  if (x === null ||
      x === true ||
      x === false ||
      typeof x === "number" ||
      typeof x === "string") {
    return x;

  } else if (x.to_json != null) {
    return x.to_json();

  } else {
    fail("Cannot convert to JSON: " + x);
  }
};

export const from_json = (x) => {
  if (x === null ||
      x === true ||
      x === false ||
      typeof x === "number" ||
      typeof x === "string") {
    return x;

  } else if (Array["isArray"](x)) {
    return new ImmutableList(map(x, from_json));

  } else if (typeof x === "object") {
    const o = {};

    // TODO inefficient ?
    each(entries(x), ([key, value]) => {
      o[key] = from_json(value);
    });

    return new ImmutableRecord(o);

{'fraction_non_alphanumeric': 0.19592055823939883, 'fraction_numerical': 0.0, 'mean_word_length': 2.537001897533207, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2592603', 'n_tokens_mistral': 620, 'n_tokens_neox': 610, 'n_words': 185}