Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions json_serialization/writer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ template writeValueStringLike(w, value) =
of '\f': addPrefixSlash 'f' # \x0c
of '\r': addPrefixSlash 'r' # \x0d
of '"' : addPrefixSlash '\"'
of '\\': addPrefixSlash '\\'
of '\x00'..'\x07', '\x0b', '\x0e'..'\x1f':
s.write "\\u00"
s.write hexChars[(uint8(c) shr 4) and 0x0f]
Expand Down
56 changes: 19 additions & 37 deletions tests/test_writer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@ type
b: Option[string]
c: int

createJsonFlavor YourJson,
omitOptionalFields = false
createJsonFlavor YourJson, omitOptionalFields = false

createJsonFlavor MyJson,
omitOptionalFields = true
createJsonFlavor MyJson, omitOptionalFields = true

ObjectWithOptionalFields.useDefaultSerializationIn YourJson
ObjectWithOptionalFields.useDefaultSerializationIn MyJson

type
FruitX = enum
BananaX = "BaNaNa"
AppleX = "ApplE"
GrapeX = "VVV"
AppleX = "ApplE"
GrapeX = "VVV"

Drawer = enum
One
Expand All @@ -47,8 +45,7 @@ FruitX.configureJsonSerialization(EnumAsString)
Json.configureJsonSerialization(Drawer, EnumAsNumber)
MyJson.configureJsonSerialization(Drawer, EnumAsString)

proc writeValue*(w: var JsonWriter, val: OWOF)
{.gcsafe, raises: [IOError].} =
proc writeValue*(w: var JsonWriter, val: OWOF) {.gcsafe, raises: [IOError].} =
w.writeObject(OWOF):
w.writeField("a", val.a)
w.writeField("b", val.b)
Expand Down Expand Up @@ -140,17 +137,9 @@ suite "Test writer":
check json == "[123,null,777]"

test "object with optional fields":
let x = ObjectWithOptionalFields(
a: Opt.some(123),
b: some("nano"),
c: 456,
)

let y = ObjectWithOptionalFields(
a: Opt.none(int),
b: none(string),
c: 999,
)
let x = ObjectWithOptionalFields(a: Opt.some(123), b: some("nano"), c: 456)

let y = ObjectWithOptionalFields(a: Opt.none(int), b: none(string), c: 999)

let u = YourJson.encode(x)
check u == """{"a":123,"b":"nano","c":456}"""
Expand All @@ -165,17 +154,9 @@ suite "Test writer":
check yy == """{"c":999}"""

test "writeField with object with optional fields":
let x = OWOF(
a: Opt.some(123),
b: some("nano"),
c: 456,
)

let y = OWOF(
a: Opt.none(int),
b: none(string),
c: 999,
)
let x = OWOF(a: Opt.some(123), b: some("nano"), c: 456)

let y = OWOF(a: Opt.none(int), b: none(string), c: 999)

let xx = MyJson.encode(x)
check xx == """{"a":123,"b":"nano","c":456}"""
Expand Down Expand Up @@ -212,11 +193,10 @@ suite "Test writer":
check true

test "Enum value representation of DefaultFlavor":
type
ExoticFruits = enum
DragonFruit
SnakeFruit
StarFruit
type ExoticFruits = enum
DragonFruit
SnakeFruit
StarFruit

DefaultFlavor.flavorEnumRep(EnumAsNumber)
let u = Json.encode(DragonFruit)
Expand All @@ -234,7 +214,7 @@ suite "Test writer":
type
Fruit = enum
Banana = "BaNaNa"
Apple = "ApplE"
Apple = "ApplE"
JackFruit = "VVV"

ObjectWithEnumField = object
Expand Down Expand Up @@ -293,8 +273,10 @@ suite "Test writer":

test "escapes":
check Json.encode("\x12") == """"\u0012""""
check Json.encode("""a\b""") == """"a\\b""""

test "Empty object":
type NoFields = object

check: Json.encode(default(NoFields)) == "{}"
check:
Json.encode(default(NoFields)) == "{}"