Skip to content

Commit

Permalink
closes node #15 #16 python #11 (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Gupta <shubhendushekhar.gupta@delta.com>
  • Loading branch information
SubhenduShekhar and Gupta authored Jun 5, 2023
1 parent 05baa59 commit de4caee
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 30 deletions.
43 changes: 38 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,48 @@
<h4> Coded Javascript Object Notation </h4>
</center>

Why static JSON if you can utilize CJSON

<br>

![NPM Package](https://github.com/SubhenduShekhar/cjson/actions/workflows/npm-publish.yml/badge.svg)

Why static JSON if you can utilize CJSON.

<b> Create your files with extension `.cjson` to unlock all these features </b>
<br/>

<br>
# 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
Expand All @@ -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
For multi line comments, use like below:
```
// This is first line comment
// This is the second one
```
3 changes: 2 additions & 1 deletion npm/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
out/
package-lock.json
package-lock.json
*.tgz
41 changes: 36 additions & 5 deletions npm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,46 @@
<h4> Coded Javascript Object Notation </h4>
</center>

Why static JSON if you can utilize CJSON

<br>

![NPM Package](https://github.com/SubhenduShekhar/cjson/actions/workflows/npm-publish.yml/badge.svg)

Why static JSON if you can utilize CJSON.

<b> Create your files with extension `.cjson` to unlock all these features </b>
<br/>

<br>
## 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
Expand All @@ -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
For multi line comments, use like below:
```
// This is first line comment
// This is the second one
```
6 changes: 4 additions & 2 deletions npm/src/cjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 5 additions & 3 deletions npm/src/utils/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
Expand Down
1 change: 1 addition & 0 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Python repository for coded json files
Empty file added python/cjson/src/coded-json.py
Empty file.
20 changes: 20 additions & 0 deletions python/setup.py
Original file line number Diff line number Diff line change
@@ -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=[]
)
3 changes: 2 additions & 1 deletion tests/npm/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
package-lock.json
package-lock.json
*.tgz
46 changes: 46 additions & 0 deletions tests/npm/spec/tests/codedJsonTests.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
13 changes: 0 additions & 13 deletions tests/npm/spec/tests/spec.js

This file was deleted.

28 changes: 28 additions & 0 deletions tests/test-files/pure.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}

0 comments on commit de4caee

Please # to comment.