Skip to content

Commit

Permalink
remove concept, fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed Dec 30, 2020
1 parent 557b6fa commit efc7ffa
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
21 changes: 13 additions & 8 deletions jason.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import std/strutils
type
Json* = distinct string ## Serialized JSON.

JasonObject = concept j
for v in fields(j):
v is Jasonable

JasonArray = concept j
for v in j:
v is Jasonable
Expand Down Expand Up @@ -300,6 +296,8 @@ macro jason*(a: JasonArray): Json =
runnableExamples:
let j = jason @[1, 3, 5, 7]
assert $j == "[1,3,5,7]"
let k = jason (1, 3, 5, 7)
assert $k == "[1,3,5,7]"

case a.kind
of nnkTupleConstr:
Expand Down Expand Up @@ -347,9 +345,9 @@ func jason*(o: ref): Json =
else:
result = jason o[]

macro jason*(o: JasonObject): Json =
## Render an anonymous Nim tuple as a JSON array; objects and named
## tuples become JSON objects.
macro jason*(o: tuple): Json =
## Render an anonymous Nim tuple as a JSON array;
## named tuples become JSON objects.
runnableExamples:
let j = jason (1, "too", 3.0)
assert $j == """[1,"too",3.0]"""
Expand All @@ -360,7 +358,14 @@ macro jason*(o: JasonObject): Json =
of nnkTupleConstr:
result = jasonTuple o
else:
# use our object construction code for named tuples, objects
# use our object construction code for named tuples
result = jasonCurly o

macro jason*(o: object): Json =
## Render an object as JSON.
runnableExamples:
let j = jason Exception(msg: "bummer")
assert $j == """{"parent":null,"name":"Exception","msg":"bummer","trace":[],"up":null}"""
result = jasonCurly o

export jason
2 changes: 1 addition & 1 deletion jason.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "compile-time json"
license = "MIT"

requires "nim >= 1.4.0 & < 2.0.0"
requires "https://github.com/disruptek/testes >= 0.7.3 & < 1.0.0"
requires "https://github.com/disruptek/testes >= 0.7.12 & < 1.0.0"
requires "https://github.com/disruptek/criterion < 1.0.0"

task test, "run tests for ci":
Expand Down
2 changes: 1 addition & 1 deletion testes
Submodule testes updated 10 files
+1 βˆ’1 .github/workflows/ci.yml
+45 βˆ’1 README.md
+26 βˆ’22 docs/clean.svg
+26 βˆ’22 docs/demo.svg
+17 βˆ’0 examples/balls.nim
+361 βˆ’92 testes.nim
+10 βˆ’25 testes.nimble
+0 βˆ’1 tests/config.nims
+0 βˆ’6 tests/nim.cfg
+10 βˆ’1 tests/testicles.nim
2 changes: 1 addition & 1 deletion tests/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ testes:
test "tuple of tuples of tuples":
# from sealmove:
let x = (((1, 2),(3, 4)), ((5, 6),(7, 8)))
check $jason(x) == "[[[1,2],[3,4]][[5,6],[7,8]]]"
check $jason(x) == "[[[1,2],[3,4]],[[5,6],[7,8]]]"

0 comments on commit efc7ffa

Please sign in to comment.