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.2.tar.gz (12.2 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.2-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file simple_fjson-0.1.2.tar.gz.

File metadata

  • Download URL: simple_fjson-0.1.2.tar.gz
  • Upload date:
  • Size: 12.2 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.2.tar.gz
Algorithm Hash digest
SHA256 5b6ab71a04a509c26b449509c81ab1e1235230849defcfa76ac4b7d47ff2d875
MD5 2579ea48b140bb7d5933c9ceb19904b6
BLAKE2b-256 da23c521b7758150ebb6adb040c2e3e76ae209ba287766f6d9e8a9cfc56861e4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simple_fjson-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cb96c8c9ea91f1719cd43af12ac3fa4d53048c193e613f8903dc069a7ea7c884
MD5 007793f39012c798adb04c234bd2cd84
BLAKE2b-256 9b532be4c062f951e24119ce914ccef23a38a163ec52bcbd517716bb9a01475a

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