thedevsaddam / gojsonq

A simple Go package to Query over JSON/YAML/XML/CSV Data
https://github.com/thedevsaddam/gojsonq/wiki
MIT License
2.18k stars 140 forks source link

Provide option for custom JSON decoder #17

Closed thedevsaddam closed 6 years ago

thedevsaddam commented 6 years ago

Option for user to provide custom JSON Decoder , like below:

package main

import (
    "fmt"

    "github.com/davecgh/go-spew/spew"
    jsoniter "github.com/json-iterator/go"
    "github.com/pquerna/ffjson/ffjson"
    "github.com/thedevsaddam/gojsonq"
)

func main() {
    jq := gojsonq.New(gojsonq.SetDecoder(&iteratorDecoder{})).
        File("./data.json").
        From("vendor.items").
        Where("id", "=", 1).OrWhere("id", "=", 3)

    spew.Dump("Result: ", jq.Only("name", "id"), jq.Error())
}

type iteratorDecoder struct {
}

func (i *iteratorDecoder) Decode(data []byte, v interface{}) error {
    var json = jsoniter.ConfigCompatibleWithStandardLibrary
    return json.Unmarshal(data, &v)
}

type ffDecoder struct {
}

func (f *ffDecoder) Decode(data []byte, v interface{}) error {
    return ffjson.Unmarshal(data, &v)
}