jsonstruct

package module
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2016 License: MIT Imports: 5 Imported by: 0

README

jsonstruct

Build Status Godoc license

A JSON deserializer for Go structures for Go 1.7+

Also visit Godoc.

Installation

go get -u github.com/berlincount/jsonstruct

Background

You are using Google's JSONAPI package with your Go web application and have a lot of structs in your database schema that you don't want to also have to implement as individual Go structures.

Introduction

jsonstruct uses StructOf to construct a Type which can be used to create Values which then can be used by other packages using reflection for structure discovery, like sqlx or GORM.

jsonstruct uses the following structures for descriptions:

type Field struct {
        Name      string            "json:\"name\""
        Type      string            "json:\"type\""
        Tags      reflect.StructTag "json:\"tags\""
}

type Struct struct {
        Struct string "json:\"struct\""
        Fields []Field
}

which allows e.g. to describe the example structures from JSON API using the following structure:

{"struct": "comment",
 "fields": [
  {"name": "ID",     "type": "int",    "tags": "jsonapi:\"primary,comments\""},
  {"name": "PostID", "type": "int",    "tags": "jsonapi:\"attr,post_id\""},
  {"name": "Body",   "type": "string", "tags": "jsonapi:\"attr,body\""}
]}
{"struct": "post",
 "fields": [
  {"name": "ID",       "type": "int", "tags": "jsonapi:\"primary,posts\""},
  {"name": "BlogID",   "type": "int", "tags": "jsonapi:\"attr,blog_id\""},
  {"name": "Title",    "type": "string", "tags": "jsonapi:\"attr,title\""},
  {"name": "Body",     "type": "string", "tags": "jsonapi:\"attr,body\""},
  {"name": "Comments", "type": "[]*comment", "tags": "jsonapi:\"relation,comments\""}
]}
{"struct": "blog",
 "fields": [
 {"name": "ID", "type": "int", "tags": "jsonapi:\"primary,blogs\""},
 {"name": "Title", "type": "string", "tags": "jsonapi:\"attr,title\""},
 {"name": "Posts", "type": "[]*post", "tags": "jsonapi:\"relation,posts\""},
 {"name": "CurrentPost", "type": "*post", "tags": "jsonapi:\"relation,current_post\""},
 {"name": "CurrentPostID", "type": "int", "tags": "jsonapi:\"attr,current_post_id\""},
 {"name": "CreatedAt", "type": "time.Time", "tags": "jsonapi:\"attr,created_at\""},
 {"name": "ViewCount", "type": "int", "tags": "jsonapi:\"attr,view_count\""}
]}

Example Apps

examples/jsonapi/jsonapi.go

examples/sqlx/sqlx.go

examples/gorm/gorm.go

These runnable files show using jsonstruct with JSON API as well as in conjunction with a database using sqlx or GORM.

You can use GB to build example binaries.

Contributing

Fork, Change, Pull Request with tests.

Documentation

Overview

Package jsonstruct provides a JSON deserializer for Go structures for Go 1.7+

Index

Examples

Constants

This section is empty.

Variables

View Source
var TypeMap map[string]reflect.Type

TypeMap provides a Type registry, mapping type names to reflect Types

Functions

func Decode

func Decode(r io.Reader) (map[string]reflect.Type, error)

Decode one or multiple Go structures from JSON, register and return their Types

Example
package main

import (
	"github.com/berlincount/jsonstruct"

	"strings"

	"github.com/davecgh/go-spew/spew"
)

func main() {
	testStructJSON := `
        {"struct": "test",
         "fields": [
          {"name": "TestInt",    "type": "int",    "tags": "testTag:\"first_field\""},
          {"name": "TestString", "type": "string", "tags": "testTag:\"second_field\""}
        ]}
	`
	decodedStructs, _ := jsonstruct.Decode(strings.NewReader(testStructJSON))

	spewWithoutAddresses := spew.ConfigState{DisablePointerAddresses: true}
	spewWithoutAddresses.Dump(decodedStructs)
}
Output:

(map[string]reflect.Type) (len=1) {
(string) (len=4) "test": (*reflect.rtype)(struct { TestInt int "testTag:\"first_field\""; TestString string "testTag:\"second_field\"" })
}

func MapType

func MapType(Name string, Type reflect.Type)

MapType adds a new Type (plus its [], * and []* variants) to the registry

Types

type Field

type Field struct {
	Name string            "json:\"name\""
	Type string            "json:\"type\""
	Tags reflect.StructTag "json:\"tags\""
}

Field holds a JSON description of individual Go fields

type Struct

type Struct struct {
	Struct string "json:\"struct\""
	Fields []Field
}

Struct holds JSON description of Go structures

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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