bung87 / scorper

scorper is a micro and elegant web framework written in Nim
Apache License 2.0
81 stars 5 forks source link

Request - Parse JSON string to Object directly #37

Closed shayanhabibi closed 3 years ago

shayanhabibi commented 3 years ago

I've used jsony to directly parse the string to an object since treeform mentions that the stringstreams are actually slower for this use anyway; is this indeed an efficient method?

Sorry my commit diff is horrible due to my editor settings, the link goes directly to the proc I added to your source code.

Else see here:

# scorper/src/scorper/http/streamserver.nim

proc obj*[T](req: ImpRequest, x: typedesc[T]): Future[T] {.async.} =
  if req.rawBody.isSome:
    return req.rawBody.unSafeGet.fromJson(T)
  var str: string
  result = "{}".fromJson(T)
  try:
    str = await req.reader.readLine(limit = req.contentLength.int)
  except AsyncStreamIncompleteError as e:
    await req.respStatus(Http400, ContentLengthMismatch)
    result = "{}".fromJson(T)
    req.rawBody = some(str)
    req.parsed = true
    return result
  try:
    result = str.fromJson(T)
  except CatchableError as e:
    raise newHttpError(Http400, e.msg)
  except Exception as e:
    raise newHttpError(Http400, e.msg)
  req.rawBody = some(str)
  req.parsed = true
bung87 commented 3 years ago

here are some thoughts. jsony is loose so it does not validate the input json string is valid or not, you may get corrupt json data. Here I don't use stringstreams I wish I find a lib that accept parse from network io buffer. Am not sure provide a api "Parse JSON string to Object directly" , is it necessary ? End developer can convert from JsonNode by calling one proc.

shayanhabibi commented 3 years ago

I suppose depends on needs; I am using this as my Scorper will be deployed in an internal environment where I need rapid serialisation and deserialisation. The conversion of string to JsonNodes before converting then to Objects is a sacrifice I can't make in this enterprise. Because I am assured the json is valid since I am developing the client ui which is interacting with the scorper it is a very useful api for myself. Perhaps offering it as an unsafe api?

I will require it in my own project however will keep it in my own fork for the future.

bung87 commented 3 years ago

I think we can expose an API wraps exists json api , handle json parse logic in same place. the main concerns here is I want response error automatically when json data is not valid.

bung87 commented 3 years ago

well , If you still need implement your own json handler , I think you can use scorper .stream api which returns a stream reader, you can use it in your own json handler or obj handler.