首頁 >後端開發 >Python教學 >怎麼使用Python寫一個簡單的JSONParser

怎麼使用Python寫一個簡單的JSONParser

WBOY
WBOY轉載
2023-05-12 14:58:061193瀏覽

JSON Tokenizer

JSON 的詞法分析,我主要是參考上面這個截圖裡面的方式,自己寫了一個簡單的範例。寫得比較簡單,應該要說它只能支援 JSON 的一個簡單子集。

這裡 TOKEN 的種類,參考了 https://json.org,不過它的 JSON 的語法格式是帶 whitespace 的,我不習慣處理這個,所以沒有參考它的語法。經過詞法分析之後,過濾掉了 空格、換行、製表符,我這裡就是簡單的丟棄不處理。

json_tokenizer.py

使用正規表示式來進行 JSON 的詞法分析。

import json
import re

from typing import Dict, List, Union


# TOKEN 的种类
LEFT_BRACE = "LEFT_BRACE"        # {
RIGHT_BRACE = "RIGHT_BRACE"      # }
LEFT_BRACKET = "LEFT_BRACKET"    # ]
RIGHT_BRACKET = "RIGHT_BRACKET"  # [
COLON = "COLON"                  # :
COMMA = "COMMA"                  # ,
NUMBER = "NUMBER"                # ".*?"
STRING = "STRING"                # [1-9]\d*
BOOL = "BOOL"                    # true/false
NULL = "NULL"                    # null
NEWLINE = "NEWLINE"            # \n
SKIP = "SKIP"                    # ' ', '\t'
MISMATCH = "MISMATCH"            # mismatch

# 处理 token 的正则
token_specification = [
    ('LEFT_BRACE', r'[{]'),
    ('RIGHT_BRACE', r'[}]'),
    ('LEFT_BRACKET', r'[\[]'),
    ('RIGHT_BRACKET', r'[\]]'),
    ('COLON', r'[:]'),
    ('COMMA', r'[,]'),
    ('NUMBER', r'-?[1-9]+[0-9]*'),
    ('STRING', r'".*?"'),
    ('BOOL', r'(true)|(false)'),
    ('NULL', r'null'),
    ('NEWLINE', r'\n'),
    ('SKIP', r'[ \t]'),
    ('MISMATCH', r'.')
]

tok_regex = &#39;|&#39;.join(&#39;(?P<%s>%s)&#39; % pair for pair in token_specification)
print("Debug: ", tok_regex)


def process(kind: str, value: str) -> Dict[str, Union[str, bool, int, None]]:
    """
    处理输入的 kind 和 value,并生成 Dict 对象,简单表示 token 对象
    """
    if kind == STRING:
        # 去掉外层的双引号,暂时没有比较好的方式
        return {"kind": kind, "value": value[1:-1]}
    if kind == NUMBER:
        return {"kind": kind, "value": int(value)}
    if kind == BOOL:
        if value == "true":
            return {"kind": kind, "value": True}
        else:
            return {"kind": kind, "value": False}
    if kind == NULL:
        return {"kind": kind, "value": None}
    return {"kind": kind, "value": value}


def tokenizer(json_str: str) -> List[Dict[str, Union[str, bool, int, None]]]:
    """
    tokenizer
    """
    tokens = []
    for m in re.finditer(tok_regex, json_str):
        # 获取 token 的类型
        kind = m.lastgroup
        # 获取 token 的值
        value = m.group()
        if kind == MISMATCH:
            raise Exception("json format is error")
        if kind == NEWLINE:
            continue
        if kind == SKIP:
            continue
        token = process(kind=kind, value=value)
        tokens.append(token)

    return tokens


if __name__ == "__main__":
    json_doc = open("./demo.json", "r", encoding="utf-8").read()
    tokens = tokenizer(json_doc)
    if tokens:
        json.dump(tokens, open("./json_tokens.json", "w",
                               encoding="utf-8"), ensure_ascii=False)

我這裡把輸入、輸出資料全部放在文件裡面了,下面我貼一下我輸入資料和部分輸出資料。

demo.json

{
    "name": "小黑子",
    "age": 3,
    "gender": false,
    "other_info": {
        "friends": [
            "嘎子",
            "潘叔",
            "狗"
        ],
        "declaration": "练习时长两年半",
        "hobbies": [
            "唱",
            "跳",
            "rap",
            "篮球????"
        ]
    }
}

json_token.json 部分數據,數據我格式化了,所以比較長,這裡只截取一部分。

怎麼使用Python寫一個簡單的JSONParser

JSON Parser

json_parser.py

對上一個步驟產生的 token 序列,進行 parser,產生 JSON 對應的 Dict 物件。 parser 的實作參考了 antlr4 的 json 語法文件,它去掉了 whitespace,處理起來更簡單一點。

