Skip to content

A language inspired by jq, to adapt JSON API requests and responses, part of the delegator architecture

Notifications You must be signed in to change notification settings

blast-hardcheese/json-adapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-adapter

json-adapter, a subset of combinators inspired by jq for doing JSON transformations. A part of the delegator architecture.

A number of operations are available. For a complete list, see ./src/language.rs.

pub enum Language {
    At(String),                            // .foo
    Map(Box<Language>),                    // map( ... )
    Object(Vec<(String, Language)>),       // { foo: .foo, bar: .bar  }
    List(Vec<Language>),                   // [ .foo, .bar, .baz ]
    Splat(Vec<Language>),                  // .foo, .bar
    Set(String),                           // ... | set("foo")
    Get(String),                           // get("bar") | ...
    Const(Value),                          // const(...)
    Identity,                              // .
    AndThen(Box<Language>, Box<Language>), // ... | ...
    Length,                                // [...] | length
    Join(String),                          // [...] | join(",")
    Default(Box<Language>),                // ... | default(<lang>)
    Flatten,                               // ... | flatten | ...
    ToString,                              // ... | tostring | ...
    EmitEvent(String),                     // ... | emit("topic")
}

Example

Below is a simple example demonstrating how to use the library to parse and transform JSON data:

use serde_json::json;
let data = json!({ "foo": "bar", "nested": { "key": "value" } });

let lang = Language::Object(vec![
    (String::from("foo"), Language::at("foo")),
    (String::from("nested_key"), Language::at("nested").and_then(Language::at("key"))),
]);

let transformed = step(&TranslateContext::noop(), &lang, &data, make_state()).unwrap();
println!("{:?}", transformed); // Prints: {"foo": "bar", "nested_key": "value"}

About

A language inspired by jq, to adapt JSON API requests and responses, part of the delegator architecture

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages