GithubHelp home page GithubHelp logo

go-json's Introduction

Go Package JSON

This package is very similar to the built-in go package called json, implementing 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.

Documentation: https://pkg.go.dev/github.com/pschou/go-json

Differences

The differences in this implementation are some additional functions and interfaces.

In the Decoder type, one will find two additional functions:

  • UseAutoConvert() - When enabled, this will attempt to convert any strings into defined element types, such as Integer, Boolean, or the CustomType interface
  • UseSlice() - When enabled, this will automatically convert an object into a slice if a slice is specified in the type declaration. When vendors provide JSON output, and this output is broken in that it can vary when one or more elements are returned, this decoder will try overcome this by creating a slice when specified and if one object is provided it will be a slice of one with that object.

Note: the encoding has two types TextMarshaler and TextUnmarshaler which can be interfaced via a custom type to implement readers and writers for non-standard JSON literals

    MarshalText() (text []byte, err error)
    UnmarshalText(text []byte) error

An example of how this could be useful is encoding and decoding custom time formats into a custom time.Duration type. Take for example a JSON API provides a time as "1D3H2M0S". With the use of a custom ToString / FromString this can be encoded / decoded when the JSON is being read in or written out.

go-json's People

Contributors

pschou avatar roelofjan-elsinga avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.