Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
362 changes: 248 additions & 114 deletions JsonPreprocessor/CJsonPreprocessor.py

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion JsonPreprocessor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from .CJsonPreprocessor import CJsonPreprocessor
from .CJsonPreprocessor import CJsonPreprocessor
from .CJsonPreprocessor import CSyntaxType

2 changes: 1 addition & 1 deletion config/CConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(self, sReferencePath="."):

# 1. basic setup stuff
self.__dictConfig['sPackageName'] = "JsonPreprocessor"
self.__dictConfig['sVersion'] = "0.1.0"
self.__dictConfig['sVersion'] = "0.2.0"
self.__dictConfig['sAuthor'] = "Mai Dinh Nam Son"
self.__dictConfig['sAuthorEMail'] = "son.maidinhnam@vn.bosch.com"
self.__dictConfig['sDescription'] = "This package provides a preprocessor for json files"
Expand Down
37 changes: 37 additions & 0 deletions sample/001_sample_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2020-2022 Robert Bosch Car Multimedia GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#################################################################################

## This example also shows how to use comments
## in json files.

import sys

sys.path.append('D:\B\python-jsonpreprocessor')

from JsonPreprocessor import CJsonPreprocessor

from pprint import pprint

prepro=CJsonPreprocessor()

# you can load the base json file with
# - relative path to your python program
# - absolute path
# - paths containting environment variables by means of
# %envvariable% syntax
data=prepro.jsonLoad(".\json\json_with_comment.json")

pprint(data)

37 changes: 37 additions & 0 deletions sample/003_sample_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2020-2022 Robert Bosch Car Multimedia GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#################################################################################

## This example also shows how to import json files
## into json files.

import sys

sys.path.append('D:\B\python-jsonpreprocessor')

from JsonPreprocessor import CJsonPreprocessor

from pprint import pprint

prepro=CJsonPreprocessor()

# you can load the base json file with
# - relative path to your python program
# - absolute path
# - paths containting environment variables by means of
# %envvariable% syntax
data=prepro.jsonLoad(".\json\json_with_import.json")

pprint(data)

40 changes: 40 additions & 0 deletions sample/005_sample_python_syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2020-2022 Robert Bosch Car Multimedia GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#################################################################################

## This example also shows how to use python-syntax
## as part of json files.

import sys

sys.path.append('D:\B\python-jsonpreprocessor')

from JsonPreprocessor import CJsonPreprocessor
from JsonPreprocessor import CSyntaxType

from pprint import pprint

# CSyntaxType.python activates recognition of python
# syntax as part of json files
prepro=CJsonPreprocessor(syntax=CSyntaxType.python)

# you can load the base json file with
# - relative path to your python program
# - absolute path
# - paths containting environment variables by means of
# %envvariable% syntax
data=prepro.jsonLoad(".\json\json_with_python_syntax.json")

pprint(data)

37 changes: 37 additions & 0 deletions sample/007_sample_use_of_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2020-2022 Robert Bosch Car Multimedia GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#################################################################################

## This example also shows how to use comments
## in json files.

import sys

sys.path.append('D:\B\python-jsonpreprocessor')

from JsonPreprocessor import CJsonPreprocessor

from pprint import pprint

prepro=CJsonPreprocessor()

# you can load the base json file with
# - relative path to your python program
# - absolute path
# - paths containting environment variables by means of
# %envvariable% syntax
data=prepro.jsonLoad(".\json\json_with_variables.json")

pprint(data)

48 changes: 48 additions & 0 deletions sample/json/json_with_comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*Copyright 2020-2022 Robert Bosch Car Multimedia GmbH

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

// You can write anywhere one line comments

/*
This way you can
write anywhere multiline
comments
*/

{
/*
This way you can
write anywhere multiline
comments
*/

// You can write anywhere one line comments
"myVar1" : "val1",

"myVar2" : "http://www.google.de",
"myVar3" : "val3", // You can write anywhere one line comments
"abc" : // You can write anywhere one line comments
{
/*
This way you can
write anywhere multiline
comments
*/
"myVar4" : "val4"
},
"def" :
{
"myVar5" : "val5" // You can write anywhere one line comments
}
}
52 changes: 52 additions & 0 deletions sample/json/json_with_import.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*Copyright 2020-2022 Robert Bosch Car Multimedia GmbH

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

/*
imports of json files are done by means of the
[import] keyword.

All imported data will be replace the node which
has the [import] keyword.

[import] can be done at any location in your json
data structure. This allows to structure big json files
into any architecture you require.
*/
{
"myVar1" : "val1",

"myVar2" : "http://www.google.de",
"abc" :
{
/*
import of json files should be always a relative path
to the current file. This allows later to shift whole
json directories to other places without braking the
internal strucutre.

All imported data will be located in this case below
node "abc" and replace the [import] node.
*/
"[import]" : "./second_file.json"
},
"def" :
{
//All imported data will be located in this case below
//node "def" and replace the [import] node.
//this file is imported in this example two times as
//re-usable data structure.
//one time here, a second time in "second_file.json"
"[import]" : "./subfolder/third_file.json"
}
}
37 changes: 37 additions & 0 deletions sample/json/json_with_python_syntax.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*Copyright 2020-2022 Robert Bosch Car Multimedia GmbH

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
{
//instead of `true` you can use the python-syntax `True`
"myVar1" : True,
"myVar1_" : true,

//instead of `frue` you can use the python-syntax `False`
"myVar2" : False,
"myVar2_" : false,

//instead of `null` you can use the python-syntax `None`
"myVar3" : None,
"myVar3_" : null,

"abc" :
{
//this is a string -> there is no change
"myVar4" : "True"
},
"def" :
{
//this is a string -> there is no change
"myVar5" : "False"
}
}
45 changes: 45 additions & 0 deletions sample/json/json_with_variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*Copyright 2020-2022 Robert Bosch Car Multimedia GmbH

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

/*
every node can be used by means of ${node} syntax
as variable once it was defined.
This works also with [import] of json data. As per
order the [import ] appears in the code the nodes
can be referenced by means of ${syntax} as variable.
*/

{
// create some initial data
"myVar1" : "val1",
"myVar2" : "val2",
"abc" : {
"arList" : ["a","b","c"]
},

//give myVar3 the value of "myVar1"
"myVar3" : "${myVar1}",

//give myVar4 the value of ${abc}['arList'][1]
"myVar4" : "${abc}['arList'][1]",

//give myVar4 the value of ${abc}['arList']
"myVar5" : "${abc}['arList']",

//add a new entry to node "abc"
"${abc}['new_entry']" : "new_entry",

//give myVar6 the value new value of node "abc"
"myVar6" : "${abc}"
}
23 changes: 23 additions & 0 deletions sample/json/second_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*Copyright 2020-2022 Robert Bosch Car Multimedia GmbH

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
{
"second_myVar1" : true,

//All imported data will be located in this case below
//the root node of this file.
//The import is done again relative to this file.
"[import]" : "./subfolder/third_file.json",

"second_myVar2" : "http://www.ebay.de"
}
Loading