json

package
v0.49.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Based on https://github.com/tidwall/sjson but modified to use https://github.com/ohler/ojg instead of encoding/json. Credit to https://github.com/tidwall as the original author of sjson.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(json, path string) (string, error)

Delete deletes a value from json for the specified path.

func DeleteBytes

func DeleteBytes(json []byte, path string) ([]byte, error)

DeleteBytes deletes a value from json for the specified path.

func Set

func Set(json, path string, value interface{}) (string, error)

Set sets a json value for the specified path. A path is in dot syntax, such as "name.last" or "age". This function expects that the json is well-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. An error is returned if the path is not valid.

A path is a series of keys separated by a dot.

{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "friends": [
    {"first": "James", "last": "Murphy"},
    {"first": "Roger", "last": "Craig"}
  ]
}
"name.last"          >> "Anderson"
"age"                >> 37
"children.1"         >> "Alex"

func SetBytes

func SetBytes(json []byte, path string, value interface{}) ([]byte, error)

SetBytes sets a json value for the specified path. If working with bytes, this method preferred over Set(string(data), path, value)

func SetBytesOptions

func SetBytesOptions(json []byte, path string, value interface{},
	opts *Options) ([]byte, error)

SetBytesOptions sets a json value for the specified path with options. If working with bytes, this method preferred over SetOptions(string(data), path, value)

func SetOptions

func SetOptions(json, path string, value interface{},
	opts *Options) (string, error)

SetOptions sets a json value for the specified path with options. A path is in dot syntax, such as "name.last" or "age". This function expects that the json is well-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. An error is returned if the path is not valid.

func SetRaw

func SetRaw(json, path, value string) (string, error)

SetRaw sets a raw json value for the specified path. This function works the same as Set except that the value is set as a raw block of json. This allows for setting premarshalled json objects.

func SetRawBytes

func SetRawBytes(json []byte, path string, value []byte) ([]byte, error)

SetRawBytes sets a raw json value for the specified path. If working with bytes, this method preferred over SetRaw(string(data), path, value)

func SetRawBytesOptions

func SetRawBytesOptions(json []byte, path string, value []byte,
	opts *Options) ([]byte, error)

SetRawBytesOptions sets a raw json value for the specified path with options. If working with bytes, this method preferred over SetRawOptions(string(data), path, value, opts)

func SetRawOptions

func SetRawOptions(json, path, value string, opts *Options) (string, error)

SetRawOptions sets a raw json value for the specified path with options. This furnction works the same as SetOptions except that the value is set as a raw block of json. This allows for setting premarshalled json objects.

Types

type Options

type Options struct {
	// Optimistic is a hint that the value likely exists which
	// allows for the sjson to perform a fast-track search and replace.
	Optimistic bool
	// ReplaceInPlace is a hint to replace the input json rather than
	// allocate a new json byte slice. When this field is specified
	// the input json will not longer be valid and it should not be used
	// In the case when the destination slice doesn't have enough free
	// bytes to replace the data in place, a new bytes slice will be
	// created under the hood.
	// The Optimistic flag must be set to true and the input must be a
	// byte slice in order to use this field.
	ReplaceInPlace bool
}

Options represents additional options for the Set and Delete functions.

Jump to

Keyboard shortcuts

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