DirtyJSON

0.0.1

A Swift package for automatically fixing invalid JSON data. 超强纠错 JSON 解析器(Swift 版)。
wuyu2015/DirtyJSONSwift

What's New

DirtyJSONSwift 0.0.1

2024-04-16T02:19:09Z

DirtyJSONSwift

This project is a Swift implementation of the DirtyJSON Node.js Package.

DirtyJSONSwift is a Swift library designed for automatically fixing invalid JSON data.

Douglas Crockford created and promoted the JSON format with the intention of providing a strict, user-friendly, and standardized data format, where syntax errors are not allowed. JSON output by programs is also expected to be flawless (as it ideally shouldn't be handwritten). However, with the emergence of artificial intelligence like ChatGPT, instances of incorrect JSON output have been observed. In my existing data, out of 80,000 conversations, approximately 4,000 outputs were in an incorrect JSON format. DirtyJSONSwift attempts to rectify some of the errors in JSON generated by AI.

DirtyJSONSwift provides the following automatic repair features:

  1. Automatically removes // and /* */ comments;
  2. Automatically removes the trailing comma ,;
  3. Automatically corrects misuse and mismatch of {}, [];
  4. Automatically adds quotes to keys and values (except for numbers and true, false, null, of course)
  5. Automatically escapes unescaped quotes inside quotes, for example: ["quotes in "quotes" in quotes"] to ["quotes in \"quotes\" in quotes"];
  6. Unifies true, false, null to lowercase;
  7. Tolerantly supports illegal full-width symbols whenever possible;
  8. Automatically identifies and removes improperly written symbols;
  9. Automatically completes unfinished JSON by appending ] or } at the end of the JSON structure.

No more worries about poorly formatted JSON output by AI. You can even write JSON by hand recklessly.

Installation

You can integrate MaterialColorSwift into your Xcode project using Swift Package Manager:

  1. In Xcode, select "File" -> "Swift Packages" -> "Add Package Dependency..."
  2. Enter the URL of this repository: https://github.com/wuyu2015/DirtyJSONSwift.git
  3. Follow the prompts to complete the integration process.

Examples

DirtyJSONSwift does not require object keys to be quoted, and can handle single-quoted value strings.

import DirtyJSON
print(DirtyJSON.fix("{ test: 'this is a test', 'number': 1.23e10 }"))
// output: {"test":"this is a test","number":1.23e10}

DirtyJSONSwift can handle embedded quotes in strings.

import DirtyJSON
print(DirtyJSON.fix("{\"test\": \"some text \"a quote\" more text\"}"))
// output: {"test":"some text \"a quote\" more text"}

DirtyJSONSwift can handle newlines inside a string.

import DirtyJSON
print(DirtyJSON.fix("{\"test\": \"each \n on \n new \n line\"}"))
// output: {"test":"each \n on \n new \n line"}

DirtyJSONSwift can handle // and /* */ comments.

import DirtyJSON
let jsonDataWithComments = """
{
    // This is a comment
    "name": "John",

    /*
    This is a multiline
    comment
    */
    "age": 30
}
""";
let fixedData = DirtyJSON.fix(jsonDataWithComments)
print(fixedData)
// output: {"name":"John","age":30}

DirtyJSONSwift can handle the trailing comma ,.

import DirtyJSON
let jsonDataWithTrailingCommas = """
{
    "name": "John",
    "age": 30, // Notice this trailing comma
}
""";
let fixedData = DirtyJSON.fix(jsonDataWithTrailingCommas)
print(fixedData)
// output: {"name":"John","age":30}

DirtyJSONSwift can handle misuse and mismatch of {}, [].

import DirtyJSON
let jsonDataWithMismatch = """
{
    "name": "John",
    "age": 30,
    "friends": [
        "Alice",
        "Bob",
    } // this '}' should be ']'
】// this abnormal square bracket  should be '}'
""";
let fixedData = DirtyJSON.fix(jsonDataWithMismatch)
print(fixedData)
// output: {"name":"John","age":30,"friends":["Alice","Bob"]}

DirtyJSONSwift can handle unfinished JSON.

import DirtyJSON
let unfinishedJsonData = """
{
    "name": "John",
    "age": 30,
    "friends": [
        "Alice",
        "Bob",
"""
let fixedData = DirtyJSON.fix(unfinishedJsonData)
print(fixedData)
// output: {"name":"John","age":30,"friends":["Alice","Bob"]}

DirtyJSONSwift can handle improperly written symbols.

import DirtyJSON
let improperlyWrittenJSON = "},{「a」:1,,b:[2,,“3”:},]},";
let fixedData = DirtyJSON.fix(improperlyWrittenJSON)
print(fixedData)
// output: {"a":1,"b":[2,"3"]}

