GithubHelp home page GithubHelp logo

irevoire / permissive-json-pointer Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 18 KB

A permissive json pointer

Home Page: https://docs.rs/permissive-json-pointer/

Rust 100.00%
json serde-json json-pointer json-path permissive

permissive-json-pointer's Introduction

Permissive json pointer

This crate provide an interface a little bit similar to what you know as “json pointer”. But it’s actually doing something quite different.

The API

The crate provide only one function called [select_values]. It takes one object in parameter and a list of selectors. It then returns a new object containing only the fields you selected.

The selectors

The syntax for the selector is easier than with other API. There is only ONE special symbol, it’s the ..

If you write dog and provide the following object;

{
  "dog": "bob",
  "cat": "michel"
}

You’ll get back;

{
  "dog": "bob",
}

Easy right?

Now the dot can either be used as a field name, or as a nested object.

For example, if you have the following json;

{
  "dog.name": "jean",
  "dog": {
    "name": "bob",
    "age": 6
  }
}

What a crappy json! But never underestimate your users, they WILL somehow base their entire workflow on this kind of json. Here with the dog.name selector both fields will be selected and the following json will be returned;

{
  "dog.name": "jean",
  "dog": {
    "name": "bob",
  }
}

And as you can guess, this crate is as permissive as possible. It’ll match everything it can! Consider this even more crappy json;

{
  "pet.dog.name": "jean",
  "pet.dog": {
    "name": "bob"
  },
  "pet": {
    "dog.name": "michel"
    "dog": {
      "name": "milan"
    }
  }
}

If you write pet.dog.name everything will be selected.

Matching arrays

With this kind of selectors you can’t match a specific element in an array. Your selector will be applied to all the element in the array.

Consider the following json;

{
  "pets": [
    {
      "animal": "dog",
      "race": "bernese mountain",
    },
    {
      "animal": "dog",
      "race": "golden retriever",
    },
    {
      "animal": "cat",
      "age": 8,
    }
  ]
}

With the filter pets.animal you’ll get;

{
  "pets": [
    {
      "animal": "dog",
    },
    {
      "animal": "dog",
    },
    {
      "animal": "cat",
    }
  ]
}

The empty element in an array gets removed. So if you were to look for pets.age you would only get;

{
  "pets": [
    {
      "age": 8,
    }
  ]
}

And I think that’s all you need to know 🎉

permissive-json-pointer's People

Contributors

irevoire avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.