Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
JsonView Class Reference

A view that renders structured JSON and returns "application/json" content type. More...

#include <JsonView.h>

+ Inheritance diagram for JsonView:
+ Collaboration diagram for JsonView:

Public Member Functions

 JsonView (const nlohmann::json &payload)
 
std::string render (const std::map< std::string, std::string > &context={}) const override
 Render the view body.
 
std::string getContentType () const override
 Return the MIME content type for this view.
 
- Public Member Functions inherited from FrameworkView
virtual ~FrameworkView ()=default
 
virtual void applyHeaders (HttpResponse &response) const
 Optional hook to set response headers (e.g., Content-Disposition).
 
virtual std::string render (const nlohmann::json &context) const
 Optional JSON context hook (used by JsonView or convenience).
 

Private Attributes

nlohmann::json payload_
 

Detailed Description

Definition at line 20 of file JsonView.h.

Constructor & Destructor Documentation

◆ JsonView()

JsonView::JsonView ( const nlohmann::json &  payload)
explicit

Definition at line 14 of file JsonView.cpp.

15 : payload_(payload) {}
nlohmann::json payload_
Definition JsonView.h:28

Member Function Documentation

◆ getContentType()

std::string JsonView::getContentType ( ) const
overridevirtual

Implements FrameworkView.

Definition at line 22 of file JsonView.cpp.

23{
24 return "application/json";
25}

◆ render()

std::string JsonView::render ( const std::map< std::string, std::string > &  context = {}) const
overridevirtual

Optional context (used by templates).

Implements FrameworkView.

Definition at line 17 of file JsonView.cpp.

18{
19 return payload_.dump(2); // pretty-print
20}

References payload_.

Member Data Documentation

◆ payload_

nlohmann::json JsonView::payload_
private

Definition at line 28 of file JsonView.h.

Referenced by render().


The documentation for this class was generated from the following files: