diff --git a/README.md b/README.md index e61b7b97..9ec89932 100644 --- a/README.md +++ b/README.md @@ -3,20 +3,48 @@

Coded Javascript Object Notation

+Why static JSON if you can utilize CJSON + +
+ ![NPM Package](https://github.com/SubhenduShekhar/cjson/actions/workflows/npm-publish.yml/badge.svg) -Why static JSON if you can utilize CJSON. - Create your files with extension `.cjson` to unlock all these features +
-
+# NODEJS + +## Steps + +- Create file with `.cjson` extension +- Write below code to decode the json: + +``` + import { Cjson } from 'coded-json'; + var cjson = new Cjson(file/path/to/file.cjson); + var b = cjson.deserialize(); +``` + +#### Output: + +``` +{ + "source": { + // Source JSON content + }, + "target": { + "fruit": "Apple", + "size": "Large", + "color": "Red" + } +} +``` ## Features - [Import multiple JSON files](#Import-multiple-JSON-files) - [Single/ Multiple line comments](#Single-Multiple-line-comments) - ## Import multiple JSON files You can use `$import` keyword for importing @@ -36,4 +64,9 @@ You can use `$import` keyword for importing For single line comments, use `//` -For multi line comments, use `/*` to start, `*/` to end \ No newline at end of file +For multi line comments, use like below: +``` +// This is first line comment +// This is the second one + +``` \ No newline at end of file diff --git a/npm/.gitignore b/npm/.gitignore index 822f8ef8..4509a9a6 100644 --- a/npm/.gitignore +++ b/npm/.gitignore @@ -1,3 +1,4 @@ node_modules/ out/ -package-lock.json \ No newline at end of file +package-lock.json +*.tgz \ No newline at end of file diff --git a/npm/README.md b/npm/README.md index e61b7b97..06489d24 100644 --- a/npm/README.md +++ b/npm/README.md @@ -3,20 +3,46 @@

Coded Javascript Object Notation

+Why static JSON if you can utilize CJSON + +
+ ![NPM Package](https://github.com/SubhenduShekhar/cjson/actions/workflows/npm-publish.yml/badge.svg) -Why static JSON if you can utilize CJSON. - Create your files with extension `.cjson` to unlock all these features +
-
+## Steps + +- Create file with `.cjson` extension +- Write below code to decode the json: + +``` + import { Cjson } from 'coded-json'; + var cjson = new Cjson(file/path/to/file.cjson); + var b = cjson.deserialize(); +``` + +#### Output: + +``` +{ + "source": { + // Source JSON content + }, + "target": { + "fruit": "Apple", + "size": "Large", + "color": "Red" + } +} +``` ## Features - [Import multiple JSON files](#Import-multiple-JSON-files) - [Single/ Multiple line comments](#Single-Multiple-line-comments) - ## Import multiple JSON files You can use `$import` keyword for importing @@ -36,4 +62,9 @@ You can use `$import` keyword for importing For single line comments, use `//` -For multi line comments, use `/*` to start, `*/` to end \ No newline at end of file +For multi line comments, use like below: +``` +// This is first line comment +// This is the second one + +``` \ No newline at end of file diff --git a/npm/src/cjson.ts b/npm/src/cjson.ts index 96e69ca1..7b187fcb 100644 --- a/npm/src/cjson.ts +++ b/npm/src/cjson.ts @@ -2,14 +2,16 @@ import { read } from "./utils/file"; import * as path from 'path'; import { Is } from "./utils/is"; import Keywords from "./utils/keywords"; -import { Json } from "./utils/json"; +import { Json, isContentJson } from "./utils/json"; + export class Cjson extends Is { private obj: JSON | undefined; private filePath: string; private content: string = ""; private commaSeparated: string[] = []; - private json: Json | undefined = undefined; + public json: Json | undefined = undefined; + public isContentJson = (isFilePath: boolean): boolean => { return isContentJson(this.content, isFilePath) }; constructor(filePath: string) { super(); diff --git a/npm/src/utils/json.ts b/npm/src/utils/json.ts index bd05dd8d..21b1dd99 100644 --- a/npm/src/utils/json.ts +++ b/npm/src/utils/json.ts @@ -5,7 +5,9 @@ import * as file from './file'; * @param content String type content * @returns `true` if content is JSON type */ -export function isContentJson(content: string) { +export function isContentJson(content: string, isFilePath?: boolean) { + if(isFilePath) + content = file.read(content); try { JSON.parse(content); return true; @@ -31,8 +33,8 @@ export class Json { private jsonValues: string[] = []; private filePath: string | undefined; - constructor(obj: any | string) { - if(typeof obj === "string") { + constructor(obj: any | string, isFilePath?: boolean) { + if(typeof obj === "string" && isFilePath) { this.filePath = obj; this.obj = JSON.parse(file.read(this.filePath)); } diff --git a/python/README.md b/python/README.md new file mode 100644 index 00000000..b0fc814f --- /dev/null +++ b/python/README.md @@ -0,0 +1 @@ +## Python repository for coded json files \ No newline at end of file diff --git a/python/cjson/src/coded-json.py b/python/cjson/src/coded-json.py new file mode 100644 index 00000000..e69de29b diff --git a/python/setup.py b/python/setup.py new file mode 100644 index 00000000..4ad4c8e6 --- /dev/null +++ b/python/setup.py @@ -0,0 +1,20 @@ +import setuptools + +setuptools.setup( + name="coded-json", + version="1.0.0", + author="Shubhendu Shekhar Gupta", + description="Coded JavaScript Object Notation", + long_description="", + author_email="subhendushekhargupta@gmail.com", + package_data=setuptools.find_packages(), + classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent" + ], + requires=">=3.10", + py_modules=["coded-json"], + package_dir={ '':'cjson/src' }, + include_dirs=[] +) \ No newline at end of file diff --git a/tests/npm/.gitignore b/tests/npm/.gitignore index ccb2c800..f4df4cb4 100644 --- a/tests/npm/.gitignore +++ b/tests/npm/.gitignore @@ -1,2 +1,3 @@ node_modules/ -package-lock.json \ No newline at end of file +package-lock.json +*.tgz \ No newline at end of file diff --git a/tests/npm/spec/tests/codedJsonTests.js b/tests/npm/spec/tests/codedJsonTests.js new file mode 100644 index 00000000..f83765e8 --- /dev/null +++ b/tests/npm/spec/tests/codedJsonTests.js @@ -0,0 +1,46 @@ +const Cjson = require('coded-json').Cjson; +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); + +const cjsonfilePath = path.join(__dirname, "..", "..", "..", "\\test-files\\target.cjson"); +const jsonfilePath = path.join(__dirname, "..", "..", "..", "\\test-files\\source.json"); +const pureJsonfilePath = path.join(__dirname, "..", "..", "..", "\\test-files\\pure.json"); + +/** + * Tests related to CJSON files + */ +describe("CJSON Test 1", () => { + + it("I should be able to import pure JSON files", () => { + var cjson = new Cjson(pureJsonfilePath); + var pureJSONContent = cjson.deserialize(); + + var jsonStringFromPure = JSON.parse(fs.readFileSync(pureJsonfilePath).toString()); + assert.equal(JSON.stringify(pureJSONContent), JSON.stringify(jsonStringFromPure)); + }); + + it("I should be able to deserialize comments from json files", () => { + var cjson = new Cjson(jsonfilePath); + cjson.deserialize(); + }); + + it("I should be able to deserialize imports and comments", () => { + var cjson = new Cjson(cjsonfilePath); + + var decodedJSON = cjson.deserialize(); + + assert.notEqual(decodedJSON, JSON.parse("{}")) + }); +}); + +/** + * Tests related to native JSON files + */ +describe("CJSON Test 2", () => { + + it("I should be able to use isContentJson()", () => { + var cjson = new Cjson(pureJsonfilePath); + assert.equal(cjson.isContentJson(), true); + }); +}); \ No newline at end of file diff --git a/tests/npm/spec/tests/spec.js b/tests/npm/spec/tests/spec.js deleted file mode 100644 index 19100129..00000000 --- a/tests/npm/spec/tests/spec.js +++ /dev/null @@ -1,13 +0,0 @@ -const Cjson = require('coded-json').Cjson; -const path = require('path'); - -const filePath = path.join(__dirname, "..", "..", "..", "\\test-files\\target.cjson"); - -describe("CJSON Test 1", () => { - it("I should be able to deserialize imports and comments", () => { - var cjson = new Cjson(filePath); - - var b = cjson.deserialize(); - console.log(b); - }); -}); \ No newline at end of file diff --git a/tests/test-files/pure.json b/tests/test-files/pure.json new file mode 100644 index 00000000..bc2b9a85 --- /dev/null +++ b/tests/test-files/pure.json @@ -0,0 +1,28 @@ +{ + "quiz": { + "sport": { + "q1": { + "question": "Which one is correct team name in NBA?", + "options": [ + "New York Bulls", + "Los Angeles Kings", + "Golden State Warriros", + "Huston Rocket" + ], + "answer": "Huston Rocket" + } + }, + "maths": { + "q1": { + "question": "5 + 7 = ?", + "options": [ + "10", + "11", + "12", + "13" + ], + "answer": "12" + } + } + } +} \ No newline at end of file