MCPcopy
hub / github.com/AykutSarac/jsoncrack.com / contentToJson

Function contentToJson

apps/www/src/lib/utils/jsonAdapter.ts:4–52  ·  view source on GitHub ↗
(value: string, format = FileFormat.JSON)

Source from the content-addressed store, hash-verified

2import { FileFormat } from "../../enums/file.enum";
3
4export const contentToJson = (value: string, format = FileFormat.JSON): Promise<object> => {
5 return new Promise(async (resolve, reject) => {
6 try {
7 if (!value) return resolve({});
8
9 if (format === FileFormat.JSON) {
10 const { parse } = await import("jsonc-parser");
11 const errors: ParseError[] = [];
12 const result = parse(value, errors);
13 if (errors.length > 0) JSON.parse(value);
14 return resolve(result);
15 }
16
17 if (format === FileFormat.YAML) {
18 const { load } = await import("js-yaml");
19 return resolve(load(value) as object);
20 }
21
22 if (format === FileFormat.XML) {
23 const { XMLParser } = await import("fast-xml-parser");
24 const parser = new XMLParser({
25 attributeNamePrefix: "$",
26 ignoreAttributes: false,
27 allowBooleanAttributes: true,
28 parseAttributeValue: true,
29 trimValues: true,
30 parseTagValue: true,
31 });
32 return resolve(parser.parse(value));
33 }
34
35 if (format === FileFormat.CSV) {
36 const { csv2json } = await import("json-2-csv");
37 const result = csv2json(value, {
38 trimFieldValues: true,
39 trimHeaderFields: true,
40 wrapBooleans: true,
41 excelBOM: true,
42 });
43 return resolve(result);
44 }
45
46 return resolve({});
47 } catch (error) {
48 const errorMessage = error instanceof Error ? error.message : "Failed to parse content";
49 return reject(errorMessage);
50 }
51 });
52};
53
54export const jsonToContent = async (json: string, format: FileFormat): Promise<string> => {
55 return new Promise(async resolve => {

Callers 3

ToolPageFunction · 0.90
generateTypeFunction · 0.90
useFile.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected