Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aqzi/jsonmapping
Convert json to and from (nested) maps and sets
https://github.com/aqzi/jsonmapping
Last synced: about 1 month ago
JSON representation
Convert json to and from (nested) maps and sets
- Host: GitHub
- URL: https://github.com/aqzi/jsonmapping
- Owner: aqzi
- License: mit
- Created: 2021-07-30T20:49:24.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-08-03T08:23:19.000Z (over 3 years ago)
- Last Synced: 2024-11-09T14:18:03.802Z (about 2 months ago)
- Language: TypeScript
- Size: 114 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JsonMapping
Convert json to and from (nested) maps and sets.## Installation
```
npm i @aqzi/jsonmapping
```## Usage
### Map/Set --> Json
The mapper function adds an identifier to the map and set objects.
When the parameter noIdentifier is true (default is false), it will convert the map to an object and the set will be a list.
(Json.stringify would transform a map to an empty object.)Map --> json:
```typescript
obj2Json({test: new Map(['😀', 1], ['👻', 2])})
@return: {"test":{"😀":1,"👻":2,"_map_":true}}//with noIdentifier = true:
obj2Json({test: new Map(['😀', 1], ['👻', 2])}, true)
@return: {"test":{"😀":1,"👻":2}}
```Set --> json:
```typescript
obj2Json({test: new Set([1,2,3])})
@return: {"test":{"_set_":[1,2,3]}}
```### Json --> Map/Set
The package provides two ways to reconstruct maps and sets correctly.
1) identifiers: If the json contains identifiers it will automaticly perform the transformation.
2) keysToConvert: You can specify some keys belonging to objects that should be converted.
if the same key is specified as value for Map and Set, than the corresponding object will be transformed
to a map. If the json contains multiple objects with the same key than all those objects will be converted
if that key was specified. Following code gives an example.```typescript
json2Obj(dataAsJson, {Map: ['🐔', '🐷'], Set: ['😺']})
```Object with keys 🐔 or 🐷 will be transformed to a map and an object with key 😺 will become a set.