dson

package module
v0.0.0-...-26a73aa Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2019 License: MIT Imports: 6 Imported by: 0

README

dson.png

Build Status CodeFactor Go Report Card Codacy Badge Maintainability Test Coverage GitHub license Twitter

dson.go provides encoding, decoding, marshaling, unmarshaling, and verification of the DSON (Doge Serialized Object Notation) as defined here.

Index

Installing dson.go package

go get muzzammil.xyz/dsongo

Documentation

Syntax

DSON is built on two structures:

  • A collection of key/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Keys and Values

A key is a string in double quotes.

A value can be a string in double quotes, or a number, or yes or no or empty, or an object or an array. These structures can be nested.

such
  "foo" is "bar"
wow

is equivalent to this in JSON:

{
  "foo": "bar"
}
Strings

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes.

Numbers

A number is very much like a C or Java number, except it is presented in the dog-friendly octal base.

Objects

An object begins with such and ends with wow. Each key is followed by is and the key/value pairs are separated by , (comma) or . (dot) or ! or ?.

such
  "foo" is "bar",
  "number" is 42!
  "alive" is yes
wow

is equivalent to this in JSON:

{
  "foo": "bar",
  "number": 34,
  "alive": true
}
Arrays

An array begins with so and ends with many. Values are separated by and or also.

such
  "ID" is 1!
  "Name" is "Reds".
  "Colors" is so
    "Crimson" and "Red" and "Ruby" also "Maroon"
  many
wow

is equivalent to this in JSON:

{
  "id": 1,
  "Name": "Reds",
  "Colors": ["Crimson", "Red", "Ruby", "Maroon"]
}

Examples

Common imports
import (
    "fmt"

    "muzzammil.xyz/dsongo"
)
Encoding JSON into DSON
func main() {
    d := dson.Encode(`{"foo":"bar"}`)
    fmt.Println(d) // such "foo" is "bar" wow
}
Decoding DSON into JSON
func main() {
    j := dson.Decode(`such "foo" is "bar" wow`)
    fmt.Println(j) // {"foo":"bar"}
}
Validating DSON
func main() {
    if dson.Valid(`such "foo" is "bar" wow`) {
        fmt.Println("Valid DSON")
    } else {
        fmt.Println("Invalid DSON")
    }
}
Marshaling DSON
func main() {
    type ColorGroup struct {
        ID     int
        Name   string
        Colors []string
    }
    RedGroup := ColorGroup{
        ID:     1,
        Name:   "Reds",
        Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
    }
    r, err := dson.Marshal(RedGroup)
    if err == nil && dson.Valid(r) {
        fmt.Println(r) // such "ID" is 1! "Name" is "Reds". "Colors" is so "Crimson" and "Red" and "Ruby" also "Maroon" many wow
    }
}
Unmarshaling DSON
func main() {
    d := `so such "Name" is "Platypus" and "Order" is "Monotremata" wow and such "Name" is "Quoll" and "Order" is "Dasyuromorphia" wow many`
    if !dson.Valid(d) {
        fmt.Println("DSON is not valid")
        return
    }
    type Animal struct {
        Name  string
        Order string
    }
    var animals []Animal
    err := dson.Unmarshal(d, &animals)
    if err == nil {
        fmt.Printf("%+v", animals) // [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
    }
}

Documentation

Overview

Package dson implements encoding, decoding, marshaling, unmarshaling, and verification of the DSON (Doge Serialized Object Notation) as defined here: https://dogeon.xyz/.

Examples can be found here: https://muzzammil.xyz/dson.go

Version 1.0.0

MIT License; https://github.com/muhammadmuzzammil1998/dsongo/blob/master/LICENSE

(c) 2018 Muhammad Muzzammil <email@muzzammil.xyz>

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(dson string) string

Decode decodes a DSON string into the JSON equivalent and retruns that JSON string

func Encode

func Encode(json string) string

Encode encodes a JSON string into the DSON equivalent and retruns that DSON string

func Marshal

func Marshal(v interface{}) (string, error)

Marshal returns the DSON encoding of v.

func Unmarshal

func Unmarshal(dson string, v interface{}) error

Unmarshal parses the DSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.

func Valid

func Valid(dson string) bool

Valid returns true if the input DSON string is valid, otherwise returns false

Types

This section is empty.

Jump to

Keyboard shortcuts

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