Rust query string parser with nesting support

Related tags

Parsing queryst
Overview

What is Queryst?

This is a fork of the original, with serde and serde_json updated to 0.9

Build Status

A query string parsing library for Rust inspired by https://github.com/hapijs/qs. A part of REST-like API micro-framework Rustless.

# Cargo.toml

[dependencies]
queryst = "1"

API docs

Usage

Use queryst library to parse query-string to corresponding json values.

use queryst::parse;

// will contain result as Json value
let object = parse("foo[0][a]=a&foo[0][b]=b&foo[1][a]=aa&foo[1][b]=bb");

Description

queryst allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets []. or example, the string 'foo[bar]=baz' converts to this JSON:

{
  "foo": {
    "bar": "baz"
  }
}

URI encoded strings work too:

parse('a%5Bb%5D=c');
// { "a": { "b": "c" } }

You can also nest your objects, like 'foo[bar][baz]=foobarbaz':

{
  "foo": {
    "bar": {
      "baz": "foobarbaz"
    }
  }
}

Parsing Arrays

queryst can also parse arrays using a similar [] notation:

parse('a[]=b&a[]=c');
// { "a": ["b", "c"] }

You may specify an index as well:

parse('a[0]=c&a[1]=b');
// { "a": ["c", "b"] }

Note that the only difference between an index in an array and a key in an object is that the value between the brackets must be a number to create an array.

queryst does't allow to specify sparse indexes on arrays and will convert target array to object:

parse('a[1]=b&a[15]=c');
// { "a": {"1": "b", "15": "c"} }

Also if you mix notations, queryst will merge the two items into an object:

parse('a[0]=b&a[b]=c');
// { "a": { "0": "b", "b": "c" } }

You can also create arrays of objects:

