Skip to content

Commit

Permalink
Merge pull request #3 from gabbhack/rename
Browse files Browse the repository at this point in the history
Rename `fromString` to `fromJson`, `toString` to `toJson`
  • Loading branch information
Gabben authored Aug 24, 2022
2 parents fb7b545 + a35f34b commit b716555
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 66 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
First, install [deser](https://github.com/gabbhack/deser) via `nimble install deser`

deser_json provides three procedures:
1. `toString` for serialization
1. `toPrettyString` for pretty serialization
1. `fromString` for deserialization
1. `toJson` for serialization
1. `toPrettyJson` for pretty serialization
1. `fromJson` for deserialization

```nim
import
Expand All @@ -26,9 +26,9 @@ import
var some = [1, 2, 3]
echo some.toString()
echo some.toJson()
some = fromString(typeof(some), "[1, 2, 3]")
some = fromJson(typeof(some), "[1, 2, 3]")
```

See the [deser documentation](https://deser.nim.town/deser.html) for a complete example.
Expand Down
4 changes: 2 additions & 2 deletions src/deser_json/des.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import des/des
export des


func fromString*(Self: typedesc, input: sink string): Self {.inline.} = ##[
func fromJson*(Self: typedesc, input: sink string): Self {.inline.} = ##[
Deserialize your type from string. Accepts only type that implement `deserialize` procedure.
]##
runnableExamples:
import deser

let integer = int.fromString("123")
let integer = int.fromJson("123")
assert integer == 123

mixin deserialize
Expand Down
8 changes: 4 additions & 4 deletions src/deser_json/ser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export
formatter


func toString*(value: auto): string {.inline.} = ##[
func toJson*(value: auto): string {.inline.} = ##[
Serialize your value to string. Accepts only type that implement `serialize` procedure.
]##
runnableExamples:
import deser

let str = 123.toString()
let str = 123.toJson()
assert str == "123"

mixin serialize
Expand All @@ -24,13 +24,13 @@ Serialize your value to string. Accepts only type that implement `serialize` pro
result = ser.writer


func toPrettyString*(value: auto): string {.inline.} = ##[
func toPrettyJson*(value: auto): string {.inline.} = ##[
Serialize your value to pretty string. Accepts only type that implement `serialize` procedure.
]##
runnableExamples:
import deser

let str = 123.toPrettyString()
let str = 123.toPrettyJson()
assert str == "123"

mixin serialize
Expand Down
56 changes: 28 additions & 28 deletions tests/des/tdes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,67 +21,67 @@ makeDeserializable(Struct)

suite "serialize":
test "bool":
doAssert true == bool.fromString("true")
doAssert false == bool.fromString("false")
doAssert true == bool.fromJson("true")
doAssert false == bool.fromJson("false")

test "int":
doAssert 123 == int.fromString("123")
doAssert 123i8 == int8.fromString("123")
doAssert 123i16 == int16.fromString("123")
doAssert 123i32 == int32.fromString("123")
doAssert 123i64 == int64.fromString("123")
doAssert 123 == int.fromJson("123")
doAssert 123i8 == int8.fromJson("123")
doAssert 123i16 == int16.fromJson("123")
doAssert 123i32 == int32.fromJson("123")
doAssert 123i64 == int64.fromJson("123")

doAssert 123.uint == uint.fromString("123")
doAssert 123u8 == uint8.fromString("123")
doAssert 123u16 == uint16.fromString("123")
doAssert 123u32 == uint32.fromString("123")
doAssert 123u64 == uint64.fromString("123")
doAssert 123.uint == uint.fromJson("123")
doAssert 123u8 == uint8.fromJson("123")
doAssert 123u16 == uint16.fromJson("123")
doAssert 123u32 == uint32.fromJson("123")
doAssert 123u64 == uint64.fromJson("123")

test "float":
doAssert 123.0 == float.fromString("123.0")
doAssert 123.0f32 == float32.fromString("123.0")
doAssert 123.0f64 == float64.fromString("123.0")
doAssert 123.0 == float.fromJson("123.0")
doAssert 123.0f32 == float32.fromJson("123.0")
doAssert 123.0f64 == float64.fromJson("123.0")

test "string":
doAssert "123" == string.fromString("\"123\"")
doAssert "123" == string.fromJson("\"123\"")

test "char":
doAssert '1' == char.fromString("\"1\"")
doAssert '1' == char.fromJson("\"1\"")

test "bytes":
doAssert ['0'.byte, '1'.byte] == fromString(array[2, byte], "[48,49]")
doAssert ['0'.byte, '1'.byte] == fromJson(array[2, byte], "[48,49]")

test "none":
doAssert none[int]() == fromString(Option[int], "null")
doAssert none[int]() == fromJson(Option[int], "null")

test "some":
doAssert some(123) == fromString(Option[int], "123")
doAssert some(123) == fromJson(Option[int], "123")

test "array":
doAssert [1,2,3] == fromString(array[3, int], "[1,2,3]")
doAssert [1,2,3] == fromJson(array[3, int], "[1,2,3]")

