json

go
package api

import (
  "encoding/json"
  "errors"
  "io"

Streaming JSON decoding with DisallowUnknownFields

go http json
by Leah Thompson 1 tab
ruby
class JsonLogFormatter < ActiveSupport::Logger::SimpleFormatter
  def call(severity, timestamp, _progname, message)
    if message.is_a?(Hash)
      entry = {
        ts: timestamp.utc.iso8601(3),
        level: severity

Lograge-Style JSON Logging Without Extra Gems

rails logging observability
by codesnips 3 tabs
go
package types

import (
  "bytes"
  "encoding/json"
  "time"

Strict JSON time parsing with custom UnmarshalJSON

go json time
by Leah Thompson 1 tab
go
package api

import (
  "encoding/json"
  "errors"
  "io"

Strict JSON decode helper (size limit + unknown fields)

go http json
by Leah Thompson 1 tab
go
package api

import (
  "net/http"
  "strings"
)

Enforce JSON Content-Type and method early in handlers

go http api
by Leah Thompson 1 tab
ruby
class Contract
  INVALID = Object.new.freeze

  COERCERS = {
    string: ->(v) { v.is_a?(String) ? v : v.to_s },
    integer: ->(v) { Integer(v.to_s) rescue INVALID },

Validated JSON Schema with dry-validation-style contract (lightweight)

api validation contracts
by codesnips 4 tabs
go
package api

import (
  "encoding/json"
  "net/http"
)

WriteJSON helper with consistent headers and status

go http json
by Leah Thompson 1 tab
python
from typing import Optional
from pydantic import BaseModel, EmailStr, Field, field_validator, model_validator


class CreateUserRequest(BaseModel):
    model_config = {"extra": "forbid"}

Validating JSON Payloads with Pydantic v2 and Returning Field-Level Errors in FastAPI

fastapi pydantic validation
by codesnips 3 tabs
go
package events

import "encoding/json"

type Envelope struct {
  Type    string          `json:"type"`

Store unstructured JSON safely with json.RawMessage

go json events
by Leah Thompson 1 tab
php
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

Laravel API resources for JSON transformation

laravel api resources
by Carlos Mendez 2 tabs
swift
import Foundation

// MARK: - Basic Codable
struct User: Codable {
    let id: Int
    let username: String

JSON encoding and decoding with Codable

swift codable json
by Sofia Martinez 1 tab
ruby
class Problem
  DEFAULT_TYPE = "https://api.example.com/problems/about:blank".freeze

  attr_reader :type, :title, :status, :detail, :instance, :extensions

  def initialize(status:, title:, detail: nil, type: nil, instance: nil, extensions: {})

API Error Handling with Problem Details (RFC7807-ish)

rails api error-handling
by codesnips 3 tabs