JSON Unmarshalling Library

JSAWN (JAY-sawn)

This is a JSON library to add to the capabilities of the standard 'encoding/json' library.

Unmarshalling

The first enhancement is to json.Unmarshal. The test file shows an example of the problem and how this library addresses it.

At a high level, the problem is that basic types on struct fields can fail to parse from JSON without causing other fields to not parse. If there is a custom struct field that fails to parse, the other fields of the struct don't get parsed. This means that if there are optional struct fields, any parsing errors on those will cause the entire parse to fail. This utility allows it to continue to parse everything else and return the failed field(s) as parse warnings.

jsawn="optional"

The jsawn.Unmarshal() returns a specific error type ParseWarning if the only parse errors happened on "optional" fields. If there was an error on a non-optional field, that error is returned instead of the warnings.

type Person struct {
  FirstName string `json:"fname"`
  LastName string `json:"lname"`

  // optional field denoted by the jsawn tag
  HairColor color.RGBA `json:"hair_color" jsawn:"optional"`
}

When there is a problem parsing this:

{
  "color":"#FFFFFF",
  "fname":"Foo",
  "lname":"Bar"
}

... the FirstName and LastName values will parse and there will be a warning for the HairColor failure. You can inspect the warnings like this:

var p Person
err := jsawn.Unmarshal(jsonStr, &p)
if err != nil {
  var pw *jsawn.ParseWarning
  errors.As(err, &pw) {
    for _, warning := range pw.Warnings {
      // warning is the original *json.UnmarshalTypeError
      log.Warn(warning)
    }
  }
}

jsawn="required"

If you want to make sure a certain field is present in the JSON, you can use the "required" tag value. If a required field is not present in the JSON string, a *json.UnmarshalTypeError will be returned.

Similar Resources

Abstract JSON for golang with JSONPath support

Abstract JSON Abstract JSON is a small golang package provides a parser for JSON with support of JSONPath, in case when you are not sure in its struct

Jan 5, 2023

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

fastjson - fast JSON parser and validator for Go Features Fast. As usual, up to 15x faster than the standard encoding/json. See benchmarks. Parses arb

Jan 5, 2023

Small utility to create JSON objects

Small utility to create JSON objects

gjo Small utility to create JSON objects. This was inspired by jpmens/jo. Support OS Mac Linux Windows Requirements Go 1.1.14~ Git Installtion Build $

Dec 8, 2022

A Go package for handling common HTTP JSON responses.

go-respond A Go package for handling common HTTP JSON responses. Installation go get github.com/nicklaw5/go-respond Usage The goal of go-respond is to

Sep 26, 2022

JSON query in Golang

gojq JSON query in Golang. Install go get -u github.com/elgs/gojq This library serves three purposes: makes parsing JSON configuration file much easie

Dec 28, 2022

Automatically generate Go (golang) struct definitions from example JSON

gojson gojson generates go struct definitions from json or yaml documents. Example $ curl -s https://api.github.com/repos/chimeracoder/gojson | gojson

Jan 1, 2023

A JSON diff utility

JayDiff A JSON diff utility. Install Downloading the compiled binary Download the latest version of the binary: releases extract the archive and place

Dec 11, 2022

Fast and flexible JSON encoder for Go

Fast and flexible JSON encoder for Go

Jettison Jettison is a fast and flexible JSON encoder for the Go programming language, inspired by bet365/jingo, with a richer features set, aiming at

Dec 21, 2022

Create go type representation from json

json2go Package json2go provides utilities for creating go type representation from json inputs. Json2go can be used in various ways: CLI tool Web pag

Dec 26, 2022
Related tags
Get JSON values quickly - JSON parser for Go
Get JSON values quickly - JSON parser for Go

get json values quickly GJSON is a Go package that provides a fast and simple way to get values from a json document. It has features such as one line

Dec 28, 2022
Fast JSON encoder/decoder compatible with encoding/json for Go
Fast JSON encoder/decoder compatible with encoding/json for Go

Fast JSON encoder/decoder compatible with encoding/json for Go

Jan 6, 2023
Package json implements encoding and decoding of JSON as defined in RFC 7159

Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions

Jun 26, 2022
Json-go - CLI to convert JSON to go and vice versa
Json-go - CLI to convert JSON to go and vice versa

Json To Go Struct CLI Install Go version 1.17 go install github.com/samit22/js

Jul 29, 2022
JSON Spanner - A Go package that provides a fast and simple way to filter or transform a json document

JSON SPANNER JSON Spanner is a Go package that provides a fast and simple way to

Sep 14, 2022
library for working amorphous data (as when you decode json into an interface{})

Introduction Decoding json into an interface{} produces an hierarchical arrangement of four data types: float64, string are 'primative types' and form

Aug 1, 2022
A blazingly fast JSON serializing & deserializing library
A blazingly fast JSON serializing & deserializing library

Sonic A blazingly fast JSON serializing & deserializing library, accelerated by JIT(just-in-time compiling) and SIMD(single-instruction-multi-data). B

Jan 5, 2023
A library to query the godoc.org JSON API.

gopkg This repository provides minimal Go package that makes queries against the godoc.org JSON API. Since that site has mostly been subsumed by pkg.g

Dec 2, 2022
Copy of Golang's json library with IsZero feature

json Copy of Golang's json library with IsZero feature from CL13977 Disclaimer It is a package primary used for my own projects, I will keep it up-to-

Oct 9, 2021
Fork of Go's standard library json encoder

A fork of the Go standard library's json encoder Why? https://github.com/golang/go/issues/6213 was proposed in 2013 but was never accepted. Difference

Nov 25, 2021