serializer

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2016 License: MIT Imports: 9 Imported by: 9

README


Build Status

Awesome GoLang

License

Releases

Godoc

Chat



Serialize any custom type to []byte or string. Your custom serializers are, finally, organised.

Easy-to-use

While providing robust set of features, simple to understand.

Built'n supported serializers:

Go here to learn how you can create and use your own custom Serializer.

This package is already used by Iris web framework and Q web framework, examples here.

Installation

The only requirement is the Go Programming Language, at least v1.6.

$ go get -u gopkg.in/kataras/go-serializer.v0

Usage

package main

import (
  "gopkg.in/kataras/go-serializer.v0"
  "gopkg.in/kataras/go-serializer.v0/markdown"
)

func main() {
  markdownSerializer := markdown.New()

  serializer.For("markdown", markdownSerializer)

  result, err := serializer.SerializeToString("markdown", "## Hello")
  // Optional third parameter is a map[string]interface{}, which is any runtime options for the serializers registered to 'markdown' key

  println(result)
}

explanation

Create your own serializer or use one of the project's built'n serializers, in this case we will use the markdown serializer will receive markdown content as string and return the HTML form of them

markdownSerializer := markdown.New()

Learn how to create your own custom serializer go to the serializer_test.go, it's just one function.

mySerializers := serializer.Serializers{}
mySerializers.For("key", markdownSerializer)

Add the serializer to a custom key, using the default Serializers{}. It supports more than one serializer for each key

serializer.For("markdown", markdownSerializer)

Serialize something with the registered serializers by a key. If more than one Serializer is registered to the same key then the final result will be the results of all of its(key's) serializers

result, err := serializer.SerializeToString("markdown", "## Hello")

.Serialize(...) returns []byte, SerializeToString(...) returns string, they're the same use that you need

Print the result, will be just a <h2>Hello</h2>

println(result)

For more, please read each of the Serializers you want to use, built'n supported are:

You can also navigate to the godocs.

FAQ

If you'd like to discuss this package, or ask questions about it, feel free to

Versioning

Current: v0.0.4

Read more about Semantic Versioning 2.0.0

People

The author of go-serializer is @kataras.

Contributing

If you are interested in contributing to the go-serializer project and add more serializers, please make a PR.

License

This project is licensed under the MIT License.

License can be found here.

Documentation

Overview

Package serializer helps GoLang Developers to serialize any custom type to []byte or string. Your custom serializers are finally, organised.

Built'n supported serializers: JSON, JSONP, XML, Markdown, Text, Binary Data.

This package is already used by Iris & Q Web Frameworks.

Index

Constants

View Source
const NotAllowedKeyChar = '.'

NotAllowedKeyChar the rune which is not allowed to be inside a serializer key string this exists because almost all package's users will use kataras/go-template with kataras/go-serializer in one method, so we need something to tell if the 'renderer' wants to render a serializer's result or the template's result, you don't have to worry about these things.

View Source
const (
	// Version current version number
	Version = "0.0.4"
)

Variables

This section is empty.

Functions

func For

func For(key string, serializer ...Serializer)

For puts a serializer(s) to the map

func Len

func Len() int

Len returns the length of the serializers map

func RegisterDefaults

func RegisterDefaults(serializers Serializers)

RegisterDefaults register defaults serializer for each of the default serializer keys (data,json,jsonp,markdown,text,xml)

func Serialize

func Serialize(key string, obj interface{}, options ...map[string]interface{}) ([]byte, error)

Serialize returns the result as bytes representation of the serializer(s)

func SerializeToString

func SerializeToString(key string, obj interface{}, options ...map[string]interface{}) (string, error)

SerializeToString returns the string representation of the serializer(s) same as Serialize but returns string

Types

type Options

type Options map[string]interface{}

Options is just a shortcut of a map[string]interface{}, which can be passed to the Serialize/SerializeToString funcs

type SerializeFunc

type SerializeFunc func(interface{}, ...map[string]interface{}) ([]byte, error)

SerializeFunc is the alternative way to implement a Serializer using a simple function

func (SerializeFunc) Serialize

func (s SerializeFunc) Serialize(obj interface{}, options ...map[string]interface{}) ([]byte, error)

Serialize accepts an object with serialization options and returns its bytes representation

type Serializer

type Serializer interface {
	// Serialize accepts an object with serialization options and returns its bytes representation
	Serialize(interface{}, ...map[string]interface{}) ([]byte, error)
}

Serializer is the interface which all serializers should implement

type Serializers

type Serializers map[string][]Serializer

Serializers is optionally, used when your app needs to manage more than one serializer keeps a map with a key of the ContentType(or any string) and a collection of Serializers

if a ContentType(key) has more than one serializer registered to it then the final result will be all of the serializer's results combined

func (Serializers) For

func (s Serializers) For(key string, serializer ...Serializer)

For puts a serializer(s) to the map

func (Serializers) Len

func (s Serializers) Len() int

Len returns the length of the serializers map

func (Serializers) Serialize

func (s Serializers) Serialize(key string, obj interface{}, options ...map[string]interface{}) ([]byte, error)

Serialize returns the result as bytes representation of the serializer(s)

func (Serializers) SerializeToString

func (s Serializers) SerializeToString(key string, obj interface{}, options ...map[string]interface{}) (string, error)

SerializeToString returns the string representation of the serializer(s) same as Serialize but returns string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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