test "seq":
doAssert @[1,2,3] == fromString(seq[int], "[1,2,3]")
doAssert @[1,2,3] == fromJson(seq[int], "[1,2,3]")

test "tuple":
doAssert (1,2,3) == fromString((int, int, int), "[1,2,3]")
doAssert (1,2,3) == fromJson((int, int, int), "[1,2,3]")

test "named tuple":
doAssert (id: 123, text: "123") == fromString(tuple[id: int, text: string], "[123,\"123\"]")
doAssert (id: 123, text: "123") == fromJson(tuple[id: int, text: string], "[123,\"123\"]")

test "map":
doAssert {"text": "123"}.toTable == fromString(Table[string, string], "{\"text\":\"123\"}")
doAssert {"text": "123"}.toTable == fromJson(Table[string, string], "{\"text\":\"123\"}")

test "struct":
doAssert Struct(id: 123, text: "123") == Struct.fromString("{\"id\":123,\"text\":\"123\"}")
doAssert Struct(id: 123, text: "123") == Struct.fromJson("{\"id\":123,\"text\":\"123\"}")

test "enum":
type Foo = enum
First = "first"

doAssert First == Foo.fromString("\"first\"")
doAssert First == Foo.fromJson("\"first\"")

type Bar = enum
Second

doAssert Second == Bar.fromString("\"Second\"")
doAssert Second == Bar.fromJson("\"Second\"")
54 changes: 27 additions & 27 deletions tests/ser/tser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,66 +21,66 @@ makeSerializable(Struct)

suite "serialize":
test "bool":
doAssert ser.toString(true) == "true"
doAssert ser.toString(false) == "false"
doAssert ser.toJson(true) == "true"
doAssert ser.toJson(false) == "false"

test "int":
doAssert ser.toString(123) == "123"
doAssert ser.toString(123i8) == "123"
doAssert ser.toString(123i16) == "123"
doAssert ser.toString(123i32) == "123"
doAssert ser.toString(123i64) == "123"
doAssert ser.toJson(123) == "123"
doAssert ser.toJson(123i8) == "123"
doAssert ser.toJson(123i16) == "123"
doAssert ser.toJson(123i32) == "123"
doAssert ser.toJson(123i64) == "123"

doAssert ser.toString(123u8) == "123"
doAssert ser.toString(123u16) == "123"
doAssert ser.toString(123u32) == "123"
doAssert ser.toString(123u64) == "123"
doAssert ser.toJson(123u8) == "123"
doAssert ser.toJson(123u16) == "123"
doAssert ser.toJson(123u32) == "123"
doAssert ser.toJson(123u64) == "123"

test "float":
doAssert ser.toString(123.0) == "123.0"
doAssert ser.toString(123.0f32) == "123.0"
doAssert ser.toString(123.0f64) == "123.0"
doAssert ser.toJson(123.0) == "123.0"
doAssert ser.toJson(123.0f32) == "123.0"
doAssert ser.toJson(123.0f64) == "123.0"

test "string":
doAssert ser.toString("123") == "\"123\""
doAssert ser.toJson("123") == "\"123\""

test "char":
doAssert ser.toString('1') == "\"1\""
doAssert ser.toJson('1') == "\"1\""

test "bytes":
doAssert ser.toString(['0'.byte, '1'.byte]) == "[48,49]"
doAssert ser.toJson(['0'.byte, '1'.byte]) == "[48,49]"

test "none":
doAssert ser.toString(none int) == "null"
doAssert ser.toJson(none int) == "null"

test "some":
doAssert ser.toString(some 123) == "123"
doAssert ser.toJson(some 123) == "123"

test "array":
doAssert ser.toString([1,2,3]) == "[1,2,3]"
doAssert ser.toJson([1,2,3]) == "[1,2,3]"

test "seq":
doAssert ser.toString(@[1,2,3]) == "[1,2,3]"
doAssert ser.toJson(@[1,2,3]) == "[1,2,3]"

test "tuple":
doAssert ser.toString((1,2,3)) == "[1,2,3]"
doAssert ser.toJson((1,2,3)) == "[1,2,3]"

test "named tuple":
doAssert ser.toString((id: 123, text: "123")) == "[123,\"123\"]"
doAssert ser.toJson((id: 123, text: "123")) == "[123,\"123\"]"

test "map":
doAssert ser.toString({"text": "123"}.toTable) == "{\"text\":\"123\"}"
doAssert ser.toJson({"text": "123"}.toTable) == "{\"text\":\"123\"}"

test "struct":
doAssert ser.toString(Struct(id: 123, text: "123")) == "{\"id\":123,\"text\":\"123\"}"
doAssert ser.toJson(Struct(id: 123, text: "123")) == "{\"id\":123,\"text\":\"123\"}"

test "enum":
type Foo = enum
First = "first"

doAssert ser.toString(First) == "\"first\""
doAssert ser.toJson(First) == "\"first\""

type Bar = enum
Second

doAssert ser.toString(Second) == "\"Second\""
doAssert ser.toJson(Second) == "\"Second\""

0 comments on commit b716555

Please sign in to comment.