jwalk

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2018 License: MIT Imports: 4 Imported by: 8 modules, 14 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GoDoc Go Report Card Build Status

jwalk

Jwalk is built on top of easyjson/jlexer, and allows to easily unmarshal any JSON input with arbitrary key names by walking through it.

Usage

package main

import (
	"fmt"
	"log"

	"github.com/romanyx/jwalk"
)

const input = `{
	"key1": [{
		"id": 1
	}, {
		"id": 2
	}],
	"key2": [1,2],
	"key3": {
		"id": 1,
		"name": null
	}
}`

func main() {
	i, err := jwalk.Parse([]byte(input))
	if err != nil {
		log.Fatal(err)
	}

	switch v := i.(type) {
	case jwalk.ObjectWalker:
		v.Walk(func(key string, value interface{}) error {
			fmt.Println(key + ":")
			switch v := value.(type) {
			case jwalk.ObjectsWalker:
				v.Walk(func(obj jwalk.ObjectWalker) error {
					fmt.Println("\t-")
					obj.Walk(func(key string, value interface{}) error {
						if v, ok := value.(jwalk.Value); ok {
							fmt.Println("\t", key+":", v.Interface())
						}
						return nil
					})
					return nil
				})
			case jwalk.Value:
				fmt.Println("\t", v.Interface())
			case jwalk.ObjectWalker:
				v.Walk(func(key string, value interface{}) error {
					if v, ok := value.(jwalk.Value); ok {
						fmt.Println("\t", key+":", v.Interface())
					}
					return nil
				})
			}
			return nil
		})
	}
}

Testing

go test

Contributing

Please feel free to submit issues, fork the repository and send pull requests!

Documentation

Overview

Code generated by go run make_value.go. DO NOT EDIT.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(data []byte) (interface{}, error)

Parse parses the json data.

Types

type ObjectWalker

type ObjectWalker interface {
	Walk(func(name string, value interface{}) error) error
	json.Marshaler
}

ObjectWalker iterates through JSON object fields.

type ObjectsWalker

type ObjectsWalker interface {
	Walk(func(obj ObjectWalker) error) error
	json.Marshaler
}

ObjectsWalker iterates through array of JSON objects.

type Value

type Value interface {
	Bytes() []byte
	String() string
	Int8() int8
	Int16() int16
	Int32() int32
	Int64() int64
	Int() int
	Uint() uint
	Uint8() uint8
	Uint16() uint16
	Uint32() uint32
	Uint64() uint64
	Float32() float32
	Float64() float64
	Interface() interface{}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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