Skip to content

cyberstudio/jsonj

Repository files navigation

JSONJ

Go GoDoc Go Report Card

JSONJ can be used to manipulate raw json input using marks and custom fragments generators.

  • Library guarantees valid json output syntax;
  • Library doesn't validate output json semantic like unique keys.

Marks

One can apply generator to marks of json input. Each mark is json key name. For example, uuid and id maybe used as mark.

[
    {
        "uuid": "302b7140-dfff-4f4b-9e72-5b731ec14d85",
        "id": 1234
    },
    {
        "uuid": "302b7140-dfff-4f4b-9e72-5b731ec14d85",
        "id": 1234
    }
]

Mark maybe renamed in result of operation. It depends on operation mode and its rules.

Advice: wrap marks by special chars, i.e. __uuid__ and unwrap during operation.

Operations

Library supports number of operations, named Mode:

  • ModeInsert: insert key/value pair after the mark.
  • ModeReplaceValue: replace value, or convert it;
  • ModeReplace: replace entire key/value pair;
  • ModeDelete: delete key/value.

Fragments generators

Type GenerateFragmentBatchFunc describes interface of generators. Key feature of generators is batch processing. Batches speed up result output.

Example:

// GeneratorParams customize generator behavior
type GeneratorParams struct{
    EmbedObjectURL bool
    BaseURL        string
}

// Generator returns batch of "url": "http://localhost/{id}" fragments
// to be inserted to json
func Generator(ctx context.Context, iterator jsonj.FragmentIterator, p interface{}) ([]interface{}, error) {
    params := p.(GeneratorParams)
    if !params.EmbedObjectURL {
        return jsonj.EmptyFragmentsGenerator(ctx, iterator, p)
    }
    type Item struct {
        URL *string `json:"url"`
    }
    result := make([]interface{}, 0, iterator.Count())
    for iterator.Next() {
        var id int64
        if err := iterator.BindParams(&id); err != nil {
            panic(err)
        }
        var item Item
        if id != 0 {
            addr := fmt.Sprintf("%s/%s", params.BaseURL, id)
            item.URL = &addr
        }
        result = append(result, item)
    }
    return result, nil
}

Reporting Issues

  • For questions or further assistance, please check existing issues or create a new one as needed.
  • If you encounter a problem or have a question, please create an issue using the provided templates.