├── .github
├── ISSUE_TEMPLATE
│ ├── bug.yml
│ ├── config.yml
│ └── feature.yml
└── workflows
│ ├── docs.yml
│ └── tests.yml
├── .gitignore
├── LICENSE
├── README.md
├── THIRD-PARTY-NOTICES.TXT
├── config.nims
├── deser.nimble
├── nim.cfg
├── nimdoc.cfg
├── src
├── deser.nim
└── deser
│ ├── base_error.nim
│ ├── des.nim
│ ├── des
│ ├── errors.nim
│ ├── helpers.nim
│ ├── impls.nim
│ └── make.nim
│ ├── errors.nim
│ ├── helpers.nim
│ ├── macroutils
│ ├── anycase.nim
│ ├── generation
│ │ ├── des.nim
│ │ ├── des
│ │ │ ├── keys.nim
│ │ │ ├── utils.nim
│ │ │ └── values.nim
│ │ ├── ser.nim
│ │ ├── ser
│ │ │ └── ser.nim
│ │ └── utils.nim
│ ├── matching.nim
│ ├── parsing
│ │ ├── field.nim
│ │ ├── pragmas.nim
│ │ └── struct.nim
│ └── types.nim
│ ├── pragmas.nim
│ ├── ser.nim
│ ├── ser
│ ├── helpers.nim
│ ├── impls.nim
│ └── make.nim
│ ├── test.nim
│ └── test
│ ├── des.nim
│ ├── ser.nim
│ └── token.nim
└── tests
├── config.nims
├── des
├── config.nims
├── timpls.nim
└── tmake.nim
├── macroutils
├── config.nims
├── tparsepragma.nim
├── tparsestruct.nim
└── ttypes.nim
├── ser
├── config.nims
├── timpls.nim
└── tmake.nim
└── thelpers.nim
/.github/ISSUE_TEMPLATE/bug.yml:
--------------------------------------------------------------------------------
1 | name: Bug report
2 | description: Report issues affecting the library or the documentation.
3 | labels:
4 | - bug
5 | body:
6 | - type: checkboxes
7 | attributes:
8 | label: Checklist
9 | options:
10 | - label: I am sure the error is coming from deser code
11 | required: true
12 | - label: I have searched in the issue tracker for similar bug reports, including closed ones
13 | required: true
14 |
15 | - type: markdown
16 | attributes:
17 | value: |
18 | ## Context
19 |
20 | Please provide as much information as possible. This will help us to reproduce the issue and fix it.
21 |
22 | - type: textarea
23 | attributes:
24 | label: Nim version.
25 | description: Copy and paste the output of `nim -v` on the command line.
26 | render: sh
27 | validations:
28 | required: true
29 |
30 | - type: input
31 | attributes:
32 | label: deser version
33 | placeholder: E.g. 0.3.0
34 | validations:
35 | required: true
36 |
37 | - type: textarea
38 | attributes:
39 | label: Current behavior
40 | description: Please describe the behavior you are currently experiencing.
41 | validations:
42 | required: true
43 |
44 | - type: textarea
45 | attributes:
46 | label: Expected behavior
47 | description: Please describe the behavior you are expecting.
48 | validations:
49 | required: true
50 |
51 | - type: textarea
52 | attributes:
53 | label: Steps to reproduce
54 | description: Please describe the steps you took to reproduce the behavior.
55 | placeholder: |
56 | 1. step 1
57 | 2. step 2
58 | 3. ...
59 | 4. you get it...
60 | validations:
61 | required: true
62 |
63 | - type: textarea
64 | attributes:
65 | label: Code example
66 | description: Provide a [minimal, reproducible](https://stackoverflow.com/help/minimal-reproducible-example) and properly formatted example (if applicable).
67 | placeholder: |
68 | import deser
69 | ...
70 | makeDeserializable(Foo)
71 | render: nim
72 |
73 | - type: textarea
74 | attributes:
75 | label: Logs
76 | description: Provide the complete traceback (if applicable) or other kind of logs.
77 | render: sh
78 |
79 | - type: textarea
80 | attributes:
81 | label: Debug dump
82 | description: If you think the bug is related to the `makeSerializable` and/or `makeDeserializable` macros, please compile with the `-d:debugMakeSerializable` and/or `-d:debugMakeDeserializable` flag and paste the output.
83 | render: nim
84 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/config.yml:
--------------------------------------------------------------------------------
1 | blank_issues_enabled: true
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature.yml:
--------------------------------------------------------------------------------
1 | name: Feature request
2 | description: Report features you would like to see or improve in the library.
3 | labels:
4 | - enhancement
5 | body:
6 | - type: textarea
7 | attributes:
8 | label: Problem
9 | description: Is your feature request related to a specific problem? If not, please describe the general idea of your request.
10 | validations:
11 | required: true
12 |
13 | - type: textarea
14 | attributes:
15 | label: Possible solution
16 | description: Describe the solution you would like to see in the library.
17 | validations:
18 | required: true
19 |
20 | - type: textarea
21 | attributes:
22 | label: References
23 | description: If you have seen a similar feature in another library/framework/lanugage.
24 |
25 | - type: textarea
26 | attributes:
27 | label: Code example
28 | description: A small code example that demonstrates the behavior you would like to see.
29 | render: nim
30 |
31 | - type: textarea
32 | attributes:
33 | label: Additional information
34 | description: Any additional information you would like to provide.
--------------------------------------------------------------------------------
/.github/workflows/docs.yml:
--------------------------------------------------------------------------------
1 | name: Build docs
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 | path-ignore:
8 | - 'README.md'
9 | release:
10 | types: [published]
11 |
12 | jobs:
13 | build:
14 | runs-on: ubuntu-latest
15 | steps:
16 | - uses: actions/checkout@v2
17 |
18 | - name: Set up Nim
19 | uses: jiro4989/setup-nim-action@v1
20 | with:
21 | nim-version: 1.6.12
22 |
23 | - name: Build docs
24 | run: nimble docs
25 |
26 | - name: Deploy docs
27 | uses: peaceiris/actions-gh-pages@v3
28 | with:
29 | github_token: ${{ secrets.GITHUB_TOKEN }}
30 | publish_dir: ./docs
31 | keep_files: true
32 |
--------------------------------------------------------------------------------
/.github/workflows/tests.yml:
--------------------------------------------------------------------------------
1 | name: Tests
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 | path-ignore:
8 | - 'README.md'
9 | pull_request:
10 | types: [opened, synchronize, reopened, ready_for_review]
11 | branches:
12 | - master
13 | path-ignore:
14 | - 'README.md'
15 |
16 | jobs:
17 | build:
18 | if: "github.event.pull_request.draft == false && ! contains(toJSON(github.event.commits.*.message), '[skip-ci]')"
19 | strategy:
20 | matrix:
21 | nim: ['1.6.12', 'devel']
22 | os: [ubuntu-latest]
23 | runs-on: ${{ matrix.os }}
24 | steps:
25 | - uses: actions/checkout@v2
26 |
27 | - name: Cache nimble
28 | id: cache-nimble
29 | uses: actions/cache@v1
30 | with:
31 | path: ~/.nimble
32 | key: ${{ runner.os }}-nimble-${{ hashFiles('*.nimble') }}
33 | if: runner.os != 'Windows'
34 |
35 | - name: Set up Nim
36 | uses: jiro4989/setup-nim-action@v1
37 | with:
38 | nim-version: ${{ matrix.nim }}
39 |
40 | - name: Install dependencies
41 | run: nimble install -d -y
42 |
43 | - name: Run tests
44 | run: nimble test
45 |
46 | - name: Try to build docs
47 | run: nimble docs
48 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | nimcache/
2 | nimblecache/
3 | htmldocs/
4 | testresults/
5 | docs/
6 |
7 | *.o
8 | !/icons/*.o
9 | *.obj
10 | *.ilk
11 | *.exp
12 | *.pdb
13 | *.lib
14 | *.dll
15 | *.exe
16 | *.so
17 | *.dylib
18 | *.zip
19 | *.iss
20 | *.log
21 | *.pdb
22 |
23 | oc/html/
24 | doc/*.html
25 | doc/*.pdf
26 | doc/*.idx
27 | /web/upload
28 | /build/*
29 | bin/*
30 |
31 | /*.html
32 | outputGotten.txt
33 | tests/megatest.nim
34 | outputExpected.txt
35 |
36 | .idea/
37 | tests/megatest
38 | tests/des/timpls
39 | tests/ser/timpls
40 | tests/ser/tmake
41 | tests/des/tmake
42 | src/deser/macroutils/generation/ser/ser
43 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Nikita Gabbasov
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Deser [![nim-version-img]][nim-version]
2 |
3 | [nim-version]: https://nim-lang.org/blog/2021/10/19/version-160-released.html
4 | [nim-version-img]: https://img.shields.io/badge/Nim_-v1.6.0%2B-blue
5 |
6 | **Serde-like de/serialization library for Nim.**
7 |
8 | `nimble install deser`
9 |
10 | [Documentation](https://deser.nim.town)
11 |
12 | ---
13 |
14 |
15 | Motivation
16 | Many serializers have already been written for Nim. You can probably find at least two serializers for each format.
17 |
18 | The problem is that each library's API and customization options are different. I can't declare an object with renamed or skipped fields once and change the data format with one line.
19 |
20 | Attempts to generalize the serializer were also made. However, I found only one library that is actively under development - [nim-serialization](https://github.com/status-im/nim-serialization). When installing the library downloaded a quarter of all libraries for Nim, so I did not try it.
21 |
22 | Thus, there was no library for Nim that standardized the serialization process, so I wrote **deser**.
23 |
24 | Also read:
25 | - [Standards](https://xkcd.com/927/)
26 | - [Not invented here](https://en.wikipedia.org/wiki/Not_invented_here)
27 |
28 |
29 |
30 | ## Supported formats
31 | - JSON - [deser_json](https://github.com/gabbhack/deser_json)
32 |
33 | Also read:
34 | - [How to make bindings](https://deser.nim.town/deser.html#how-to-make-bindings)
35 |
36 |
37 | ## Example
38 | ```nim
39 | import std/[
40 | options,
41 | times
42 | ]
43 |
44 | import
45 | deser,
46 | deser_json
47 |
48 | proc fromTimestamp(deserializer: var auto): Time =
49 | fromUnix(deserialize(int64, deserializer))
50 |
51 | proc toTimestamp(self: Time, serializer: var auto) =
52 | serializer.serializeInt64(self.toUnix())
53 |
54 | type
55 | ChatType = enum
56 | Private = "private"
57 | Group = "group"
58 |
59 | Chat {.renameAll(SnakeCase).} = object
60 | id: int64
61 | username {.skipSerializeIf(isNone).}: Option[string]
62 | created {.serializeWith(toTimestamp), deserializeWith(fromTimestamp).}: Time
63 |
64 | case kind {.renamed("type").}: ChatType
65 | of Private:
66 | firstName: string
67 | lastName {.skipSerializeIf(isNone).}: Option[string]
68 | bio {.skipSerializeIf(isNone).}: Option[string]
69 | of Group:
70 | title: string
71 |
72 | # Use public to export deserialize or serialize procedures
73 | # false by default
74 | makeSerializable(Chat, public=true)
75 | makeDeserializable(Chat, public=true)
76 |
77 | const
78 | json = """
79 | {
80 | "id": 123,
81 | "username": "gabbhack",
82 | "created": 1234567890,
83 | "type": "private",
84 | "first_name": "Gabben"
85 | }
86 | """
87 | chat = Chat(
88 | id: 123,
89 | username: some "gabbhack",
90 | created: fromUnix(1234567890),
91 | kind: Private,
92 | firstName: "Gabben"
93 | )
94 |
95 | echo Chat.fromJson(json)
96 | echo chat.toJson()
97 | ```
98 |
99 | Also read:
100 | - [Customize serialization process](https://deser.nim.town/deser.html#customize-serialization-process)
101 |
102 | ## License
103 | Licensed under MIT license.
104 |
105 | Deser uses third-party libraries or other resources that may be
106 | distributed under licenses different than the deser.
107 |
108 | THIRD-PARTY-NOTICES.TXT
109 |
110 |
111 | ## Acknowledgements
112 | - [serde.rs](https://serde.rs), for all the ideas I stole
113 | - [fusion/matching](https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.nim), for making it easier to work with object variants
114 | - [anycase](https://github.com/epszaw/anycase)
--------------------------------------------------------------------------------
/THIRD-PARTY-NOTICES.TXT:
--------------------------------------------------------------------------------
1 | deser uses third-party libraries or other resources that may be
2 | distributed under licenses different than the deser
3 |
4 | The attached notices are provided for information only.
5 |
6 | License notice for fusion/matching
7 | -------------------------------
8 | MIT License
9 |
10 | Copyright (c) 2020 Andreas Rumpf and contributors
11 |
12 | Permission is hereby granted, free of charge, to any person obtaining a copy
13 | of this software and associated documentation files (the "Software"), to deal
14 | in the Software without restriction, including without limitation the rights
15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 | copies of the Software, and to permit persons to whom the Software is
17 | furnished to do so, subject to the following conditions:
18 |
19 | The above copyright notice and this permission notice shall be included in all
20 | copies or substantial portions of the Software.
21 |
22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 | SOFTWARE.
29 | -------------------------------
30 |
31 | License notice for anycase-fork
32 | The MIT License (MIT)
33 |
34 | Copyright 2019 Konstantin Epishev
35 | Copyright (c) 2020 Nikita "gabbhack" Gabbasov
36 |
37 | Permission is hereby granted, free of charge, to any person obtaining a copy of
38 | this software and associated documentation files (the "Software"), to deal in
39 | the Software without restriction, including without limitation the rights to
40 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
41 | the Software, and to permit persons to whom the Software is furnished to do so,
42 | subject to the following conditions:
43 |
44 | The above copyright notice and this permission notice shall be included in all
45 | copies or substantial portions of the Software.
46 |
47 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
48 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
49 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
50 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
51 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/config.nims:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gabbhack/deser/efa0b92765fa84130aadb62344aec4e703f13805/config.nims
--------------------------------------------------------------------------------
/deser.nimble:
--------------------------------------------------------------------------------
1 | # Package
2 |
3 | version = "0.3.2"
4 | author = "gabbhack"
5 | description = "De/serialization library for Nim"
6 | license = "MIT"
7 | srcDir = "src"
8 |
9 |
10 | # Dependencies
11 |
12 | requires "nim >= 1.6.0"
13 |
14 | # Tasks
15 | import std/[os, strformat]
16 |
17 | task test, "Run tests":
18 | exec "testament all"
19 |
20 | task docs, "Generate docs":
21 | rmDir "docs"
22 | exec "nimble doc2 --outdir:docs --project --git.url:https://github.com/gabbhack/deser --git.commit:master --index:on src/deser"
23 |
--------------------------------------------------------------------------------
/nim.cfg:
--------------------------------------------------------------------------------
1 | path="$config/src"
--------------------------------------------------------------------------------
/nimdoc.cfg:
--------------------------------------------------------------------------------
1 | doc.body_toc_group = """
2 |