diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2018-04-10 22:28:01 +0300 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2018-04-10 22:28:01 +0300 |
| commit | 8dbc7d9d9b24a602c15acf4a99c323af183473c4 (patch) | |
| tree | 4be527a2d845cfa84c87293cd1d4de99e4131a0f | |
| parent | d6896cabcbe3b774c7a6864875803a35c20eae54 (diff) | |
| download | nimpb-8dbc7d9d9b24a602c15acf4a99c323af183473c4.tar.gz nimpb-8dbc7d9d9b24a602c15acf4a99c323af183473c4.zip | |
JSON parsing fixesjson-parsing
| -rw-r--r-- | src/nimpb/json.nim | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/src/nimpb/json.nim b/src/nimpb/json.nim index 6079e37..0cf8a58 100644 --- a/src/nimpb/json.nim +++ b/src/nimpb/json.nim @@ -44,6 +44,13 @@ proc toJson*(value: int64): JsonNode = proc toJson*(value: uint64): JsonNode = newJString($value) +proc toJson*[Enum: enum](value: Enum): JsonNode = + for v in Enum: + if value == v: + return %($v) + # The enum has a value that is not defined in the enum type + result = %(cast[int](value)) + proc toJson*(message: google_protobuf_DoubleValue): JsonNode = if hasValue(message): result = toJson(message.value) @@ -231,7 +238,7 @@ proc parseEnum*[T](node: JsonNode): T = if node.kind == JString: result = parseEnum[T](node.str) elif node.kind == JInt: - result = T(node.num) + result = cast[T](node.num) else: raise newException(ValueError, "invalid enum value") @@ -318,16 +325,18 @@ proc parseBool*(node: JsonNode): bool = result = getBool(node) proc parsegoogle_protobuf_DoubleValueFromJson*(node: JsonNode): google_protobuf_DoubleValue = - if node.kind != JFloat: - raise newException(JsonParseError, "not a float") result = newgoogle_protobuf_DoubleValue() - result.value = getFloat(node) + if node.kind == JInt or node.kind == JFloat: + result.value = getFloat(node) + else: + raise newException(JsonParseError, "not a float") proc parsegoogle_protobuf_FloatValueFromJson*(node: JsonNode): google_protobuf_FloatValue = - if node.kind != JFloat: - raise newException(JsonParseError, "not a float") result = newgoogle_protobuf_FloatValue() - result.value = getFloat(node) + if node.kind == JInt or node.kind == JFloat: + result.value = getFloat(node) + else: + raise newException(JsonParseError, "not a float") proc parsegoogle_protobuf_Int64ValueFromJson*(node: JsonNode): google_protobuf_Int64Value = result = newgoogle_protobuf_Int64Value() |
