jsonmask

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2021 License: MIT Imports: 12 Imported by: 2

README

json-mask-go

Build Status Coverage Status

JSON mask for Go, selecting specific parts of JSON string. Inspired by:

https://developers.google.com/tasks/performance#partial-response

https://github.com/nemtsov/json-mask

Installation

go get github.com/teambition/json-mask-go

Syntax

The syntax is loosely based on XPath:

a       select a field 'a'
a,b,c   comma-separated list will select multiple fields
a/b/c   path will select a field from its parent
a(b,c)  sub-selection will select many fields from a parent
a/*/c   the star * wildcard will select all items in a field
a,b/c(d,e(f,g/h)),i

Examples

doc := `
{
  "kind": "demo",
  "items": [
  {
    "title": "First title",
    "comment": "First comment.",
    "characteristics": {
      "length": "short",
      "accuracy": "high",
      "followers": ["Jo", "Will"]
    },
    "status": "active"
  },
  {
    "title": "Second title",
    "comment": "Second comment.",
    "characteristics": {
      "length": "long",
      "accuracy": "medium",
      "followers": [ ]
    },
    "status": "pending"
  }
  ]
}
`

result, _ := jsonmask.Mask([]byte(doc), "kind,items(title,characteristics/length)")
// OR:
// selection, err := jsonmask.Compile("kind,items(title,characteristics/length)")
// result, err := selection.Mask([]byte(doc))
fmt.Println(string(result))
// Output:
// {"items":[{"characteristics":{"length":"short"},"title":"First title"},{"characteristics":{"length":"long"},"title":"Second title"}],"kind":"demo"}

Documentation

Overview

Example (Mask)
package main

import (
	"fmt"

	jsonmask "github.com/teambition/json-mask-go"
)

func main() {
	doc := `
	{
		"kind": "demo",
		"items": [
		{
			"title": "First title",
			"comment": "First comment.",
			"characteristics": {
				"length": "short",
				"accuracy": "high",
				"followers": ["Jo", "Will"]
			},
			"status": "active"
		},
		{
			"title": "Second title",
			"comment": "Second comment.",
			"characteristics": {
				"length": "long",
				"accuracy": "medium",
				"followers": [ ]
			},
			"status": "pending"
		}
		]
	}
	`

	result, _ := jsonmask.Mask([]byte(doc), "kind,items(title,characteristics/length)")

	fmt.Println(string(result))
}
Output:

{"items":[{"characteristics":{"length":"short"},"title":"First title"},{"characteristics":{"length":"long"},"title":"Second title"}],"kind":"demo"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Mask

func Mask(doc []byte, fields string) ([]byte, error)

Mask selects the specific parts of an JSON string, according to the mask "fields".

func NewJSONMask added in v1.2.0

func NewJSONMask(next http.Handler, queryKey string) (http.Handler, error)

Types

type JSONMask added in v1.2.0

type JSONMask struct {
	// contains filtered or unexported fields
}

JSONMask

func (*JSONMask) ServeHTTP added in v1.2.0

func (h *JSONMask) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type JSONMaskResponseWriter added in v1.2.0

type JSONMaskResponseWriter interface {
	http.ResponseWriter
	Close() error
	Status() JSONMaskResponseWriterStatus
}

func NewResponseWriter added in v1.2.0

func NewResponseWriter(rw http.ResponseWriter, sl Selection) JSONMaskResponseWriter

type JSONMaskResponseWriterStatus added in v1.2.0

type JSONMaskResponseWriterStatus struct {
	Status  int
	Size    int
	Trimmed int
}

type ResponseWriter added in v1.2.0

type ResponseWriter struct {
	// contains filtered or unexported fields
}

func (*ResponseWriter) Close added in v1.2.0

func (jrw *ResponseWriter) Close() error

func (*ResponseWriter) Flush added in v1.2.0

func (jrw *ResponseWriter) Flush()

func (*ResponseWriter) Header added in v1.2.0

func (jrw *ResponseWriter) Header() http.Header

func (*ResponseWriter) Hijack added in v1.2.0

func (jrw *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*ResponseWriter) Status added in v1.2.0

func (*ResponseWriter) Write added in v1.2.0

func (jrw *ResponseWriter) Write(b []byte) (int, error)

func (*ResponseWriter) WriteHeader added in v1.2.0

func (jrw *ResponseWriter) WriteHeader(s int)

type ResponseWriterWithCloseNotify added in v1.2.0

type ResponseWriterWithCloseNotify struct {
	*ResponseWriter
}

func (*ResponseWriterWithCloseNotify) CloseNotify added in v1.2.0

func (jrw *ResponseWriterWithCloseNotify) CloseNotify() <-chan bool

type Selection added in v1.1.0

type Selection map[string]Selection

func Compile added in v1.1.0

func Compile(str string) (Selection, error)

The syntax is loosely based on XPath:

a select a field 'a' a,b,c comma-separated list will select multiple fields a/b/c path will select a field from its parent a(b,c) sub-selection will select many fields from a parent a/*/c the star * wildcard will select all items in a field a,b/c(d,e(f,g/h)),i

func (Selection) Mask added in v1.1.0

func (sl Selection) Mask(doc []byte) ([]byte, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL