json-duplicate-keys 2024.11.19__py3-none-any.whl → 2024.11.28__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  try:
2
3
  unicode # Python 2
3
4
  except NameError:
@@ -150,9 +151,8 @@ def loads(Jstr, dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isD
150
151
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
151
152
  def load(Jfilepath, dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isDebug_=False):
152
153
  try:
153
- Jfile = open(Jfilepath)
154
- Jstr = Jfile.read()
155
- Jfile.close()
154
+ with open(Jfilepath) as Jfile:
155
+ Jstr = Jfile.read()
156
156
 
157
157
  return loads(Jstr, dupSign_start=dupSign_start, dupSign_end=dupSign_end, ordered_dict=ordered_dict, _isDebug_=_isDebug_)
158
158
  except Exception as e:
@@ -307,16 +307,20 @@ class JSON_DUPLICATE_KEYS:
307
307
  else:
308
308
  if self.get(separator.join(name.split(separator)[:-1]), case_insensitive=case_insensitive, separator=separator, parse_index=parse_index)["value"] != "JSON_DUPLICATE_KEYS_ERROR":
309
309
  Jget = self.get(separator.join(name.split(separator)[:-1]), case_insensitive=case_insensitive, separator=separator, parse_index=parse_index)
310
- exec_expression = "self.getObject()"
310
+ if type(Jget["value"]) in [dict, OrderedDict]:
311
+ exec_expression = "self.getObject()"
311
312
 
312
- for k in Jget["name"].split(separator):
313
- if re.search("^"+re.escape(parse_index)+"\\d+"+re.escape(parse_index)+"$", k):
314
- exec_expression += "["+k.split(parse_index)[1]+"]"
315
- else:
316
- exec_expression += "["+repr(k)+"]"
313
+ for k in Jget["name"].split(separator)+[name.split(separator)[-1]]:
314
+ if re.search("^"+re.escape(parse_index)+"\\d+"+re.escape(parse_index)+"$", k):
315
+ exec_expression += "["+k.split(parse_index)[1]+"]"
316
+ else:
317
+ exec_expression += "["+repr(k)+"]"
317
318
 
318
- exec(exec_expression+"="+repr(value))
319
- return True
319
+ exec(exec_expression+"="+repr(value))
320
+ return True
321
+ else:
322
+ if _isDebug_: print("\x1b[31m[-] KeyNameInvalidError: \x1b[0m"+name)
323
+ return False
320
324
  else:
321
325
  if _isDebug_: print("\x1b[31m[-] KeyNameNotExistError: {}\x1b[0m".format(separator.join(Jget["name"].split(separator)[:-1])))
322
326
  return False
@@ -328,7 +332,7 @@ class JSON_DUPLICATE_KEYS:
328
332
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
329
333
  # # # # # # # # # # # # # # update # # # # # # # # # # # # # #
330
334
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
331
- def update(self, name, value, case_insensitive=False, separator="||", parse_index="$", _isDebug_=False):
335
+ def update(self, name, value, case_insensitive=False, allow_new_key=False, separator="||", parse_index="$", dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isDebug_=False):
332
336
  import re
333
337
 
334
338
  # User input data type validation
@@ -344,7 +348,10 @@ class JSON_DUPLICATE_KEYS:
344
348
 
345
349
  if type(parse_index) not in [str, unicode]: parse_index = "$"
346
350
 
347
- if self.get(name, case_insensitive=case_insensitive, separator=separator, parse_index=parse_index, _isDebug_=_isDebug_)["value"] != "JSON_DUPLICATE_KEYS_ERROR":
351
+ _debug_ = _isDebug_
352
+ if allow_new_key: _debug_ = False
353
+
354
+ if self.get(name, case_insensitive=case_insensitive, separator=separator, parse_index=parse_index, _isDebug_=_debug_)["value"] != "JSON_DUPLICATE_KEYS_ERROR":
348
355
  Jname = self.get(name, case_insensitive=case_insensitive, separator=separator, parse_index=parse_index)["name"]
349
356
  try:
350
357
  exec_expression = "self.getObject()"
@@ -359,6 +366,8 @@ class JSON_DUPLICATE_KEYS:
359
366
  return True
360
367
  except Exception as e:
361
368
  if _isDebug_: print("\x1b[31m[-] ExceptionError: {}\x1b[0m".format(e))
369
+ elif allow_new_key:
370
+ return self.set(name, value, case_insensitive=case_insensitive, separator=separator, parse_index=parse_index, dupSign_start=dupSign_start, dupSign_end=dupSign_end, ordered_dict=ordered_dict, _isDebug_=_isDebug_)
362
371
 
363
372
  return False
364
373
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@@ -420,10 +429,10 @@ class JSON_DUPLICATE_KEYS:
420
429
  for k, v in JDKSObject.getObject().items():
421
430
  if type(k) == str and type(name) == str:
422
431
  if re.search(name, k):
423
- newJDKSObject.set(k, v, separator="!!"+separator+"!!", parse_index="!!"+parse_index+"!!", ordered_dict=ordered_dict)
432
+ newJDKSObject.set(k, v, separator="§§"+separator+"§§", parse_index="§§"+parse_index+"§§", ordered_dict=ordered_dict)
424
433
  else:
425
434
  if name == k:
426
- newJDKSObject.set(k, v, separator="!!"+separator+"!!", parse_index="!!"+parse_index+"!!", ordered_dict=ordered_dict)
435
+ newJDKSObject.set(k, v, separator="§§"+separator+"§§", parse_index="§§"+parse_index+"§§", ordered_dict=ordered_dict)
427
436
 
428
437
  return newJDKSObject
429
438
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@@ -444,10 +453,10 @@ class JSON_DUPLICATE_KEYS:
444
453
  for k, v in JDKSObject.getObject().items():
445
454
  if type(v) == str and type(value) == str:
446
455
  if re.search(value, v):
447
- newJDKSObject.set(k, v, separator="!!"+separator+"!!", parse_index="!!"+parse_index+"!!", ordered_dict=ordered_dict)
456
+ newJDKSObject.set(k, v, separator="§§"+separator+"§§", parse_index="§§"+parse_index+"§§", ordered_dict=ordered_dict)
448
457
  else:
449
458
  if value == v:
450
- newJDKSObject.set(k, v, separator="!!"+separator+"!!", parse_index="!!"+parse_index+"!!", ordered_dict=ordered_dict)
459
+ newJDKSObject.set(k, v, separator="§§"+separator+"§§", parse_index="§§"+parse_index+"§§", ordered_dict=ordered_dict)
451
460
 
452
461
  return newJDKSObject
453
462
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json-duplicate-keys
3
- Version: 2024.11.19
3
+ Version: 2024.11.28
4
4
  Summary: Flatten/ Unflatten and Load(s)/ Dump(s) JSON File/ Object with Duplicate Keys
5
5
  Home-page: https://github.com/truocphan/json-duplicate-keys
6
6
  Author: TP Cyber Security
@@ -197,13 +197,17 @@ print(JDKSObject.getObject())
197
197
  ```
198
198
  ---
199
199
 
200
- ### JSON_DUPLICATE_KEYS.update(`name`, `value`, `case_insensitive`=False, `separator`="||", `parse_index`="$", `_isDebug_`=False)
201
- _Update new `value` for existing `name` in the JSON object_
200
+ ### JSON_DUPLICATE_KEYS.update(`name`, `value`, `case_insensitive`=False, `allow_new_key`=False, `separator`="||", `parse_index`="$", `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=False)
201
+ _Update new `value` for existing `name` or Set a new `name` in the JSON object_
202
202
  - `name`: the key name of the JSON object. Supported flatten key name format