parse('a[][b]=c');
// { "a": [{ "b": "c" }] }
Comments
  • refactor(lib): update serde and serde_json to 0.9

    refactor(lib): update serde and serde_json to 0.9

    The Serde update breaks its API, introducing a wrapper around the previous type alias for a BTreeMap. It also updates to_value to return a Result instead of a Value directly. I've updated the uses of BTreeMap to refer to the new Map struct instead, and added a .expect to the use of to_value - I don't think there's any reason it should fail, since our data is definitely serializable, but let me know if you'd prefer a different technique there.

    This is a breaking change, since it requires users of queryst to update their serde_json.

    opened by lord 3
  • feat(parser): Support assignment of 'None' values for standalone query-string keys

    feat(parser): Support assignment of 'None' values for standalone query-string keys

    In a number of query-string parsing and serialization environments there is ambiguity about the intent and meaning of ?a= vs ?a.

    The URI RFC allows for both inside URIs, and does not specify behaviour. Many environments choose to have both null/none/empty and also empty-string values serialize to a=.

    This can lead to ambiguity and in some cases it could be useful to reliably parse null/none/empty values back from a query-string (a), while retaining the ability to parse empty strings (a=).

    This pull request is a work-in-progress; I'd like to resolve the following:

    • [ ] Backwards-compatibility: ideally the existing behaviour should be maintained for prior library versions; in other words, unless requested otherwise, a None match for a value should return an empty string unless the caller opts-in for None result value parsing
    opened by jayaddison 2
  • Also accept semicolon as pair delimiter, behind feature

    Also accept semicolon as pair delimiter, behind feature

    I am trying to (ab)use this library to parse attributes in the path component of PKCS11 URIs (RFC 7512), which use semicolon delimiters (in the query component, the usual ampersand delimiters are used). I honestly don't know where this comes from, maybe the obsolete RFC 1866 which mentions accepting both semicolons and ampersands.

    What do you think about this? I'd completely understand if you think such a feature is out of scope.

    opened by nickray 1
  • refactor(parser): remove lint warning related to 'try' usage

    refactor(parser): remove lint warning related to 'try' usage

    This change eliminates a compile-time lint warning; the indicated try! macro is removed, and an ? operator is introduced instead.

    warning: use of deprecated item 'try': use the `?` operator instead
       --> src/parser.rs:179:29
        |
    179 |         let parse_key_res = try!(parse_key(key));
    
    opened by jayaddison 1
  • Wrongly parsing space / +

    Wrongly parsing space / +

    Hello,

    i have an input filed which should be send to the server for filtering which can be entered by an user. when he types some words+ (a little contrieved, i know, but still possible) Google Chrome generates the following request to the server: https://server/api/endpoint?filter%5Bquery%5D=some+words%2B&page%5Bsize%5D=100 Which looks fine to me. When parsing the querystring it gives the following result: Ok(Object({"filter": Object({"query": String("some+words+")}), "page": Object({"size": String("100")})}))

    In this Object the real plus is indistinguishable from the plus as space character.

    This probably stems from first calling url::percent_decode and then using its own parse_pairs instead of directly using https://docs.rs/url/1.7.2/url/struct.Url.html#method.query_pairs

    I try an create a failing test in a PR and fix the problem later.

    (There apparently is a difference between decoding a url and getting the query_params as pairs which has to do with if the parser is in urlformdecode mode or not. Don't Ask me)

    opened by spruce 0
  • Update project dependencies

    Update project dependencies

    Hello there!

    This MR updates the dependencies of the crate to their most recent versions, it also adds an include attribute based on the suggestions of the cargo-diet command.

    opened by DiD92 0
  • Travis build badge points to wrong repo

    Travis build badge points to wrong repo

    It points to https://travis-ci.org/github/lord/queryst-prime not https://travis-ci.org/github/s-panferov/queryst and the latter doesn't appear to be enabled yet.

    opened by palfrey 0
Owner
Stanislav Panferov
Senior Full Stack developer, Tech Lead, Architect. Rust, TypeScript
Stanislav Panferov
Website for Microformats Rust parser (using 'microformats-parser'/'mf2')

Website for Microformats Rust parser (using 'microformats-parser'/'mf2')

Microformats 5 Jul 19, 2022
A native Rust port of Google's robots.txt parser and matcher C++ library.

robotstxt A native Rust port of Google's robots.txt parser and matcher C++ library. Native Rust port, no third-part crate dependency Zero unsafe code

Folyd 72 Dec 11, 2022
Rust parser combinator framework

nom, eating data byte by byte nom is a parser combinators library written in Rust. Its goal is to provide tools to build safe parsers without compromi

Geoffroy Couprie 7.6k Jan 7, 2023
Parsing Expression Grammar (PEG) parser generator for Rust

Parsing Expression Grammars in Rust Documentation | Release Notes rust-peg is a simple yet flexible parser generator that makes it easy to write robus

Kevin Mehall 1.2k Dec 30, 2022
A fast monadic-style parser combinator designed to work on stable Rust.

Chomp Chomp is a fast monadic-style parser combinator library designed to work on stable Rust. It was written as the culmination of the experiments de

Martin Wernstål 228 Oct 31, 2022
A parser combinator library for Rust

combine An implementation of parser combinators for Rust, inspired by the Haskell library Parsec. As in Parsec the parsers are LL(1) by default but th

Markus Westerlind 1.1k Dec 28, 2022
LR(1) parser generator for Rust

LALRPOP LALRPOP is a Rust parser generator framework with usability as its primary goal. You should be able to write compact, DRY, readable grammars.

null 2.4k Jan 7, 2023
A typed parser generator embedded in Rust code for Parsing Expression Grammars

Oak Compiled on the nightly channel of Rust. Use rustup for managing compiler channels. You can download and set up the exact same version of the comp

Pierre Talbot 138 Nov 25, 2022
Soon to be AsciiDoc parser implemented in rust!

pagliascii "But ASCII Doc, I am Pagliascii" Soon to be AsciiDoc parser implemented in rust! This project is the current implementation of the requeste

Lukas Wirth 49 Dec 11, 2022
PEG parser for YAML written in Rust 🦀

yaml-peg PEG parser (pest) for YAML written in Rust ?? Quick Start ⚡️ # Run cargo run -- --file example_files/test.yaml # Output { "xmas": "true",

Visarut Phusua 4 Sep 17, 2022
This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCSS AST.

CSS(less like) parser written in rust (WIP) This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCS

Huang Liuhaoran 21 Aug 23, 2022
MRT/BGP data parser written in Rust.

BGPKIT Parser BGPKIT Parser aims to provides the most ergonomic MRT/BGP message parsing Rust API. BGPKIT Parser has the following features: performant

BGPKIT 46 Dec 19, 2022
This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCSS AST. Very early stage, do not use in production.

CSS(less like) parser written in rust (WIP) This project aims to implement a CSS(less like) parser in rust. Currently the code is targeting the PostCS

Huang Liuhaoran 21 Aug 23, 2022
A feature-few, no-allocation JSON parser in `no_std` rust.

Small JSON Parser in no_std This library reads and parses JSON strings. Its intended use case is to read a JSON payload once. It does not serialise da

Robert Spencer 18 Nov 29, 2022
A Gura parser for Rust

Gura Rust parser IMPORTANT: if you need to use Gura in a more user-friendly way, you have at your disposal Serde Gura which allows you to perform Seri

Gura Config Lang 21 Nov 13, 2022
Front Matter parser for Rust.

fronma Front Matter parser for Rust. Usage Add this crate as a dependency: [dependencies] fronma = "~0.1" then use fronma::parser::parse to parse text

Ryo Nakamura 6 Nov 19, 2021
A Rust crate for LL(k) parser combinators.

oni-comb-rs (鬼昆布,おにこんぶ) A Rust crate for LL(k) parser combinators. Main project oni-comb-parser-rs Sub projects The following is projects implemented

Junichi Kato 24 Nov 3, 2022
gors is an experimental go toolchain written in rust (parser, compiler).

gors gors is an experimental go toolchain written in rust (parser, compiler). Install Using git This method requires the Rust toolchain to be installe

Aymeric Beaumet 12 Dec 14, 2022
🕑 A personal git log and MacJournal output parser, written in rust.

?? git log and MacJournal export parser A personal project, written in rust. WORK IN PROGRESS; NOT READY This repo consolidates daily activity from tw

Steven Black 4 Aug 17, 2022