Skip to main content

A flexible JSON parser

Project description

fJson

English | 中文

fJson is a lenient JSON parser that supports comments, unquoted keys, arrays, tuples, etc. It does not support special character escaping but supports multiline strings, multiline comments, and full-width quotes.

The original intention was to solve the problem of LLMs' quirks, but later it was found that it could actually be used for other purposes, such as parsing command line arguments. Thus, this project was born.

This project is mainly for Python because Python supports dynamic types, so it can be parsed directly, while C++ and other languages need to implement data structure management themselves.

Features

  • Dictionary: Supports unquoted keys
  • List: Supports standard JSON list format
  • Tuple: Supports tuple format
  • Argument Group: Supports --key value form of argument groups
  • Multiline String: Supports R"delimiter(content)delimiter" form of multiline strings
  • Base64 Encoded String: Supports $"base64 string" form of Base64 encoded strings
  • Expression Evaluation: Supports some expression evaluations, such as Cartesian product, string concatenation, conditional expressions, etc.
  • More features are expected to be supported

Usage

Except for the decode function, other classes are used to parse JSON. The decode function is the external interface, which takes a string and returns a parsed object.

Example

from fjson import decode

text = '''
{
    key1: value1,
    key2: {
        key3: value3,
        key4: value4
    },
    key5: [value5, value6, value7],
    key6: (value8, value9),
    key7: (--key10 value10 --key11 value11),
    key8: R"delimiter(
        multi-line
        string
    )delimiter",
    key9: $"YmFzZTY0IGVuY29kZWQgc3RyaW5n"
}
'''

result = decode(text)
print(result)

Functions

decode(json_str: str) -> Any

Parses a JSON string and returns the parsed object.

def decode(json_str):
    """
    Parses a JSON string
    """
    tokens = fJsonLexer().tokenize(json_str)
    tokens = fJsonLexer().reject_comments(tokens)
    tokens = fJsonLexer().concat_negative_number(tokens)
    return fJsonBuilder(tokens).build()

Examples

import fJson as json

fjson_str = """
/* This is a comment */
[
    {
        name: 'John',
        age: 30,
        city: "New York",
        male: true
    }, // Dictionary
    {
        “name”: "Ja" + “ne”,
        'age': 5 * 5,
        "city": "London",
        male: fAlsE
    }
],  // List

{A,B,C} * {1,2,3}, // Cartesian product

(1, 2), // Tuple

{1, 2, 3}, // Set

--draw circle --rotate 90 --fill red --position (0,0) (1,1) (2,2), // Argument group

R"delimiter(
    multi-line
    string
)delimiter", // Multiline string

$"YmFzZTY0IGVuY29kZWQgYmFzZTY0IGVuY29kZWQ=", // Base64 encoded string

[1, 2, 3] + [4, 5, 6], // Concatenation expression

[1, 2] * [3, 4], // Element-wise multiplication

[1 ,2] * 3 // List multiplication
"""

print(json.decode(fjson_str))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple-fjson-0.1.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_fjson-0.1.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file simple-fjson-0.1.1.tar.gz.

File metadata

  • Download URL: simple-fjson-0.1.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.9

File hashes

Hashes for simple-fjson-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b80377ad8f160abd320adf14ac880ba6cabe9749a7f4a7f44779ae1bc0cb0326
MD5 1be9eb20fcf63d755c76b8e0a11f7458
BLAKE2b-256 6b0d2c73b32e5d3d1ac5b89deb3d2871ee5d5f266aaea10a1662514a8ddc3dd1

See more details on using hashes here.

File details

Details for the file simple_fjson-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: simple_fjson-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.9

File hashes

Hashes for simple_fjson-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 be72fc3dc9a076a4f36036e6814e9507fb91cdebbd1ba448f19327f924faed55
MD5 7b1ee78e41baeb3fd0d365b7fb72789e
BLAKE2b-256 b205194ef7aa10239290f39f4f8d8f09d2e9aafe714e673d412ce9fc72462a15

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page