ojson

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2021 License: MIT Imports: 5 Imported by: 9

README

ojson

Go library for ordered JSON values.

Motivation

Go's JSON (de-)serialization doesn't maintain the order of keys. Despite this being the expected behavior according to the JSON spec, some legacy JSON implementations expect key order to be preserved, especially since JSON is a commonly-used serialization protocol for data between different systems.

Thus, this library allows handling of ordered JSON data. Calling json.Unmarshal() on an ojson.Value behaves similarly to calling json.Unmarshal() on an interface{}, except that objects are unmarshaled to *ojson.Object, which is a struct containing a map[string]interface{} plus a key ordering. Furthermore, calling json.Marshal() on an ojson.Object outputs JSON that is serialized with the declared key ordering.

Example

package main

import (
  "encoding/json"

  "github.com/airplanedev/ojson"
)

func main() {
  var v ojson.Value
  b := []byte(`{"b":[],"c":true,"a":{"2":1,"1":{}}}`)
  json.Unmarshal(b, &v)

  // Here, s.([]byte) should be equal to b, preserving the order of keys in the relevant JSON objects.
  s, _ := json.Marshal(v)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Object

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

Object represents a JSON object that maintains key ordering.

func NewObject

func NewObject() *Object

func (*Object) Get

func (o *Object) Get(k string) (interface{}, bool)

func (*Object) KeyOrder added in v0.1.0

func (o *Object) KeyOrder() []string

func (Object) MarshalJSON

func (o Object) MarshalJSON() ([]byte, error)

func (*Object) Set

func (o *Object) Set(k string, v interface{})

func (*Object) SetAndReturn

func (o *Object) SetAndReturn(k string, v interface{}) *Object

SetAndReturn is equivalent to Set, while returning a pointer to the Object. Primarily used for creating Objects more easily, by allowing chaining of multiple SetAndReturns.

type Value

type Value struct {
	V interface{}
}

Value represents a JSON value unmarshaled from a string that maintains key ordering in its nested component objects. Calling json.Unmarshal() on it behaves similarly to calling json.Unmarshal() on interface{}, except that objects are unmarshaled to *ojson.Object, which maintains key ordering, instead of map[string]interface{}, which doesn't.

func MustNewValueFromJSON

func MustNewValueFromJSON(s string) Value

func NewValueFromJSON

func NewValueFromJSON(s string) (Value, error)

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

func (*Value) Scan

func (v *Value) Scan(src interface{}) error

func (*Value) UnmarshalJSON

func (v *Value) UnmarshalJSON(b []byte) error

func (Value) Value

func (v Value) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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