{"id":26540169,"url":"https://github.com/xxoo/jsex","last_synced_at":"2026-05-20T14:38:13.015Z","repository":{"id":57153298,"uuid":"304803493","full_name":"xxoo/jsex","owner":"xxoo","description":"data serialization/deserialization for javascript","archived":false,"fork":false,"pushed_at":"2021-11-01T01:19:22.000Z","size":86,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-26T03:43:53.158Z","etag":null,"topics":["javascript","json","serialize-data"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xxoo.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-17T05:21:15.000Z","updated_at":"2021-12-11T12:09:34.000Z","dependencies_parsed_at":"2022-09-06T11:22:37.151Z","dependency_job_id":null,"html_url":"https://github.com/xxoo/jsex","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxoo%2Fjsex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxoo%2Fjsex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxoo%2Fjsex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxoo%2Fjsex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xxoo","download_url":"https://codeload.github.com/xxoo/jsex/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244890114,"owners_count":20527030,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","json","serialize-data"],"created_at":"2025-03-22T00:29:25.573Z","updated_at":"2026-05-20T14:38:07.993Z","avatar_url":"https://github.com/xxoo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## What is jsex?\njsex is a strict subset of javascript designed for data serialization/deserialization. It supports most native javascript data types. You may consider it as **JSON Extended** or **JavaScript Expression**.\n\n\n## How many data types are supported?\nAs many as possible, including:\n* All types supported by JSON\n* function\n* bigint\n* symbol\n* Date\n* Error\n* RegExp\n* Set\n* Map\n* Typed Arrays\n* Infinity, NaN and undefined\n\n\n## How many browsers are supported?\njsex relies on certain ES2020 features, making it difficult to provide a comprehensive list of compatible browsers. To ensure proper functionality in production environments, we recommend installing appropriate polyfills or using transpilers like Babel.\n\n\n## How to serialize data?\nBy calling `toJsex(data, options)`.\n* `options` defaults to `{sorting: false, implicitConversion: false, jsonCompatible: false, debug: false}`.\n  - `sorting`: Whether sorting the contents of `Map`, `Set` and `Object`.\n  - `implicitConversion`: Whether attempts to resolve unrecognized type by calling its `valueOf` method.\n  - `jsonCompatible`: Whether generating JSON compatible string.\n  - `debug`: Whether throw error for unexpected data instead of skip them silently. eg: native functions, cyclic references, etc.\n### serializing example:\n```javascript\nrequire('jsex');\nlet data = {\n  someRegex: RegExp('\\r\\u2028\\n\\ud800', 'ig'),\n  someSet: new Set([a =\u003e a, 1, 0n]),\n  [Symbol.for('symbolKey')]: 'valueForSymbolKey',\n  normalKey: 'valueForNormalKey'\n};\nconsole.log('normal:', toJsex(data), '\\nsorted:', toJsex(data, {sorting: true}));\n//normal: {\"someRegex\":/\\r\\u2028\\n\\ud800/gi,\"someSet\":new Set([Function(\"a\",\"return a\"),1,0n]),\"normalKey\":\"valueForNormalKey\",[Symbol.for(\"symbolKey\")]:\"valueForSymbolKey\",\"__proto__\":null}\n//sorted: {\"normalKey\":\"valueForNormalKey\",\"someRegex\":/\\r\\u2028\\n\\ud800/gi,\"someSet\":new Set([0n,1,Function(\"a\",\"return a\")]),[Symbol.for(\"symbolKey\")]:\"valueForSymbolKey\",\"__proto__\":null}\ntry {\n  JSON.parse(toJsex(data, {jsonCompatible: true}));\n} catch(e) {\n  console.log('error: jsonCompatible makes sense only if data does not contain extended types');\n}\n```\n### another serializing example:\n```javascript\nlet obj = {\n    [\"__proto__\"]: '\\v',\n    \"tab\": \"\\t\"\n  },\n  jsonstr = JSON.stringify(obj),\n  jsexstr = toJsex(obj);\nconsole.log('jsexstr:', jsexstr, '\\njsonstr:', jsonstr);\n//jsexstr: {[\"__proto__\"]:\"\u000b\",\"tab\":\"\t\",\"__proto__\":null}\n//jsonstr: {\"__proto__\":\"\\u000b\",\"tab\":\"\\t\"}\nconsole.log('is compatible:', toJsex(obj, {jsonCompatible: true}) === jsonstr);\n//is compatible: true\n```\n\n\n## How to deserialize?\nBasically you can just `eval` the string if you trust the source. However if you don't, use `String.prototype.parseJsex(forbiddenMethods)` instead. This method returns `undefined` if parsing failed, or an `Object` with a `length` key (indicating the number of parsed characters) and a `value` key (containing the actual result).\n* `forbiddenMethods` defaults to a `Set` containing all implicit methods of the current javascript engine. These methods are typically excluded to prevent automatic execution. You can also set this parameter to `null` or provide a custom `Set`.\n### deserializing example:\n```javascript\n//following the above code\nlet evalJsex = Function('return ' + jsexstr)(),\n  parseJsex = jsexstr.parseJsex().value,\n  evalJson = Function('return ' + jsonstr)(),\n  parseJson = JSON.parse(jsonstr),\n  parseJsonByJsex = jsonstr.parseJsex().value;\nconsole.log('evalJsex:', evalJsex, '\\nparseJsex:', parseJsex, '\\nevalJson:', evalJson, '\\nparseJson:', parseJson, '\\nparseJsonByJsex:', parseJsonByJsex);\nconsole.log('json is a subset of javascript?', JSON.stringify(evalJson) === JSON.stringify(parseJson));\n//json is a subset of javascript? false\nconsole.log('jsex is a subset of javascript?', JSON.stringify(evalJsex) === JSON.stringify(parseJsex) \u0026\u0026 JSON.stringify(evalJson) === JSON.stringify(parseJsonByJsex));\n//jsex is a subset of javascript? true\n```\n\n\n## Does `parseJsex` support JSON string?\nYes, but any `__proto__` key of `Object` in JSON objects will be ignored, as demonstrated in the example above.\n\n\n## How to serialize a `class`?\nFor security reason, `class` is not supported by default. However, you can serialize them as strings by calling `toJsex` with `implicitConversion` option set to `true`.\n### class example:\n```javascript\nclass customType {\n  constructor () {\n    this.args = [...arguments];\n  }\n  valueOf() {\n    return this.args;\n  }\n}\nlet source = toJsex(customType, {implicitConversion: true});\nlet deserializedClass = Function('return ' + source.parseJsex().value)();\nconsole.log(deserializedClass.toString() === customType.toString());\n//true\n```\n\n\n## How to serialize a custom type?\njsex doesn't support custom type definitions. However, you can resolve custom types to supported types by implementing a `valueOf` method, then calling `toJsex` with the `implicitConversion` option set to `true`.\n### custom type example:\n```javascript\n//following the above code\nlet instance1 = new customType(1, 2n, {});\nlet jsex = toJsex(instance1, {implicitConversion: true});\nconsole.log(jsex);\n//[1,2n,{\"__proto__\":null}]\nlet instance2 = Reflect.construct(deserializedClass, jsex.parseJsex().value);\n```\n\n\n## Can I use comments in jsex?\nYes, comments are allowed, but not in all positions. For example, `-/*123*/4` is invalid in jsex.\n\n\n## Is there any other difference between JSON and jsex?\nYes, there are a few more differences.\n* jsex differentiates between `0` and `-0`.\n* `Object` in jsex have no prototype, making any key name safe to use.\n* By default, `toJsex` doesn't escape ASCII control characters (except for `\\r` and `\\n`).\n* `toJsex` includes non-enumerable properties and symbol keys in `Object`.\n\n\n## When should I use jsex?\nWhen you are using javascript, but JSON does not fit your needs.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxxoo%2Fjsex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxxoo%2Fjsex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxxoo%2Fjsex/lists"}