License

MIT License


Simplified Chinese introduction:

超强纠错 JSON 解析器(Swift 版)

本项目是 DirtyJSON Node.js Package 的 Swift 实现。

DirtyJSONSwift 可以为你解析非法的 JSON 数据。

Douglas Crockford 创建和推广 JSON 格式的初衷是提供一种严格、易用、统一的数据格式,语法错误等是不被允许的。通过程序生成的 JSON 完美无暇(这本来就不应该是手写的格式),直到人工智能 ChatGPT 出现,让它输出 JSON 的时候,在我已有的数据看来,8 万次对话有 4000 次输出了错误格式的 JSON,DirtyJSONSwift 试图修复一些 AI 生成 JSON 的错误。

DirtyJSONSwift 提供以下自动修复功能:

  1. 自动删除 ///* */ 注释;
  2. 自动删除末尾的 ,
  3. 自动纠正 {}, [] 的乱写和不匹配问题;
  4. 自动为键和值添加引号(当然,除了数字和 true, false, null);
  5. 自动转义引号内未转义的引号,例如:["quotes in "quotes" in quotes"] 转为 ["quotes in \"quotes\" in quotes"]
  6. truefalsenull 统一转换为小写;
  7. 尽可能宽容地支持非法的全角符号;
  8. 自动识别并删除错误写入的符号;
  9. 通过在结尾处添加 ]} 自动补全未完成的 JSON 结构;

再也不怕 AI 输出的 JSON 乱写一气了。你也可以用手写的方式乱写 JSON 了。

安装

您可以使用 Swift Package Manager 将 MaterialColorSwift 集成到您的 Xcode 项目中:

  1. 在 Xcode 中,选择 "File" -> Swift Packages" -> ""Add Package Dependency..."
  2. 输入此 URL:https://github.com/wuyu2015/DirtyJSONSwift.git
  3. 按照提示完成集成过程。

示例

不写引号或写为单引号。

import DirtyJSON
print(DirtyJSON.fix("{ test: 'this is a test', 'number': 1.23e10 }"))
// 输出: {"test":"this is a test","number":1.23e10}

处理字符串中嵌入的引号。

import DirtyJSON
print(DirtyJSON.fix("{\"test\": \"some text \"a quote\" more text\"}"))
// 输出: {"test":"some text \"a quote\" more text"}

转义字符串内的换行符。

import DirtyJSON
print(DirtyJSON.fix("{\"test\": \"each \n on \n new \n line\"}"))
// 输出: {"test":"each \n on \n new \n line"}

在 JSON 内部随意书写单行 // 和多行 /* */ 注释。

import DirtyJSON
let jsonDataWithComments = """
{
    // 这个是单行注释
    "name": "小明",

    /*
    这个是多行注释
    这个是多行注释
    */
    "age": 30
}
""";
let fixedData = DirtyJSON.fix(jsonDataWithComments)
print(fixedData)
// 输出: {"name":"小明","age":30}

自动删除最后一个逗号 ,

import DirtyJSON
let jsonDataWithTrailingCommas = """
{
    "name": "小明",
    "age": 30, // 注意这个逗号将被删除
}
""";
let fixedData = DirtyJSON.fix(jsonDataWithTrailingCommas)
print(fixedData)
// 输出: {"name":"小明","age":30}

DirtyJSONSwift 可以处理不匹配的 {}, []

import DirtyJSON
let jsonDataWithMismatch = """
{
    "name": "小明",
    "age": 30,
    "friends": [
        "小红",
        "小刚",
    } // 这里的 '}' 应该是 ']'
】// 这里的 '】' 应该是 '}'
""";
let fixedData = DirtyJSON.fix(jsonDataWithMismatch)
print(fixedData)
// 输出: {"name":"小明","age":30,"friends":["小红","小刚"]}

DirtyJSONSwift 可以处理未完成的 JSON。

import DirtyJSON
let unfinishedJsonData = """
{
    "name": "小明",
    "age": 30,
    "friends": [
        "小红",
        "小刚",
""";
let fixedData = DirtyJSON.fix(unfinishedJsonData)
print(fixedData)
// 输出: {"name":"小明","age":30,"friends":["小红","小刚"]}

让事情更混乱一些。

import DirtyJSON
let improperlyWrittenJSON = "},{「a」:1,,b:[2,,“3”:},]},";
let fixedData = DirtyJSON.fix(improperlyWrittenJSON)
print(fixedData)
// 输出: {"a":1,"b":[2,"3"]}

许可证

MIT License

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Apr 18 2024 15:51:03 GMT-0900 (Hawaii-Aleutian Daylight Time)