jsoneasy

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2023 License: MIT Imports: 3 Imported by: 0

README

jsoneasy

Overview

Inspired by JavaScript's JSON methods, jsoneasy contains two methods: Stringify and Parse.

In addition to using these clear and well-understood names instead of "Marshall," "Unmarshal," etc., the jsoneasy package addresses the false assumption that the Go's built-in encoding/json package makes: By default, json.Marshal converts &, <, > characters to Unicode in its generated JSON string output. This is helpful only when in an HTML (or XML) context. Otherwise, it is a mistake.

JSON.Stringify does not convert &, <, > to Unicode, thus preserving the integrity of the original content.

Examples

import "codeberg.org/gold/jsoneasy"

var JSON = jsoneasy.JSON{}

func main() {
  data := map[string]interface{
    "dimension": "fourth",
    "ThePrisonerName": 2,
    "number": "e",
    "isPackageUseful": true,
  }

  // 2nd arg is the indent level.
  jsonString, err := JSON.Stringify(data, 2)

  // Convert string to []byte if necessary.
  jsonByteSlice := JSON.StringToByteSlice(jsonString)

  myParsedData := make(map[string](interface{}))
  err := JSON.Parse(&jsonByteSlice, &myParsedData)

  // myParsedData is now a native Go map.
}

Credits

Gerry Gold, June 2023

Have fun!

Documentation

Index

Constants

View Source
const LARGE_STRING_MIN_LENGTH = 5_000

Variables

This section is empty.

Functions

This section is empty.

Types

type JSON

type JSON struct{}

func (JSON) ConvertLargeStringToByteSlice added in v1.1.0

func (j JSON) ConvertLargeStringToByteSlice(str string) []byte

The minor overhead for stream-based conversion is warrented for greater efficiency.

func (JSON) ConvertSmallStringToByteSlice added in v1.1.0

func (j JSON) ConvertSmallStringToByteSlice(str string) []byte

This is acceptable for smaller strings.

func (JSON) Parse

func (j JSON) Parse(jsonBytes *[]byte, v any) error

Convert a JSON string to native data object, e.g.:

myData := make(map[string](interface{})) err := JSON.Parse(&myJsonString, &myData)

func (JSON) StringToByteSlice added in v1.1.0

func (j JSON) StringToByteSlice(str string) []byte

func (JSON) Stringify

func (j JSON) Stringify(t interface{}, indentLevel int) (string, error)

Convert native data to JSON string, e.g.:

myHttpResponsePayloadString, err := JSON.Stringify(myData, 0)

Jump to

Keyboard shortcuts

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