import json
from typing import Dict, Union

# TOKEN 的种类
LEFT_BRACE = "LEFT_BRACE"        # {
RIGHT_BRACE = "RIGHT_BRACE"      # }
LEFT_BRACKET = "LEFT_BRACKET"    # ]
RIGHT_BRACKET = "RIGHT_BRACKET"  # [
COLON = "COLON"                  # :
COMMA = "COMMA"                  # ,
NUMBER = "NUMBER"                # ".*?"
STRING = "STRING"                # [1-9]\d*
BOOL = "BOOL"                    # true/false
NULL = "NULL"                    # null


class Token(object):
    """为了简单,就不创建这个了"""


class JSON_Parser(object):
    """
    JSON_Parser the class aims parse input token sequence into a python object or array.
    """

    def __init__(self, tokens) -> None:
        self.index = 0
        self.tokens = tokens

    def get_token(self) -> Dict[str, Union[str, int, bool, None]]:
        """
        get current&#39;s token
        """
        if self.index < len(self.tokens):
            return self.tokens[self.index]
        else:
            raise Exception("index out of range.")

    def move_token(self) -> Dict[str, Union[str, int, bool, None]]:
        """
        move to next token and return it
        """
        if self.index + 1 < len(self.tokens):
            self.index = self.index + 1
            return self.tokens[self.index]
        else:
            raise Exception("index out of range.")

    def parse(self):
        """
        parse whole json
        """
        token = self.get_token()
        if token.get("kind") == LEFT_BRACE:
            return self.parse_obj()
        elif token.get("kind") == LEFT_BRACKET:
            return self.parse_arr()
        else:
            raise Exception("error json, neither object or array.")

    def parse_obj(self):
        """
        parse object
        """
        obj = {}
        token = self.move_token()
        kind = token.get("kind")
        # &#39;{&#39; &#39;}&#39;
        if kind == RIGHT_BRACE:
            return obj
        # &#39;{&#39; pair (&#39;,&#39; pair)* &#39;}&#39;
        name, val = self.parse_pair()
        obj[name] = val

        while self.index < len(self.tokens):
            token = self.move_token()
            kind = token.get("kind")
            if kind == COMMA:
                self.move_token()
                name, val = self.parse_pair()
                obj[name] = val
            elif kind == RIGHT_BRACE:
                return obj
            else:
                raise Exception("parse object encounter error")

    def parse_arr(self):
        """
        parse array
        """
        arr = []
        token = self.move_token()
        kind = token.get("kind")
        # &#39;[&#39; &#39;]&#39;
        if kind == RIGHT_BRACE:
            return arr
        # &#39;[&#39; value (&#39;,&#39; value)* &#39;]&#39;
        val = self.parse_value()
        arr.append(val)

        while self.index < len(self.tokens):
            token = self.move_token()
            kind = token.get("kind")
            if kind == COMMA:
                self.move_token()
                val = self.parse_value()
                arr.append(val)
            elif kind == RIGHT_BRACKET:
                return arr
            else:
                raise Exception("parse array encounter error")

    def parse_value(self):
        """
        parse value
        """
        token = self.get_token()
        kind = token.get("kind")
        if kind == LEFT_BRACE:
            return self.parse_obj()
        elif kind == LEFT_BRACKET:
            return self.parse_arr()
        elif kind == STRING or kind == NUMBER or kind == BOOL:
            return token.get("value")
        elif kind == NULL:
            return
        else:
            raise Exception("encounter unexcepted token")

    def parse_pair(self):
        """
        parse pair
        """
        token = self.get_token()
        kind = token.get("kind")
        name = token.get("value")
        # STRING &#39;:&#39; value
        if kind == STRING:
            token = self.move_token()
            kind = token.get("kind")
            if kind == COLON:
                token = self.move_token()
                return name, self.parse_value()

        raise Exception("parse pair encounter error")


if __name__ == "__main__":
    # json token 文件路径
    TOKEN_PATH = "./json_tokens.json"
    # 读取 token 序列
    input_tokens = [token for token in json.load(
        open(TOKEN_PATH, "r", encoding="utf-8"))]
    if not input_tokens:
        raise Exception("input token sequence is empty")

    # 调试的时候,用来查表的,很方便定位到 index 走到哪一个 token 了

    for i, tok in enumerate(input_tokens):
        print(f"debug {i:2d} --> {tok}")
    print("\n===========================================\n")
    parser = JSON_Parser(tokens=input_tokens)
    json_obj = parser.parse()
    # 再将 object 转成 json 并格式化后输出
    print(json.dumps(json_obj, ensure_ascii=False, indent=4))

輸出結果:

怎麼使用Python寫一個簡單的JSONParser

#

以上是怎麼使用Python寫一個簡單的JSONParser的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除