schemaJSON

package module
v0.0.0-...-87081e2 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

schemaJSON

translate your json-schema data to json data, if you have set default value, it will be translated too

将json-schema数据转为默认json数据。

Get Started

Install Package
go get github.com/wuranxu/schemaJSON

Example
package main

import (
	"fmt"
	"github.com/wuranxu"
)

func main() {
	data := `{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "default": "wuranxu",
      "description": "f"
    },
    "age": {
      "type": "number",
      "default": 10,
      "description": "f"
    },
    "class": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "default": "math"
          },
          "score": {
            "type": "number",
            "default": 120
          }
        },
        "required": [
          "name",
          "score"
        ],
        "description": "f"
      },
      "description": "f"
    }
  },
  "description": "few"
}`
	s := schemaJSON.NewSchema(data)
	data, err := s.GenerateJSON() // return string
	fmt.Println(data, err)
	data2, err := s.Generate() // return interface{}
	fmt.Println(data2, err)
}

// {"age":10,"class":[{"name":"math","score":120}],"name":"wuranxu"} <nil>
// map[age:10 class:[map[name:math score:120]] name:wuranxu] <nil>

Documentation

Overview

The package can translate your json-schema data to json data, if you have set default value, it will be translated too It depends on simpleson: https://github.com/simplejson/simplejson

Index

Constants

View Source
const (
	NotValidJSONSchema = "valid JSON schema data"
	NotValidMap        = "is not a map"
)
View Source
const (
	NotValid = "data is not valid"
)

Variables

This section is empty.

Functions

func NewSchema

func NewSchema(data string) *schema

func Version

func Version() string

returns the current implementation version

Types

type JSONData

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

func NewJSON

func NewJSON(data interface{}) (*JSONData, error)

func (*JSONData) Array

func (j *JSONData) Array(path ...interface{}) ([]interface{}, error)

return array and error

func (*JSONData) Bool

func (j *JSONData) Bool(path ...interface{}) (bool, error)

return bool and error

func (*JSONData) Data

func (j *JSONData) Data() []byte

get origin data

func (*JSONData) Float64

func (j *JSONData) Float64(path ...interface{}) (float64, error)

return float64 and error

func (*JSONData) FormatString

func (j *JSONData) FormatString() string

translate []byte to string

func (*JSONData) GetJSON

func (j *JSONData) GetJSON(path ...interface{}) (*JSONData, error)

Get JSONData

func (*JSONData) Int

func (j *JSONData) Int(path ...interface{}) (int, error)

return int and error

func (*JSONData) Int64

func (j *JSONData) Int64(path ...interface{}) (int64, error)

return int64 and error

func (*JSONData) Interface

func (j *JSONData) Interface(path ...interface{}) (interface{}, error)

return interface{} and error

func (*JSONData) Map

func (j *JSONData) Map(path ...interface{}) (map[string]interface{}, error)

return map and error

func (*JSONData) Set

func (j *JSONData) Set(key string, value interface{}) (string, error)

set key

func (*JSONData) SetPath

func (j *JSONData) SetPath(v interface{}, path ...string) error

set key-value by path

func (*JSONData) StrArray

func (j *JSONData) StrArray(path ...interface{}) ([]string, error)

return []string and error

func (*JSONData) String

func (j *JSONData) String(path ...interface{}) (string, error)

get string by path, return string and error

func (*JSONData) Uint64

func (j *JSONData) Uint64(path ...interface{}) (uint64, error)

return Uint64 and error

Jump to

Keyboard shortcuts

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