Deserialize the request body

Returns the deserialized request body body or null if something went wrong. If the object cannot be deserialized, answers with a HTTP 400.

See BODY and new_body_object.

Example:

class MyDeserializedHandler
    super Handler

    redef type BODY: MyObject

    redef fun post(req, res) do
        var form = deserialize_body(req, res)
        if form == null then return # Deserialization error
        # At this point popcorn returned a HTTP 400 code if something was wrong with
        # the deserialization process

        # TODO do something with the input
        print form.name
    end
end

class MyObject
    serialize

    var name: String
end

Property definitions

popcorn :: pop_json $ Handler :: deserialize_body
	# Deserialize the request body
	#
	# Returns the deserialized request body body or `null` if something went wrong.
	# If the object cannot be deserialized, answers with a HTTP 400.
	#
	# See `BODY` and `new_body_object`.
	#
	# Example:
	# ~~~nit
	# class MyDeserializedHandler
	#	super Handler
	#
	#	redef type BODY: MyObject
	#
	#	redef fun post(req, res) do
	#		var form = deserialize_body(req, res)
	#		if form == null then return # Deserialization error
	#		# At this point popcorn returned a HTTP 400 code if something was wrong with
	#		# the deserialization process
	#
	#		# TODO do something with the input
	#		print form.name
	#	end
	# end
	#
	# class MyObject
	#	serialize
	#
	#	var name: String
	# end
	# ~~~
	fun deserialize_body(req: HttpRequest, res: HttpResponse): nullable BODY do
		var body = req.body
		var deserializer = new JsonDeserializer(body)
		var form = deserializer.deserialize(body_type)
		if not form isa BODY or deserializer.errors.not_empty then
			res.json_error("Bad input", 400)
			return null
		end
		return form
	end
lib/popcorn/pop_json.nit:117,2--157,4