Skip to content

Commit 9e1017f

Browse files
authored
add decde overload for JsonString (#127)
1 parent c4bdde9 commit 9e1017f

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

json_serialization/format.nim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ when declared(macrocache.hasKey): # Nim 1.6 have no macrocache.hasKey
100100
generateJsonAutoSerializationAddon(DummyFlavor)
101101
DummyFlavor.automaticBuiltinSerialization(false)
102102

103+
template decode*(
104+
Format: type Json,
105+
inputParam: JsonString,
106+
RecordType: type,
107+
params: varargs[untyped],
108+
): auto =
109+
decode(Format, string(inputParam), RecordType, params)
110+
103111
template createJsonFlavor*(FlavorName: untyped,
104112
mimeTypeValue = "application/json",
105113
automaticObjectSerialization = false,
@@ -118,6 +126,14 @@ template createJsonFlavor*(FlavorName: untyped,
118126
template PreferredOutputType*(T: type FlavorName): type = string
119127
template mimeType*(T: type FlavorName): string = mimeTypeValue
120128

129+
template decode*(
130+
Format: type FlavorName,
131+
inputParam: JsonString,
132+
RecordType: type,
133+
params: varargs[untyped],
134+
): auto =
135+
decode(Format, string(inputParam), RecordType, params)
136+
121137
template flavorUsesAutomaticObjectSerialization*(T: type FlavorName): bool = automaticObjectSerialization
122138
template flavorOmitsOptionalFields*(T: type FlavorName): bool = omitOptionalFields
123139
template flavorRequiresAllFields*(T: type FlavorName): bool = requireAllFields

tests/test_json_flavor.nim

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ suite "Test JsonFlavor":
130130

131131
test "object with null fields":
132132
expect JsonReaderError:
133-
let x = Json.decode(jsonTextWithNullFields, Container)
134-
discard x
133+
discard Json.decode(jsonTextWithNullFields, Container)
134+
expect JsonReaderError:
135+
discard Json.decode(JsonString(jsonTextWithNullFields), Container)
135136

136137
let x = NullyFields.decode(jsonTextWithNullFields, Container)
137138
check x.list.len == 0
@@ -203,7 +204,7 @@ suite "Test JsonFlavor":
203204
NullyFields.flavorEnumRep(EnumAsStringifiedNumber)
204205
let x = NullyFields.encode(JackFruit)
205206
check x == "\"2\""
206-
207+
207208
NullyFields.flavorEnumRep(EnumAsNumber)
208209
let z = NullyFields.encode(Banana)
209210
check z == "0"

tests/test_serialization.nim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,11 @@ suite "toJson tests":
608608
"""
609609

610610
expect IncompleteObjectError:
611-
let shouldNotDecode = Json.decode(json, Simple, requireAllFields = true)
612-
echo "This should not have decoded ", shouldNotDecode
611+
discard Json.decode(json, Simple, requireAllFields = true)
612+
613+
expect IncompleteObjectError:
614+
# JsonString overload parameters forwarding..
615+
discard Json.decode(JsonString(json), Simple, requireAllFields = true)
613616

614617
test "all fields were required, but not all were provided (additional fields present instead)":
615618
let json = test_dedent"""

0 commit comments

Comments
 (0)