Files
nim-jwt-full/src/main/jwt_full/private/jsonutils.nim
T

35 lines
1.1 KiB
Nim

import std/json, std/options, std/strutils, std/times
import ./timeutils
func reqStrVal*(n: JsonNode, key: string): string =
if n.hasKey(key):
let val = n[key].getStr("")
if not val.isEmptyOrWhitespace: return val
raise newException(ValueError, "missing value for '" & key & "'")
func optStrVal*(n: JsonNode, key: string): Option[string] =
if n.hasKey(key): some(n[key].getStr)
else: none[string]()
func reqIntVal*(n: JsonNode, key: string): BiggestInt =
if n.hasKey(key):
if n[key].kind == JInt:
return n[key].getBiggestInt
raise newException(ValueError, "value for '" & key & "' is missing or not an integer")
func optIntVal*(n: JsonNode, key: string): Option[BiggestInt] =
if n.hasKey(key) and n[key].kind == JInt:
some(n[key].getBiggestInt)
else: none[BiggestInt]()
proc reqNumericDate*(n: Jsonnode, key: string): DateTime =
fromNumericDate(n.reqIntVal(key))
proc optNumericDate*(n: JsonNode, key: string): Option[DateTime] =
let val = n.optIntVal(key)
if val.isSome: return some(fromNumericDate(val.get))
else: return none[DateTime]()