Home

Awesome

transit (go)

GoDoc

Transit is a data format and a set of libraries for conveying values between applications written in different languages. This library provides support for marshalling Transit data to/from Go.

This implementation's major.minor version number corresponds to the version of the Transit specification it supports.

Currently on the JSON formats are implemented. MessagePack is not implemented yet.

NOTE: Transit is a work in progress and may evolve based on feedback. As a result, while Transit is a great option for transferring data between applications, it should not yet be used for storing data durably over time. This recommendation will change when the specification is complete.

Usage

Reading data with Transit(go) involves creating a transit.Decoder and calling Decode:

package main

import (
	"fmt"
	"os"
	"github.com/russolsen/transit"
)

func ReadTransit(path string) interface{} {
	f, err := os.Open(path)

	if err != nil {
		fmt.Printf("Error opening file: %v\n", err)
		return nil
	}

	decoder := transit.NewDecoder(f)

	value, err := decoder.Decode()

	if err != nil {
		fmt.Printf("Error reading Transit data: %v\n", err)
		return nil
	}

	fmt.Printf("The value read is: %v\n", value)

	return value
}

Writing is similar:

func WriteTransit(path string, value interface{}) {
	f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)

	if err != nil {
		fmt.Printf("Error opening file: %v\n", err)
		return
	}

	encoder := transit.NewEncoder(f, false)

	err = encoder.Encode(value)

	if err != nil {
		fmt.Printf("Error writing Transit data: %v\n", err)
		return
	}
}

Default Type Mapping

Semantic Typewrite acceptsread produces
nullnilnil
stringstringstring
booleanboolbool
integer, signed 64 bitany signed or unsiged int typeint64
floating pt decimalfloat32 or float64float64
bytes[]byte[]byte
keywordtransit.Keywordtransit.Keyword
symboltransit.Symboltransit.Keyword
arbitrary precision decimalbig.Float or github.com/shopspring/decimal.Decimalgithub.com/shopspring/decimal.Decimal
arbitrary precision integerbig.Intbig.Int
point in timetime.Timetime.Time
point in time RFC 33339-time.Time
ugithub.com/pborman/uuid UUIDgithub.com/pborman/uuid UUID
urinet/url URLnet/url URL
charrunerune
special numbersAs defined by math NaN and math.Inf()TBD
arrayarrays or slices[]interface{}
mapmap[interface{}]interface{}map[interface{}]interface{}
settransit.Settransit.Set
listcontainer/list Listcontainer/list List
map w/ composite keystransit.CMaptransit.CMap
linktransit.Linktransit.Link
ratiobig.Ratbig.Rat

Copyright and License

Copyright © 2016 Russ Olsen

This library is a Go port of the Java version created and maintained by Cognitect, therefore

Copyright © 2014 Cognitect

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

This README file is based on the README from transit-csharp, therefore:

Copyright © 2014 NForza.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.