Iris
Getting Started
  • What is Iris
  • 📌Getting started
    • Installation
    • Quick start
  • 🔌Routing
    • Middleware
    • API Versioning
  • 🗜️Compression
    • Index
  • ✈️Redirect
    • Redirect from Context
    • Rewrite Middleware
    • Multi Application Instances
  • 🖼️ View/Templates
    • Documentation
    • Benchmarks
    • ➲ Examples
  • 📁File Server
    • Introduction
    • Listing
    • In-memory Cache
    • HTTP/2 Push + Embedded + Cache and Compression
    • The PrefixDir function
    • Serve files from Context
    • ➲ Examples
  • 🌎Localization
    • Documentation
    • Sitemap
    • ➲ Examples
  • 🛡️Security
    • Basic Authentication
    • CORS
    • Sessions & Cookies
    • CSRF
    • JSON Web Tokens
    • Access Control
    • Anti-bot CAPTCHA
    • ➲ Examples
  • 🚀Responses
    • Text
    • HTML
    • Markdown
    • XML
    • YAML
    • Binary
    • JSON
    • JSONP
    • Problem
    • Protocol Buffers
    • MessagePack
    • Gzip
    • Content Negotiation
    • Stream
    • Server-Sent Events
    • HTTP/2 Push
    • Recorder
    • Outroduction
    • ➲ Examples
  • 📥Requests
    • URL Query
    • Headers
    • URL Path Parameters
    • Form
    • Text
    • XML
    • YAML
    • Binary
    • JSON
    • Validation
    • Protocol Buffers
    • MessagePack
    • Gzip
    • ➲ Examples
  • 💉Dependency Injection
    • Documentation
    • Register Dependency from Context
    • Inputs
    • Outputs
    • ➲ Examples
  • 🦏MVC
    • Quick start
    • Documentation
    • Handle Errors
    • Sessions
    • Websockets
    • gRPC
    • ➲ Examples
  • 🤓Resources
    • Examples
    • Starter Kits
    • Publications
    • Benchmarks
    • Support
  • 📘Contents
    • Host
      • Automatic Public Domain with TLS
    • Configuration
    • Routing
      • Path Parameter Types
      • Reverse Lookups
      • Handle HTTP errors
      • Subdomains
      • Wrap the Router
      • Context Methods
    • HTTP Method Override
    • HTTP Referrer
    • URL Query Parameters
    • Forms
    • Model Validation
    • Cache
    • Cookies
    • Sessions
      • Database
      • Flash Messages
    • Websockets
    • Sitemap
    • Localization
    • Testing
Powered by GitBook
On this page
  • ASCII
  • UnescapeHTML

Was this helpful?

  1. 🚀Responses

JSON

Content-Type: "application/json"

The Context.JSON(v, ...opts) is the method which sends JSON responses to the client. It accepts the value and optional settings for rendering. The JSON options structure looks like this:

// JSON contains the options for the JSON (Context's) Renderer.
type JSON struct {
    // http-specific
    StreamingJSON bool
    // content-specific
    UnescapeHTML bool
    Indent       string
    Prefix       string
    ASCII        bool
    // if true then it prepends a "while(1);" when Go slice (to JSON Array) value.
    Secure       bool
    // proto.Message specific marshal options.
    Proto ProtoMarshalOptions
}

If Indent field is empty and the application runs without optimizations, the Indent field will be automatically set to 2 spaces.

So, if we want to write a JSON with indentation of four spaces and prefixed with while(1) we can do something like that:

func handler(ctx iris.Context) {
    response := []string{"val1", "val2", "val3"}
    options := iris.JSON{Indent: "    ", Secure: true}
    ctx.JSON(response, options)
}

Result

while(1);[
    "val1",
    "val2",
    "val3"
]
type User struct {
    Firstname    string `json:"firstname"`
    Lastname     string `json:"lastname"`
    IgnoredField int    `json:"-"`
}

func handler(ctx iris.Context) {
    response := User{
        Firstname: "makis",
        Lastname: "maropoulos",
        IgnoredField:42,
    }

    ctx.JSON(response)
}

Result

{
  "firstname": "makis",
  "lastname": "maropoulos"
}

ASCII

Use the ASCII field to generate ASCII-only JSON with escaped non-ASCII characters.

app.Get("/json_ascii", func(ctx iris.Context) {
    response := iris.Map{"lang": "GO-虹膜", "tag": "<br>"}
    options := iris.JSON{Indent: "    ", ASCII: true}
    ctx.JSON(response, options)
})

Result

{
    "lang": "GO-\u8679\u819c",
    "tag": "\u003cbr\u003e"
}

UnescapeHTML

Do not replace special HTML characters with their unicode entities using the UnescapeHTML field.

app.Get("/json_raw", func(ctx iris.Context) {
    options := iris.JSON{UnescapeHTML: true}
    ctx.JSON(iris.Map{
        "html": "<b>Hello, world!</b>",
    }, options)
})

Result

{"html":"<b>Hello, world!</b>"}
PreviousBinaryNextJSONP

Last updated 2 years ago

Was this helpful?

As with all RESTful rich responses, any marshallable (JSON at this case) Go type can be given. If we want to render a Go struct as JSON, the struct's fields we want to render should be , and optionally tagged with the json struct tag. Look the exaple below:

exported