Contents

wuyu2015/dirtyjsonswift

This project is a Swift implementation of the [DirtyJSON Node.js Package](https://github.com/wuyu2015/DirtyJSON).

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 实现。

点这里查看演示:Demo App

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

Package Metadata

Repository: wuyu2015/dirtyjsonswift

Default branch: main

README: README.md