203
203
  - `value`: new value for key `name`
204
204
  - `case_insensitive`: the key name case (in)sensitive
205
+ - `allow_new_key`: allows to create a new key name if the key name does not exist
205
206
  - `separator`:
206
207
  - `parse_index`:
208
+ - `dupSign_start`:
209
+ - `dupSign_end`:
210
+ - `ordered_dict`: preserves the order in which the Keys are inserted
207
211
  - `_isDebug_`: Show/ Hide debug error messages
208
212
  ```python
209
213
  import json_duplicate_keys as jdks
@@ -254,7 +258,7 @@ print(JDKSObject.getObject())
254
258
  - `name`:
255
259
  - `separator`:
256
260
  - `parse_index`:
257
- - `ordered_dict`:
261
+ - `ordered_dict`: preserves the order in which the Keys are inserted
258
262
  ```python
259
263
  import json_duplicate_keys as jdks
260
264
 
@@ -275,7 +279,7 @@ print(JDKSObject.dumps())
275
279
  - `value`:
276
280
  - `separator`:
277
281
  - `parse_index`:
278
- - `ordered_dict`:
282
+ - `ordered_dict`: preserves the order in which the Keys are inserted
279
283
  ```python
280
284
  import json_duplicate_keys as jdks
281
285
 
@@ -393,6 +397,9 @@ print(JDKSObject.getObject())
393
397
  ---
394
398
 
395
399
  ## CHANGELOG
400
+ #### [json-duplicate-keys v2024.11.28](https://github.com/truocphan/json-duplicate-keys/tree/2024.11.28)
401
+ - **Fixed**: Add subkey name to empty dict of existing key name
402
+
396
403
  #### [json-duplicate-keys v2024.11.19](https://github.com/truocphan/json-duplicate-keys/tree/2024.11.19)
397
404
  - **Updated**: Allows getting (`JSON_DUPLICATE_KEYS.get`), setting (`JSON_DUPLICATE_KEYS.set`), updating (`JSON_DUPLICATE_KEYS.update`), deleting (`JSON_DUPLICATE_KEYS.delete`) JSON_DUPLICATE_KEYS objects with case-insensitive key names
398
405
 
@@ -0,0 +1,6 @@
1
+ json_duplicate_keys/__init__.py,sha256=YV3PpNeXAynBoY46yTs_M3t3KivpXvOLdeMD__IyviM,27170
2
+ json_duplicate_keys-2024.11.28.dist-info/LICENSE,sha256=_hudSGMciUc27wGR8EMKfeb3atJRlf6rbhJtLnel-nc,1093
3
+ json_duplicate_keys-2024.11.28.dist-info/METADATA,sha256=zIHJT2JFMlMJHG7sBu-wk_O-PM6NvMwK6rweJKQUhuM,18661
4
+ json_duplicate_keys-2024.11.28.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
5
+ json_duplicate_keys-2024.11.28.dist-info/top_level.txt,sha256=vdsGrPLVS_l-BFL1i4hCJBY5_-gq4Cwp-11yegA750Y,20
6
+ json_duplicate_keys-2024.11.28.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.41.1)
2
+ Generator: setuptools (75.6.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,6 +0,0 @@
1
- json_duplicate_keys/__init__.py,sha256=K9bg6uSaRnP4JNorn0Y_Chw_ZnRIZoIU0RGxqHFk8-s,26545
2
- json_duplicate_keys-2024.11.19.dist-info/LICENSE,sha256=_hudSGMciUc27wGR8EMKfeb3atJRlf6rbhJtLnel-nc,1093
3
- json_duplicate_keys-2024.11.19.dist-info/METADATA,sha256=yAQnSyE3h__xXLTnBIbtQfLBEgeCC7eoZSFtG4vdVGE,18084
4
- json_duplicate_keys-2024.11.19.dist-info/WHEEL,sha256=5sUXSg9e4bi7lTLOHcm6QEYwO5TIF1TNbTSVFVjcJcc,92
5
- json_duplicate_keys-2024.11.19.dist-info/top_level.txt,sha256=vdsGrPLVS_l-BFL1i4hCJBY5_-gq4Cwp-11yegA750Y,20
6
- json_duplicate_keys-2024.11.19.dist-info/RECORD,,