jsonextend

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2023 License: MIT Imports: 4 Imported by: 0

README

go-jsonextend

Description

A simple Go json parser that support defining variables in json file.

Usage

as a json template processor, it can be used in the following ways:


package main

import (
    "fmt"
    "strings"

    "github.com/jaksonlin/go-jsonextend"
)

func main() {
    template := `{"hello": "world", "name": "this is my ${name}", "age": ${age}}`
    variables := map[string]interface{}{"name": "jakson", "age": 18}

    result, err := jsonextend.Parse(strings.NewReader(template), variables)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(result)

}


as a json unmarshaller, (tag support will be added in the future)


type SomeStruct struct {
    Field1 string
    Field2 bool
    Field3 int
    Field4 interface{}
}

testExample := SomeStruct{
    Field1: "hello",
    Field2: true,
    Field3: 100,
    Field4: nil,
}

data, _ := json.Marshal(testExample)
var out SomeStruct
err := jsonextend.Unmarshal(bytes.NewReader(data), nil, &out)
if err != nil {
    t.FailNow()
}

as a dyanmaic json unmarshaller, you can use variable as a route table to route the value to the right field.

type SomeStruct struct {
    Field1 string
    Field2 int
    Field3 interface{}
}
testExample := `
{
    "Field1": "hello ${var1}",
    "${var2}": ${var2Value},
    "Field3":${var3}
}`

variables := map[string]interface{}{
    "var1":      "world!",
    "var2":      "Field2",
    "var2Value": 100,
    "var3":      []int{1, 2, 3},
}

var out SomeStruct
err := jsonextend.Unmarshal(strings.NewReader(testExample), variables, &out)
if err != nil {
    t.FailNow()
}
if out.Field1 != "hello world!" {
    t.FailNow()
}
if out.Field2 != 100 {
    t.FailNow()
}
for i, v := range out.Field3.([]int) {
    if v != i+1 {
   	t.FailNow()
}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(reader io.Reader, variables map[string]interface{}) (string, error)

func Unmarshal added in v0.3.0

func Unmarshal(reader io.Reader, variables map[string]interface{}, out interface{}) error

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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