triggerdotdev / jsonhero-web

JSON Hero is an open-source, beautiful JSON explorer for the web that lets you browse, search and navigate your JSON files at speed. 🚀. Built with 💜 by the Trigger.dev team.
https://jsonhero.io
Apache License 2.0
9.42k stars 512 forks source link

Receiving 'RangeError: invalid ISO 8601 string' via API for Numbers-only Strings #171

Open obliqueRotationMethod opened 1 year ago

obliqueRotationMethod commented 1 year ago

Hi,

running locally, I POST an pretty big JSON to the API. Some of my Strings in the JSON have Number-Characters only (userIDs). When opening the link, console is showing a lot of 'RangeError: invalid ISO 8601 string: 12345' - warnings/ errors. It takes up to one minute to open the link, npm service is running on 100% CPU usage

On the site, I see that the program interprets my Number-Strings as Date-Objects (string/datetime/rfc3339). Example: {"lastModifiedBy": "12345678"}

My actual date strings in there are ISO 8601-formated. Maybe a workaround could be to add a parameter to state the date format. To let the backend know which Strings to parse as dates and leave the rest as normal text?

CharkeyQK commented 4 months ago

same issue.

CharkeyQK commented 4 months ago

ChatGPT helps:

import { inferType as originalInferType, JSONValueType, JSONIntType, JSONStringType } from '@jsonhero/json-infer-types';

export function inferType(value: unknown): JSONValueType {
  const result: JSONValueType = originalInferType(value) as JSONValueType;

  // check
  if (typeof value === 'number') {
    // if result is JSONStringType and format is datetime,change to JSONIntType
    if (result.name === 'string' && 'format' in result && result.format?.name === 'datetime') {
      return { name: 'int', value } as JSONIntType;
    }

    // return int directly
    return { name: 'int', value } as JSONIntType;
  }

  // another check
  if (result.name === 'string') {
    const stringValue = result.value as string;
    // if string looks like number,and should not as datetime
    if (/^\d+$/.test(stringValue) && result.format?.name === 'datetime') {
      return { name: 'int', value: parseInt(stringValue, 10) } as JSONIntType;
    }
  }

  return result;
}